144 : this(userAgent, token, null, null)
146 if (userAgent ==
null)
147 throw new ArgumentNullException(nameof(userAgent));
149 throw new ArgumentNullException(nameof(token));
151 throw new InvalidOperationException(
"token.Bearer must be set!");
176 public ApiHeaders(ProductHeaderValue userAgent,
string username,
string password)
177 : this(userAgent, null, username, password)
179 if (userAgent ==
null)
180 throw new ArgumentNullException(nameof(userAgent));
181 if (username ==
null)
182 throw new ArgumentNullException(nameof(username));
183 if (password ==
null)
184 throw new ArgumentNullException(nameof(password));
195 public ApiHeaders(RequestHeaders requestHeaders,
bool ignoreMissingAuth,
bool allowEventStreamAccept)
197 if (requestHeaders ==
null)
198 throw new ArgumentNullException(nameof(requestHeaders));
201 var errorBuilder =
new StringBuilder();
202 var multipleErrors =
false;
207 multipleErrors =
true;
208 errorBuilder.AppendLine();
211 badHeaders |= headerType;
212 errorBuilder.Append(message);
216 var eventStreamAccept =
new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(
TextEventStreamMime);
217 if (!requestHeaders.Accept.Any(accept => accept.IsSubsetOf(jsonAccept)))
218 if (!allowEventStreamAccept)
219 AddError(
HeaderErrorTypes.Accept, $
"Client does not accept {ApplicationJsonMime}!");
220 else if (!requestHeaders.Accept.Any(eventStreamAccept.IsSubsetOf))
221 AddError(
HeaderErrorTypes.Accept, $
"Client does not accept {ApplicationJsonMime} or {TextEventStreamMime}!");
223 if (!requestHeaders.Headers.TryGetValue(HeaderNames.UserAgent, out var userAgentValues) || userAgentValues.Count == 0)
224 AddError(
HeaderErrorTypes.UserAgent, $
"Missing {HeaderNames.UserAgent} header!");
229 AddError(
HeaderErrorTypes.UserAgent, $
"Malformed {HeaderNames.UserAgent} header!");
234 if (!requestHeaders.Headers.TryGetValue(
ApiVersionHeader, out var apiUserAgentHeaderValues) || !ProductInfoHeaderValue.TryParse(apiUserAgentHeaderValues.FirstOrDefault(), out var apiUserAgent) || apiUserAgent.Product.Name !=
AssemblyName.Name)
236 else if (!
Version.TryParse(apiUserAgent.Product.Version, out apiVersion))
239 if (!requestHeaders.Headers.TryGetValue(HeaderNames.Authorization, out StringValues authorization))
241 if (!ignoreMissingAuth)
242 AddError(
HeaderErrorTypes.AuthorizationMissing, $
"Missing {HeaderNames.Authorization} header!");
246 var auth = authorization.First();
247 var splits =
new List<string>(auth.Split(
' '));
248 var scheme = splits.First();
249 if (String.IsNullOrWhiteSpace(scheme))
250 AddError(
HeaderErrorTypes.AuthorizationInvalid,
"Missing authentication scheme!");
254 var parameter = String.Concat(splits);
255 if (String.IsNullOrEmpty(parameter))
256 AddError(
HeaderErrorTypes.AuthorizationInvalid,
"Missing authentication parameter!");
259 if (requestHeaders.Headers.TryGetValue(
InstanceIdHeader, out var instanceIdValues))
261 var instanceIdString = instanceIdValues.FirstOrDefault();
262 if (instanceIdString !=
default && Int64.TryParse(instanceIdString, out var instanceId))
269 if (requestHeaders.Headers.TryGetValue(
OAuthProviderHeader, out StringValues oauthProviderValues))
271 var oauthProviderString = oauthProviderValues.First();
272 if (Enum.TryParse<
OAuthProvider>(oauthProviderString,
true, out var oauthProvider))
278 AddError(
HeaderErrorTypes.OAuthProvider, $
"Missing {OAuthProviderHeader} header!");
292 catch (ArgumentException ex) when (ex is not ArgumentNullException)
294 AddError(
HeaderErrorTypes.AuthorizationInvalid, $
"Invalid JWT: {ex.Message}");
299 string badBasicAuthHeaderMessage = $
"Invalid basic {HeaderNames.Authorization} header!";
303 var base64Bytes = Convert.FromBase64String(parameter);
304 joinedString = Encoding.UTF8.GetString(base64Bytes);
312 var basicAuthSplits = joinedString.Split(
ColonSeparator, StringSplitOptions.RemoveEmptyEntries);
313 if (basicAuthSplits.Length < 2)
320 Password = String.Concat(basicAuthSplits.Skip(1));
323 AddError(
HeaderErrorTypes.AuthorizationInvalid,
"Invalid authentication scheme!");
333 errorBuilder.Insert(0, $
"Multiple header validation errors occurred:{Environment.NewLine}");
373 throw new ArgumentNullException(nameof(headers));
375 throw new InvalidOperationException(
"Specified different instance IDs in constructor and SetRequestHeaders!");
379 headers.UserAgent.Add(
new ProductInfoHeaderValue(
UserAgent));
387 headers.Authorization =
new AuthenticationHeaderValue(
389 Convert.ToBase64String(Encoding.UTF8.GetBytes($
"{Username}:{Password}")));
394 if (instanceId.HasValue)
395 headers.Add(
InstanceIdHeader, instanceId.Value.ToString(CultureInfo.InvariantCulture));