public static void AdalIdTest() { IHttpWebRequest request = NetworkPlugin.HttpWebRequestFactory.Create("https://test"); AdalIdHelper.AddAsHeaders(request); Verify.AreEqual(4, request.Headers.Count); Verify.IsNotNull(request.Headers[AdalIdParameter.Product]); Verify.IsNotNull(request.Headers[AdalIdParameter.Version]); Verify.IsNotNull(request.Headers[AdalIdParameter.CpuPlatform]); #if TEST_ADAL_WINRT_UNIT Verify.IsNull(request.Headers[AdalIdParameter.OS]); Verify.IsNotNull(request.Headers[AdalIdParameter.DeviceModel]); #else Verify.IsNotNull(request.Headers[AdalIdParameter.OS]); Verify.IsNull(request.Headers[AdalIdParameter.DeviceModel]); #endif RequestParameters parameters = new RequestParameters(null, new ClientKey("client_id")); AdalIdHelper.AddAsQueryParameters(parameters); Verify.AreEqual(5, parameters.Count); Verify.IsNotNull(parameters[AdalIdParameter.Product]); Verify.IsNotNull(parameters[AdalIdParameter.Version]); Verify.IsNotNull(parameters[AdalIdParameter.CpuPlatform]); #if TEST_ADAL_WINRT_UNIT Verify.IsFalse(parameters.ContainsKey(AdalIdParameter.OS)); Verify.IsNotNull(parameters[AdalIdParameter.DeviceModel]); #else Verify.IsNotNull(parameters[AdalIdParameter.OS]); Verify.IsFalse(parameters.ContainsKey(AdalIdParameter.DeviceModel)); #endif }
private RequestParameters CreateAuthorizationRequest(string loginHint, bool includeFormsAuthParam) { RequestParameters authorizationRequestParameters = new RequestParameters(this.Resource, this.ClientKey); authorizationRequestParameters[OAuthParameter.ResponseType] = OAuthResponseType.Code; authorizationRequestParameters[OAuthParameter.RedirectUri] = this.redirectUriRequestParameter; if (!string.IsNullOrWhiteSpace(loginHint)) { authorizationRequestParameters[OAuthParameter.LoginHint] = loginHint; } if (this.CallState != null && this.CallState.CorrelationId != Guid.Empty) { authorizationRequestParameters[OAuthParameter.CorrelationId] = this.CallState.CorrelationId.ToString(); } // ADFS currently ignores the parameter for now. if (promptBehavior == PromptBehavior.Always) { authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.Login; } else if (promptBehavior == PromptBehavior.RefreshSession) { authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.RefreshSession; } else if (promptBehavior == PromptBehavior.Never) { authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.AttemptNone; } if (includeFormsAuthParam) { authorizationRequestParameters[OAuthParameter.FormsAuth] = OAuthValue.FormsAuth; } AdalIdHelper.AddAsQueryParameters(authorizationRequestParameters); if (!string.IsNullOrWhiteSpace(extraQueryParameters)) { // Checks for extraQueryParameters duplicating standard parameters Dictionary <string, string> kvps = EncodingHelper.ParseKeyValueList(extraQueryParameters, '&', false, this.CallState); foreach (KeyValuePair <string, string> kvp in kvps) { if (authorizationRequestParameters.ContainsKey(kvp.Key)) { var ex = new AdalException(AdalError.DuplicateQueryParameter, string.Format(AdalErrorMessage.DuplicateQueryParameterTemplate, kvp.Key)); Logger.LogException(this.CallState, ex); throw ex; } } authorizationRequestParameters.ExtraQueryParameter = extraQueryParameters; } return(authorizationRequestParameters); }
private async Task <AuthenticationResult> SendHttpMessageAsync(RequestParameters requestParameters) { string uri = HttpHelper.CheckForExtraQueryParameter(this.Authenticator.TokenUri); TokenResponse tokenResponse = await HttpHelper.SendPostRequestAndDeserializeJsonResponseAsync <TokenResponse>(uri, requestParameters, this.CallState); AuthenticationResult result = OAuth2Response.ParseTokenResponse(tokenResponse, this.CallState); if (result.RefreshToken == null && requestParameters.ContainsKey(OAuthParameter.RefreshToken)) { result.RefreshToken = requestParameters[OAuthParameter.RefreshToken]; Logger.Verbose(this.CallState, "Refresh token was missing from the token refresh response, so the refresh token in the request is returned instead"); } result.IsMultipleResourceRefreshToken = (!string.IsNullOrWhiteSpace(result.RefreshToken) && !string.IsNullOrWhiteSpace(tokenResponse.Resource)); return(result); }
private async Task<AuthenticationResult> SendHttpMessageAsync(RequestParameters requestParameters) { string uri = HttpHelper.CheckForExtraQueryParameter(this.Authenticator.TokenUri); TokenResponse tokenResponse = await HttpHelper.SendPostRequestAndDeserializeJsonResponseAsync<TokenResponse>(uri, requestParameters, this.CallState); AuthenticationResult result = OAuth2Response.ParseTokenResponse(tokenResponse, this.CallState); if (result.RefreshToken == null && requestParameters.ContainsKey(OAuthParameter.RefreshToken)) { result.RefreshToken = requestParameters[OAuthParameter.RefreshToken]; Logger.Verbose(this.CallState, "Refresh token was missing from the token refresh response, so the refresh token in the request is returned instead"); } result.IsMultipleResourceRefreshToken = (!string.IsNullOrWhiteSpace(result.RefreshToken) && !string.IsNullOrWhiteSpace(tokenResponse.Resource)); return result; }
private RequestParameters CreateAuthorizationRequest(string loginHint, bool includeFormsAuthParam) { RequestParameters authorizationRequestParameters = new RequestParameters(this.Resource, this.ClientKey); authorizationRequestParameters[OAuthParameter.ResponseType] = OAuthResponseType.Code; authorizationRequestParameters[OAuthParameter.RedirectUri] = this.redirectUriRequestParameter; if (!string.IsNullOrWhiteSpace(loginHint)) { authorizationRequestParameters[OAuthParameter.LoginHint] = loginHint; } if (this.CallState != null && this.CallState.CorrelationId != Guid.Empty) { authorizationRequestParameters[OAuthParameter.CorrelationId] = this.CallState.CorrelationId.ToString(); } // ADFS currently ignores the parameter for now. if (promptBehavior == PromptBehavior.Always) { authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.Login; } else if (promptBehavior == PromptBehavior.RefreshSession) { authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.RefreshSession; } else if (promptBehavior == PromptBehavior.Never) { authorizationRequestParameters[OAuthParameter.Prompt] = PromptValue.AttemptNone; } if (includeFormsAuthParam) { authorizationRequestParameters[OAuthParameter.FormsAuth] = OAuthValue.FormsAuth; } AdalIdHelper.AddAsQueryParameters(authorizationRequestParameters); if (!string.IsNullOrWhiteSpace(extraQueryParameters)) { // Checks for extraQueryParameters duplicating standard parameters Dictionary<string, string> kvps = EncodingHelper.ParseKeyValueList(extraQueryParameters, '&', false, this.CallState); foreach (KeyValuePair<string, string> kvp in kvps) { if (authorizationRequestParameters.ContainsKey(kvp.Key)) { throw new AdalException(AdalError.DuplicateQueryParameter, string.Format(AdalErrorMessage.DuplicateQueryParameterTemplate, kvp.Key)); } } authorizationRequestParameters.ExtraQueryParameter = extraQueryParameters; } return authorizationRequestParameters; }