public async Task <AuthInfo> LoginAsync() { _loginResultWaitHandle = new AsyncAutoResetEvent(false); try { using (var httpClient = HttpClientFactory.Build()) { var httpResult = await httpClient.GetAsync(Constants.DiscoveryEndpoint); var json = await httpResult.Content.ReadAsStringAsync(); var jObj = JObject.Parse(json); var configuration = new AuthorizationServiceConfiguration( Android.Net.Uri.Parse(jObj["authorization_endpoint"].ToString()), Android.Net.Uri.Parse(jObj["token_endpoint"].ToString())) { DiscoveryDoc = new AuthorizationServiceDiscovery(new JSONObject(json)) }; MakeAuthRequest(configuration, new AuthState()); await _loginResultWaitHandle.WaitAsync(); } } catch (AuthorizationException) { } return(new AuthInfo() { IsAuthorized = _authState?.IsAuthorized ?? false, AccessToken = _authState?.AccessToken, IdToken = _authState?.IdToken, RefreshToken = _authState?.RefreshToken, Scope = _authState?.Scope }); }
private async Task HandleReject() { using (var client = HttpClientFactory.Build()) { var tokenStorage = DependencyService.Get <ITokenStorage>(); var authInfo = await tokenStorage.GetAuthInfo(); var jObj = new JObject { { "auth_req_id", AuthReqId }, { "id_token_hint", authInfo.IdToken } }; var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(Constants.RejectAuthReqId), Content = new StringContent(jObj.ToString(), Encoding.UTF8, "application/json") }; await client.SendAsync(request); if (IsRejected != null) { IsRejected(this, EventArgs.Empty); } } }
private async Task ConfirmAuthReqId(string idToken, string authReqId, IEnumerable <string> permissionIds) { using (var client = HttpClientFactory.Build()) { var jObj = new JObject { { "id_token_hint", idToken }, { "auth_req_id", authReqId }, { "permission_ids", JArray.FromObject(permissionIds) } }; var httpRequestMessage = new HttpRequestMessage { RequestUri = new Uri(Constants.ConfirmAuthReqId), Method = HttpMethod.Post, Content = new StringContent(jObj.ToString(), Encoding.UTF8, "application/json") }; await client.SendAsync(httpRequestMessage); } }
internal void NotifyOfCallback(Intent intent) { try { if (!intent.HasExtra(Constants.AuthStateKey)) { _authState = null; } else { try { _authState = AuthState.JsonDeserialize(intent.GetStringExtra(Constants.AuthStateKey)); } catch (JSONException) { _authState = null; } } if (_authState != null) { AuthorizationResponse response = AuthorizationResponse.FromIntent(intent); AuthorizationException authEx = AuthorizationException.FromIntent(intent); _authState.Update(response, authEx); if (response != null) { try { var clientAuthentication = _authState.ClientAuthentication; } catch (ClientAuthenticationUnsupportedAuthenticationMethod) { SetWaitHandle(); return; } var request = response.CreateTokenExchangeRequest(); using (var httpClient = HttpClientFactory.Build()) { var jObj = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("client_id", request.ClientId), new KeyValuePair <string, string>("code", request.AuthorizationCode), new KeyValuePair <string, string>("code_verifier", request.CodeVerifier), new KeyValuePair <string, string>("grant_type", request.GrantType), new KeyValuePair <string, string>("scope", request.Scope), new KeyValuePair <string, string>("redirect_uri", request.RedirectUri.ToString()) }; var httpRequest = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(request.Configuration.TokenEndpoint.ToString()), Content = new FormUrlEncodedContent(jObj) }; var httpResult = httpClient.SendAsync(httpRequest).Result; var tokenResponseJObject = JObject.Parse(httpResult.Content.ReadAsStringAsync().Result); tokenResponseJObject.Add("request", JObject.Parse(request.JsonSerializeString())); var tokenResponse = TokenResponse.JsonDeserialize(new JSONObject(tokenResponseJObject.ToString())); ReceivedTokenResponse(tokenResponse, null); } } } else { SetWaitHandle(); } } catch (Exception) { SetWaitHandle(); } }