Esempio n. 1
0
        private async Task <bool> ExecuteClientCredentialsFlow(IOwinEnvironment context, IClient client, CancellationToken cancellationToken)
        {
            var basicHeaderParser = new BasicAuthenticationParser(context.Request.Headers.GetString("Authorization"), _logger);

            if (!basicHeaderParser.IsValid)
            {
                await Error.Create <OauthInvalidRequest>(context, cancellationToken);

                return(true);
            }

            var preLoginContext = new PreLoginContext(context)
            {
                Login = basicHeaderParser.Username
            };
            await _handlers.PreLoginHandler(preLoginContext, cancellationToken).ConfigureAwait(false);

            var request = new ClientCredentialsGrantRequest
            {
                Id     = basicHeaderParser.Username,
                Secret = basicHeaderParser.Password
            };

            if (preLoginContext.AccountStore != null)
            {
                request.AccountStoreHref = preLoginContext.AccountStore.Href;
            }

            var application = await client
                              .GetApplicationAsync(_configuration.Application.Href, cancellationToken)
                              .ConfigureAwait(false);

            IOauthGrantAuthenticationResult tokenResult;

            try
            {
                tokenResult = await application
                              .ExecuteOauthRequestAsync(request, cancellationToken)
                              .ConfigureAwait(false);
            }
            // Catch error 10019 (API Authentication failed)
            catch (ResourceException rex) when(rex.Code == 10019)
            {
                return(await Error.Create <OauthInvalidClient>(context, cancellationToken).ConfigureAwait(false));
            }

            var accessToken = await tokenResult.GetAccessTokenAsync(cancellationToken).ConfigureAwait(false);

            var account = await accessToken.GetAccountAsync(cancellationToken).ConfigureAwait(false);

            var postLoginContext = new PostLoginContext(context, account);
            await _handlers.PostLoginHandler(postLoginContext, cancellationToken).ConfigureAwait(false);

            var sanitizer = new GrantResultResponseSanitizer();

            return(await JsonResponse.Ok(context, sanitizer.SanitizeResponseWithoutRefreshToken(tokenResult)).ConfigureAwait(false));
        }
        public async Task <IOauthGrantAuthenticationResult> ClientCredentialsGrantAsync(
            IOwinEnvironment environment,
            IApplication application,
            Func <AbstractError, CancellationToken, Task> errorHandler,
            string id,
            string secret,
            CancellationToken cancellationToken)
        {
            var preLoginHandlerContext = new PreLoginContext(environment)
            {
                Login = id
            };

            await _handlers.PreLoginHandler(preLoginHandlerContext, cancellationToken);

            if (preLoginHandlerContext.Result != null)
            {
                if (!preLoginHandlerContext.Result.Success)
                {
                    var message = string.IsNullOrEmpty(preLoginHandlerContext.Result.ErrorMessage)
                        ? "An error has occurred. Please try again."
                        : preLoginHandlerContext.Result.ErrorMessage;
                    await errorHandler(new BadRequest(message), cancellationToken);

                    return(null);
                }
            }

            var request = new ClientCredentialsGrantRequest
            {
                Id     = id,
                Secret = secret
            };

            if (preLoginHandlerContext.AccountStore != null)
            {
                request.AccountStoreHref = preLoginHandlerContext.AccountStore.Href;
            }

            if (!string.IsNullOrEmpty(preLoginHandlerContext.OrganizationNameKey))
            {
                request.OrganizationNameKey = preLoginHandlerContext.OrganizationNameKey;
            }

            IOauthGrantAuthenticationResult tokenResult;

            try
            {
                tokenResult = await application
                              .ExecuteOauthRequestAsync(request, cancellationToken)
                              .ConfigureAwait(false);
            }
            // Catch error 10019 (API Authentication failed)
            catch (ResourceException rex) when(rex.Code == 10019)
            {
                await errorHandler(new OauthInvalidClient(), cancellationToken);

                return(null);
            }

            return(tokenResult);
        }