protected override void OnCreate(Bundle savedInstanceState)
        {
            AuthorizationResponse  resp = AuthorizationResponse.FromIntent(Intent);
            AuthorizationException ex   = AuthorizationException.FromIntent(Intent);

            base.OnCreate(savedInstanceState);
        }
Exemplo n.º 2
0
        internal void NotifyOfCallback(Intent intent)
        {
            try
            {
                if (!intent.HasExtra(Constants.AuthStateKey))
                {
                    _authState = null;
                }
                else
                {
                    try
                    {
                        _authState = AuthState.JsonDeserialize(intent.GetStringExtra(Constants.AuthStateKey));
                    }
                    catch (JSONException ex)
                    {
                        Console.WriteLine("Malformed AuthState JSON saved: " + ex);
                        _authState = null;
                    }
                }
                if (_authState != null)
                {
                    AuthorizationResponse  response = AuthorizationResponse.FromIntent(intent);
                    AuthorizationException authEx   = AuthorizationException.FromIntent(intent);
                    _authState.Update(response, authEx);

                    if (response != null)
                    {
                        Console.WriteLine("Received AuthorizationResponse.");
                        try
                        {
                            var clientAuthentication = _authState.ClientAuthentication;
                        }
                        catch (ClientAuthenticationUnsupportedAuthenticationMethod ex)
                        {
                            _loginResultWaitHandle.Set();

                            Console.WriteLine(
                                "Token request cannot be made, client authentication for the token endpoint could not be constructed: " +
                                ex);

                            return;
                        }
                        _authService.PerformTokenRequest(response.CreateTokenExchangeRequest(), ReceivedTokenResponse);
                    }
                    else
                    {
                        Console.WriteLine("Authorization failed: " + authEx);
                    }
                }
                else
                {
                    _loginResultWaitHandle.Set();
                }
            }
            catch (Exception)
            {
                _loginResultWaitHandle.Set();
            }
        }
Exemplo n.º 3
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //SetContentView(Resource.Layout.activity_token);

            _authService = new AuthorizationService(this);

            if (savedInstanceState != null)
            {
                if (savedInstanceState.ContainsKey(KEY_AUTH_STATE))
                {
                    try
                    {
                        _authState = AuthState.JsonDeserialize(savedInstanceState.GetString(KEY_AUTH_STATE));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Malformed authorization JSON saved: " + ex);
                    }
                }

                if (savedInstanceState.ContainsKey(KEY_USER_INFO))
                {
                    try
                    {
                        _userInfoJson = new JSONObject(savedInstanceState.GetString(KEY_USER_INFO));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Failed to parse saved user info JSON: " + ex);
                    }
                }
            }

            if (_authState == null)
            {
                _authState = GetAuthStateFromIntent(Intent);
                var response = AuthorizationResponse.FromIntent(Intent);
                var ex       = AuthorizationException.FromIntent(Intent);
                _authState.Update(response, ex);

                if (response != null)
                {
                    Console.WriteLine("Received AuthorizationResponse");
                    PerformTokenRequest(response.CreateTokenExchangeRequest());
                }
                else
                {
                    Console.WriteLine("Authorization failed: " + ex);
                }
            }

            //RefreshUi();

            //var contactsViewController = App.GetContactsPage().CreateViewController();
            //NavigationController.PresentViewController(contactsViewController, true, null);
            Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
Exemplo n.º 4
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_token);

            if (savedInstanceState != null)
            {
                if (savedInstanceState.ContainsKey(KEY_AUTH_STATE))
                {
                    authState = AuthState.JsonDeserialize(savedInstanceState.GetString(KEY_AUTH_STATE));
                }

                if (savedInstanceState.ContainsKey(KEY_USER_INFO))
                {
                    userInfoJson = new JSONObject(savedInstanceState.GetString(KEY_USER_INFO));
                }
            }

            if (authState == null)
            {
                authState = GetAuthStateFromIntent(Intent);
                AuthorizationResponse  response = AuthorizationResponse.FromIntent(Intent);
                AuthorizationException ex       = AuthorizationException.FromIntent(Intent);
                authState.Update(response, ex);
                if (response != null)
                {
                    PerformTokenRequest(response.CreateTokenExchangeRequest());
                }
            }
        }
        /// <summary>
        /// Handles the auth result.Should be called from <see cref="Activity.OnActivityResult(int, Result, Intent)">
        /// </summary>
        /// <returns>The auth result.</returns>
        /// <param name="intent">Intent.</param>
        public async Task HandleAuthResult(Intent intent)
        {
            NonNull(intent, "intent");
            NonNull(authenticateTaskComplete, "authenticateTaskComplete");

            AuthorizationResponse  response = AuthorizationResponse.FromIntent(intent);
            AuthorizationException error    = AuthorizationException.FromIntent(intent);

            authState.Update(response, error);

            if (response != null)
            {
                try
                {
                    User user = await exchangeTokens(response);

                    authenticateTaskComplete.TrySetResult(user);
                }
                catch (Exception ex)
                {
                    this.logger.Error("Unexpected error in token exchange", ex);
                    authenticateTaskComplete.TrySetException(ex);
                }
            }
            else
            {
                authenticateTaskComplete.TrySetException(error);
            }
        }
