public async Task <(bool IsSucessful, string Error)> Login(Urls.OIDCUrls urls)
        {
            var configuration = new AuthorizationServiceConfiguration(
                ToUrl(urls.Authorization),
                ToUrl(urls.Token)
                );

            var authRequestBuilder = new AuthorizationRequest.Builder(
                configuration,
                AuthConstants.ClientId,
                ResponseTypeValues.Code,
                global::Android.Net.Uri.Parse(AuthConstants.RedirectUri)
                ).SetScope(AuthConstants.Scope);

            if (AuthConstants.Scope.Contains("offline_access"))
            {
                authRequestBuilder = authRequestBuilder.SetPrompt("consent");
            }
            var authRequest = authRequestBuilder.Build();

            MicroLogger.LogDebug("Making auth request to " + configuration.AuthorizationEndpoint);
#pragma warning disable IDE0059 // Unnecessary assignment of a value
            var intent = authService.GetAuthorizationRequestIntent(authRequest);
#pragma warning restore IDE0059 // Unnecessary assignment of a value

            taskCompletitionSource = new TaskCompletionSource <AuthState>();

            authService.PerformAuthorizationRequest(
                authRequest,
                AuthActivity.CreatePostAuthorizationIntent(
                    _context,
                    authRequest),
                authService.CreateCustomTabsIntentBuilder().Build()
                );

            var state = await taskCompletitionSource.Task;
            if (state.AuthorizationException != null)
            {
                return(false, state.AuthorizationException.ErrorDescription);
            }
            else
            {
                return(true, null);
            }
        }
Beispiel #2
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));
                    }
                });