コード例 #1
0
        // Authorization code flow using OIDAuthState automatic code exchanges.
        public async Task <AuthInfo> LoginAsync()
        {
            //var issuer = new NSUrl(Constants.Issuer);
            var redirectUri = new NSUrl(Constants.RedirectUri);

            //Console.WriteLine($"Fetching configuration for issuer: {issuer}");

            try
            {
                // discovers endpoints
                var configuration =
                    await AuthorizationService.DiscoverServiceConfigurationForDiscoveryAsync(
                        new NSUrl(Constants.DiscoveryEndpoint));

                Console.WriteLine($"Got configuration: {configuration}");

                // builds authentication request
                var request = new AuthorizationRequest(configuration, Constants.ClientId,
                                                       new string[] { Scope.OpenId, Scope.Profile, "offline_access" }, redirectUri, ResponseType.Code, null);
                // performs authentication request
                var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
                Console.WriteLine($"Initiating authorization request with scope: {request.Scope}");

                appDelegate.CurrentAuthorizationFlow = AuthState.PresentAuthorizationRequest(request,
                                                                                             UIKit.UIApplication.SharedApplication.KeyWindow.RootViewController, (authState, error) =>
                {
                    if (authState != null)
                    {
                        _authState = authState;
                        Console.WriteLine(
                            $"Got authorization tokens. Access token: {authState.LastTokenResponse.AccessToken}");
                    }
                    else
                    {
                        Console.WriteLine($"Authorization error: {error.LocalizedDescription}");
                        _authState = null;
                    }
                    //We need this line to tell the Login method to return the result
                    _loginResultWaitHandle.Set();
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error retrieving discovery document: {ex}");
                _authState = null;
                //We need this line to tell the Login method to return the result
                _loginResultWaitHandle.Set();
            }

            await _loginResultWaitHandle.WaitAsync();

            return(new AuthInfo()
            {
                IsAuthorized = _authState?.IsAuthorized ?? false,
                AccessToken = _authState?.LastTokenResponse?.AccessToken,
                IdToken = _authState?.LastTokenResponse?.IdToken,
                RefreshToken = _authState?.LastTokenResponse?.RefreshToken,
                Scope = _authState?.LastTokenResponse?.Scope
            });
        }
コード例 #2
0
 /// <summary>
 /// Sends a request to the Keycloak server to perform token exchange.
 /// On successfully completing the token exchange the callback is invoked with the `openid` credentials for the user.
 /// Otherwise the callback is invoked with the error that occured during token exchange.
 /// </summary>
 /// <returns>The authorization flow.</returns>
 /// <param name="request">an openid authorisation request.</param>
 /// <param name="presentingViewController">The view controller from which to present the SafariViewController.</param>
 /// <param name="callback">a callback function that will be invoked when the token exchange is completed.</param>
 private IAuthorizationFlowSession startAuthorizationFlow(AuthorizationRequest request, UIViewController presentingViewController, OIDAuthFlowCallback callback)
 {
     return(AuthState.PresentAuthorizationRequest(request, presentingViewController, (authState, error) =>
     {
         if (authState == null || error != null)
         {
             callback(null, error);
         }
         else
         {
             callback(new OIDCCredential(authState), null);
         }
     }));
 }
コード例 #3
0
        public async Task <(bool, string)> AuthWithAutoCodeExchange(Urls.OIDCUrls urls)
        {
            MicroLogger.LogDebug(nameof(AuthWithAutoCodeExchange));
            var redirectURI = new NSUrl(AuthConstants.RedirectUri);

            try
            {
                // discovers endpoints
                var configuration = new ServiceConfiguration(ToUrl(urls.Authorization), ToUrl(urls.Token));

                MicroLogger.LogDebug($"Got configuration: {configuration}");

                // builds authentication request
                var request = new AuthorizationRequest(configuration, AuthConstants.ClientId, AuthConstants.ClientSecret, AuthConstants.ScopesArray, redirectURI, ResponseType.Code, null);
                // performs authentication request
                var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
                MicroLogger.LogDebug($"Initiating authorization request with scope: {request.Scope}");

                var tcl = new TaskCompletionSource <(bool, string)>();

                appDelegate.CurrentAuthorizationFlow = AuthState
                                                       .PresentAuthorizationRequest(request, appDelegate.Window.RootViewController, (authState, error) =>
                {
                    MicroLogger.LogDebug(nameof(AuthState.PresentAuthorizationRequest) + "Done");
                    if (authState != null)
                    {
                        AuthService.SaveState(authState);
                        MicroLogger.LogDebug($"Got authorization tokens. Access token: {authState.LastTokenResponse.AccessToken}");
                        tcl.SetResult((true, null));
                    }
                    else
                    {
                        MicroLogger.LogError($"Authorization error: {error.LocalizedDescription}");
                        AuthService.ClearState();
                        tcl.SetResult((false, error.LocalizedDescription));
                    }
                });
コード例 #4
0
        public async Task <AuthInfo> LoginAsync()
        {
            var redirectUri = new NSUrl(Constants.RedirectUri);

            try
            {
                var configuration = await AuthorizationService
                                    .DiscoverServiceConfigurationForDiscoveryAsync(
                    new NSUrl(Constants.DiscoveryEndpoint));

                var keys = new[]
                {
                    new NSString("prompt")
                };

                var values = new NSString[] { new NSString("login") };



                var param = new NSDictionary <NSString, NSString>(keys, values);



                var request = new AuthorizationRequest(
                    configuration, Constants.ClientId,
                    Constants.Scopes, redirectUri, ResponseType.Code, param);

                // Performs authentication request
                var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
                appDelegate.CurrentAuthorizationFlow = AuthState.PresentAuthorizationRequest(request,
                                                                                             UIKit.UIApplication.SharedApplication.KeyWindow.RootViewController, (authState, error) =>
                {
                    if (authState != null)
                    {
                        _authState = authState;
                    }
                    else
                    {
                        Console.WriteLine($"Authorization error: {error.LocalizedDescription}");
                        _authState = null;
                    }

                    _loginResultWaitHandle.Set();
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error retrieving discovery document: {ex}");
                _authState = null;
                _loginResultWaitHandle.Set();
            }

            await _loginResultWaitHandle.WaitAsync();

            return(new AuthInfo()
            {
                IsAuthorized = _authState?.IsAuthorized ?? false,
                AccessToken = _authState?.LastTokenResponse?.AccessToken,
                IdToken = _authState?.LastTokenResponse?.IdToken,
                RefreshToken = _authState?.LastTokenResponse?.RefreshToken,
                Scope = _authState?.LastTokenResponse?.Scope
            });
        }