Exemplo n.º 6
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            AuthorizationResponse  resp = AuthorizationResponse.FromIntent(Intent);
            AuthorizationException ex   = AuthorizationException.FromIntent(Intent);

            AuthService.Instance.AuthActivityCreated(resp, ex);
            Finish();
        }
        public void NotifyCallback(Intent intent)
        {
            if (!intent.TryGetAuthStateFromExtra(out authorizationState))
            {
                taskCompletionSource.SetResult(false);
                return;
            }

            AuthorizationResponse  authorizationResponse  = AuthorizationResponse.FromIntent(intent);
            AuthorizationException authorizationException = AuthorizationException.FromIntent(intent);

            authorizationState.Update(authorizationResponse, authorizationException);

            if (authorizationException != null)
            {
                taskCompletionSource.SetResult(false);
                return;
            }

            authorizationService.PerformTokenRequest(authorizationResponse.CreateTokenExchangeRequest(), ReceivedTokenResponse);
        }
Exemplo n.º 8
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (authState == null)
            {
                authState = GetAuthStateFromIntent(Intent);
                AuthorizationResponse  response = AuthorizationResponse.FromIntent(Intent);
                AuthorizationException ex       = AuthorizationException.FromIntent(Intent);
                authState.Update(response, ex);

                if (response != null)
                {
                    Console.WriteLine("Received AuthorizationResponse.");
                    App.OpenIdService.PerformTokenRequest(authState.JsonSerializeString());
                }
                else
                {
                    Console.WriteLine("Authorization failed: " + ex);
                }
            }

            Finish();
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.activity_token);

            authService = new AuthorizationService(this);

            if (savedInstanceState != null)
            {
                if (savedInstanceState.ContainsKey(KEY_AUTH_STATE))
                {
                    try
                    {
                        authState = AuthState.JsonDeserialize(savedInstanceState.GetString(KEY_AUTH_STATE));
                    }
                    catch (JSONException ex)
                    {
                        Console.WriteLine("Malformed authorization JSON saved: " + ex);
                    }
                }

                if (savedInstanceState.ContainsKey(KEY_USER_INFO))
                {
                    try
                    {
                        userInfoJson = new JSONObject(savedInstanceState.GetString(KEY_USER_INFO));
                    }
                    catch (JSONException ex)
                    {
                        Console.WriteLine("Failed to parse saved user info JSON: " + ex);
                    }
                }
            }

            if (authState == null)
            {
                authState = GetAuthStateFromIntent(Intent);
                AuthorizationResponse  response = AuthorizationResponse.FromIntent(Intent);
                AuthorizationException ex       = AuthorizationException.FromIntent(Intent);
                authState.Update(response, ex);

                if (response != null)
                {
                    Console.WriteLine("Received AuthorizationResponse.");
                    PerformTokenRequest(response.CreateTokenExchangeRequest());
                }
                else
                {
                    Console.WriteLine("Authorization failed: " + ex);
                }
            }

            RefreshUi();

            var refreshTokenButton = FindViewById <Button>(Resource.Id.refresh_token);

            refreshTokenButton.Click += delegate
            {
                PerformTokenRequest(authState.CreateTokenRefreshRequest());
            };

            var viewProfileButton = FindViewById <Button>(Resource.Id.view_profile);

            viewProfileButton.Click += delegate
            {
                Task.Run(() => FetchUserInfo());
            };
        }
Exemplo n.º 10
0
        internal void NotifyOfCallback(Intent intent)
        {
            try
            {
                if (!intent.HasExtra("authState"))
                {
                    _authState = null;
                }
                else
                {
                    try
                    {
                        _authState = AuthState.JsonDeserialize(intent.GetStringExtra("authState"));
                    }
                    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 = new HttpClient(GetUnsecuredHandler()))
                        {
                            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();
            }
        }