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());
        }
        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();
            }
        }
        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());
                }
            }
        }
        private AuthState GetAuthState()
        {
            var pref      = _context.GetSharedPreferences(nameof(AuthService), FileCreationMode.Private);
            var stateJson = pref.GetString(nameof(AuthState), null);

            if (stateJson != null)
            {
                return(AuthState.JsonDeserialize(stateJson));
            }
            else
            {
                return(new AuthState());
            }
        }
        private static AuthState GetAuthStateFromIntent(Intent intent)
        {
            if (!intent.HasExtra(EXTRA_AUTH_STATE))
            {
                throw new InvalidOperationException("The AuthState instance is missing in the intent.");
            }

            try
            {
                return(AuthState.JsonDeserialize(intent.GetStringExtra(EXTRA_AUTH_STATE)));
            }
            catch (JSONException ex)
            {
                throw new InvalidOperationException("The AuthState instance is missing in the intent.", ex);
            }
        }
        static AuthState GetAuthStateFromIntent(Intent intent)
        {
            if (!intent.HasExtra(EXTRA_AUTH_STATE))
            {
                throw new InvalidOperationException("The AuthState instance is missing in the intent.");
            }

            try
            {
                return(AuthState.JsonDeserialize(intent.GetStringExtra(EXTRA_AUTH_STATE)));
            }
            catch (JSONException ex)
            {
                Console.WriteLine("Malformed AuthState JSON saved: " + ex);
                throw new InvalidOperationException("The AuthState instance is missing in the intent.", ex);
            }
        }
        public void PerformTokenRequest(string authStateJson)
        {
            AuthState = AuthState.JsonDeserialize(authStateJson);

            TokenRequest request = AuthState.LastAuthorizationResponse.CreateTokenExchangeRequest();

            //IClientAuthentication clientAuthentication;
            //try
            //{
            //	clientAuthentication = AuthState.ClientAuthentication;
            //}
            //catch (ClientAuthenticationUnsupportedAuthenticationMethod ex)
            //{
            //	Console.WriteLine("Token request cannot be made, client authentication for the token endpoint could not be constructed: " + ex);
            //	return;
            //}

            AuthService.PerformTokenRequest(request, ReceivedTokenResponse);
        }
Exemple #8
0
        public static bool TryGetAuthStateFromExtra(this Intent intent, out AuthState authState)
        {
            authState = null;

            if (!intent.HasAuthStateExtra())
            {
                return(false);
            }

            try
            {
                authState = AuthState.JsonDeserialize(intent.GetAuthStateExtra());
            }
            catch (JSONException)
            {
                return(false);
            }

            return(authState != null);
        }
        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());
            };
        }
Exemple #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();
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Auth.Credentials.OIDCCredential"/> class
 /// using the provided serialized form of the credential.
 /// </summary>
 /// <param name="serializedCredential">Serialized credential <see cref="SerializedCredential"/>.</param>
 internal OIDCCredential(string serializedCredential)
 {
     AuthState = AuthState.JsonDeserialize(serializedCredential);
 }