Ejemplo n.º 1
0
        private TokenInfoDto GenerateTokenForAuthorizationCodeGrant(AskTokenDto tokenInfo, IStringLocalizer errorLocal)
        {
            TokenInfoDto toReturn = null;

            if (String.IsNullOrWhiteSpace(tokenInfo.ClientPublicId))
            {
                throw new DaOAuthTokenException()
                      {
                          Error       = OAuthConvention.ErrorNameInvalidRequest,
                          Description = errorLocal["ClientIdParameterError"]
                      };
            }

            using (var context = RepositoriesFactory.CreateContext())
            {
                var clientRepo = RepositoriesFactory.GetClientRepository(context);
                var myClient   = clientRepo.GetByPublicId(tokenInfo.ClientPublicId);

                if (!CheckIfClientsCredentialsAreValid(myClient, tokenInfo.AuthorizationHeader))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameUnauthorizedClient,
                              Description = errorLocal["UnauthorizedClient"]
                          };
                }

                if (String.IsNullOrWhiteSpace(tokenInfo.CodeValue))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidRequest,
                              Description = errorLocal["CodeParameterError"]
                          };
                }

                if (String.IsNullOrWhiteSpace(tokenInfo.RedirectUrl) || !Uri.TryCreate(tokenInfo.RedirectUrl, UriKind.Absolute, out var myUri))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidRequest,
                              Description = errorLocal["ReturnUrlParameterError"]
                          };
                }

                if (!CheckIfClientValidForToken(myClient, tokenInfo.RedirectUrl, OAuthConvention.ResponseTypeCode))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidClient,
                              Description = errorLocal["AskTokenInvalidClient"]
                          };
                }

                if (!CheckIfCodeIsValid(tokenInfo.ClientPublicId, tokenInfo.Scope, tokenInfo.CodeValue, context, out var userName))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidGrant,
                              Description = errorLocal["AskTokenInvalidGrant"]
                          };
                }

                toReturn = GenerateAccessTokenAndUpdateRefreshToken(tokenInfo, context, userName);

                context.Commit();
            }

            return(toReturn);
        }