/// <summary> /// Add claims to the request context of the given request message /// </summary> /// <param name="wwwAuthenticateHeader">String representation of www Authenticate Header</param> /// <param name="newRequest">Request message to add claims to</param> private void AddClaimsToRequestContext(HttpRequestMessage newRequest, string wwwAuthenticateHeader) { int claimsStart = wwwAuthenticateHeader.IndexOf("claims=", StringComparison.OrdinalIgnoreCase); if (claimsStart < 0) { return; // do nothing as there is no claims in www Authenticate Header } claimsStart += 8; // jump to the index after the opening quotation mark // extract and decode the Base 64 encoded claims property byte[] bytes = Convert.FromBase64String(wwwAuthenticateHeader.Substring(claimsStart, wwwAuthenticateHeader.Length - claimsStart - 1)); string claimsChallenge = Encoding.UTF8.GetString(bytes, 0, bytes.Length); // Try to get the current options otherwise create new ones AuthenticationHandlerOption authenticationHandlerOption = newRequest.GetMiddlewareOption <AuthenticationHandlerOption>() ?? AuthOption; IAuthenticationProviderOption authenticationProviderOption = authenticationHandlerOption.AuthenticationProviderOption ?? new CaeAuthenticationProviderOption(); // make sure that there is no information loss due to casting by copying over the scopes information if necessary CaeAuthenticationProviderOption caeAuthenticationProviderOption; if (authenticationProviderOption is CaeAuthenticationProviderOption option) { caeAuthenticationProviderOption = option; } else { caeAuthenticationProviderOption = new CaeAuthenticationProviderOption(authenticationProviderOption); } // update the claims property in the options caeAuthenticationProviderOption.Claims = claimsChallenge; authenticationHandlerOption.AuthenticationProviderOption = caeAuthenticationProviderOption; // update the request context with the updated options GraphRequestContext requestContext = newRequest.GetRequestContext(); requestContext.MiddlewareOptions[typeof(AuthenticationHandlerOption).ToString()] = authenticationHandlerOption; newRequest.Properties[typeof(GraphRequestContext).ToString()] = requestContext; }
/// <summary> /// Create instance of <see cref="CaeAuthenticationProviderOption"/> from an <see cref="IAuthenticationProviderOption"/> instance /// </summary> public CaeAuthenticationProviderOption(IAuthenticationProviderOption authenticationProviderOption) { this.Scopes = authenticationProviderOption?.Scopes; }