Example #1
0
        public async Task <TokenResponse> PostToken()
        {
            var certificate = GetCertificate();

            if (Request.Form == null)
            {
                throw new ArgumentNullException(nameof(Request.Form));
            }

            var          serializer   = new ParamSerializer();
            var          tokenRequest = serializer.Deserialize <TokenRequest>(Request.Form);
            GrantedToken result       = null;
            StringValues authorizationHeader;
            AuthenticationHeaderValue authenticationHeaderValue = null;

            if (Request.Headers.TryGetValue("Authorization", out authorizationHeader))
            {
                var authorizationHeaderValue         = authorizationHeader.First();
                var splittedAuthorizationHeaderValue = authorizationHeaderValue.Split(' ');
                if (splittedAuthorizationHeaderValue.Count() == 2)
                {
                    authenticationHeaderValue = new AuthenticationHeaderValue(
                        splittedAuthorizationHeaderValue[0],
                        splittedAuthorizationHeaderValue[1]);
                }
            }

            switch (tokenRequest.GrantType)
            {
            case GrantTypes.password:
                var resourceOwnerParameter = tokenRequest.ToResourceOwnerGrantTypeParameter();
                result = await _tokenActions.GetTokenByResourceOwnerCredentialsGrantType(resourceOwnerParameter, authenticationHeaderValue, certificate);

                break;

            case GrantTypes.authorization_code:
                var authCodeParameter = tokenRequest.ToAuthorizationCodeGrantTypeParameter();
                result = await _tokenActions.GetTokenByAuthorizationCodeGrantType(
                    authCodeParameter,
                    authenticationHeaderValue);

                break;

            case GrantTypes.refresh_token:
                var refreshTokenParameter = tokenRequest.ToRefreshTokenGrantTypeParameter();
                result = await _tokenActions.GetTokenByRefreshTokenGrantType(refreshTokenParameter);

                break;

            case GrantTypes.client_credentials:
                var clientCredentialsParameter = tokenRequest.ToClientCredentialsGrantTypeParameter();
                result = await _tokenActions.GetTokenByClientCredentialsGrantType(clientCredentialsParameter, authenticationHeaderValue);

                break;
            }

            return(result.ToDto());
        }
        public async Task <IActionResult> PostToken()
        {
            var certificate = GetCertificate();

            try
            {
                if (Request.Form == null)
                {
                    return(BuildError(ErrorCodes.InvalidRequestCode, "no parameter in body request", HttpStatusCode.BadRequest));
                }
            }
            catch (Exception)
            {
                return(BuildError(ErrorCodes.InvalidRequestCode, "no parameter in body request", HttpStatusCode.BadRequest));
            }

            var          serializer   = new ParamSerializer();
            var          tokenRequest = serializer.Deserialize <TokenRequest>(Request.Form);
            GrantedToken result       = null;
            StringValues authorizationHeader;
            AuthenticationHeaderValue authenticationHeaderValue = null;

            if (Request.Headers.TryGetValue("Authorization", out authorizationHeader))
            {
                var authorizationHeaderValue         = authorizationHeader.First();
                var splittedAuthorizationHeaderValue = authorizationHeaderValue.Split(' ');
                if (splittedAuthorizationHeaderValue.Count() == 2)
                {
                    authenticationHeaderValue = new AuthenticationHeaderValue(
                        splittedAuthorizationHeaderValue[0],
                        splittedAuthorizationHeaderValue[1]);
                }
            }

            var issuerName = Request.GetAbsoluteUriWithVirtualPath();

            switch (tokenRequest.GrantType)
            {
            case GrantTypes.password:
                var resourceOwnerParameter = tokenRequest.ToResourceOwnerGrantTypeParameter();
                result = await _tokenActions.GetTokenByResourceOwnerCredentialsGrantType(resourceOwnerParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.authorization_code:
                var authCodeParameter = tokenRequest.ToAuthorizationCodeGrantTypeParameter();
                result = await _tokenActions.GetTokenByAuthorizationCodeGrantType(authCodeParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.refresh_token:
                var refreshTokenParameter = tokenRequest.ToRefreshTokenGrantTypeParameter();
                result = await _tokenActions.GetTokenByRefreshTokenGrantType(refreshTokenParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.client_credentials:
                var clientCredentialsParameter = tokenRequest.ToClientCredentialsGrantTypeParameter();
                result = await _tokenActions.GetTokenByClientCredentialsGrantType(clientCredentialsParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.uma_ticket:
                var tokenIdParameter = tokenRequest.ToTokenIdGrantTypeParameter();
                result = await _umaTokenActions.GetTokenByTicketId(tokenIdParameter, authenticationHeaderValue, certificate, issuerName);

                break;
            }

            return(new OkObjectResult(result.ToDto()));
        }
Example #3
0
        public async Task <IActionResult> PostToken()
        {
            var certificate = GetCertificate();

            try
            {
                if (Request.Form == null)
                {
                    return(BuildError(ErrorCodes.InvalidRequestCode, "no parameter in body request", HttpStatusCode.BadRequest));
                }
            }
            catch (Exception)
            {
                return(BuildError(ErrorCodes.InvalidRequestCode, "no parameter in body request", HttpStatusCode.BadRequest));
            }

            var          serializer   = new ParamSerializer();
            var          tokenRequest = serializer.Deserialize <TokenRequest>(Request.Form);
            GrantedToken result       = null;
            StringValues authorizationHeader;
            AuthenticationHeaderValue authenticationHeaderValue = null;

            if (Request.Headers.TryGetValue("Authorization", out authorizationHeader))
            {
                var authorizationHeaderValue         = authorizationHeader.First();
                var splittedAuthorizationHeaderValue = authorizationHeaderValue.Split(' ');
                if (splittedAuthorizationHeaderValue.Count() == 2)
                {
                    authenticationHeaderValue = new AuthenticationHeaderValue(
                        splittedAuthorizationHeaderValue[0],
                        splittedAuthorizationHeaderValue[1]);
                }
            }

            var issuerName = Request.GetAbsoluteUriWithVirtualPath();

            switch (tokenRequest.GrantType)
            {
            case GrantTypes.password:
                var resourceOwnerParameter = tokenRequest.ToResourceOwnerGrantTypeParameter();
                result = await _tokenActions.GetTokenByResourceOwnerCredentialsGrantType(resourceOwnerParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.authorization_code:
                var authCodeParameter = tokenRequest.ToAuthorizationCodeGrantTypeParameter();
                result = await _tokenActions.GetTokenByAuthorizationCodeGrantType(authCodeParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.refresh_token:
                var refreshTokenParameter = tokenRequest.ToRefreshTokenGrantTypeParameter();
                result = await _tokenActions.GetTokenByRefreshTokenGrantType(refreshTokenParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.client_credentials:
                var clientCredentialsParameter = tokenRequest.ToClientCredentialsGrantTypeParameter();
                result = await _tokenActions.GetTokenByClientCredentialsGrantType(clientCredentialsParameter, authenticationHeaderValue, certificate, issuerName);

                break;

            case GrantTypes.uma_ticket:
                var tokenIdParameter           = tokenRequest.ToTokenIdGrantTypeParameter();
                var getTokenByTicketIdResponse = await _umaTokenActions.GetTokenByTicketId(tokenIdParameter, _authorizationServerOptions.OpenidWellKnownConfiguration, issuerName);

                if (!getTokenByTicketIdResponse.IsValid)
                {
                    var errorResponse = new ErrorResponse
                    {
                        Error = Errors.ErrorCodes.NotAuthorized
                    };

                    var errorDetails = new List <object>();
                    foreach (var policyResult in getTokenByTicketIdResponse.ResourceValidationResult.AuthorizationPoliciesResult)
                    {
                        var dic = new Dictionary <string, object>
                        {
                            { "status", policyResult.Type.ToString() },
                            { "details", policyResult.ErrorDetails }
                        };
                        if (policyResult.Policy != null)
                        {
                            dic.Add("policy_id", policyResult.Policy.Id);
                        }

                        errorDetails.Add(dic);
                    }

                    errorResponse.ErrorDetails = errorDetails;
                    return(new JsonResult(errorResponse)
                    {
                        StatusCode = (int)HttpStatusCode.InternalServerError
                    });
                }

                result = getTokenByTicketIdResponse.GrantedToken;
                break;
            }

            return(new OkObjectResult(result.ToDto()));
        }