Пример #1
0
        public void HttpUnauthorizedResult_InitializesStatusCode()
        {
            // Arrange & act
            var result = new UnauthorizedResult();

            // Assert
            Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode);
        }
        public void HttpUnauthorizedResult_InitializesStatusCode()
        {
            // Arrange & act
            var result = new UnauthorizedResult();

            // Assert
            Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode);
        }
        public IActionResult CreateAccessToken([FromForm]OAuthTokenRequest tokenRequest)
        {
            IActionResult result = null;

            if (string.Compare(tokenRequest.grant_type, "password", true) == 0)
            {
                Model.AccessKey accessKey = BusinessLogicFactory.AccessKeys.GetAccessKey(tokenRequest.username);
                if (accessKey != null)
                {
                    if (string.Compare(tokenRequest.password, accessKey.Secret) == 0)
                    {
                        OAuthToken token = CreateOAuthToken(accessKey.OrganisationID);
                        result = new ObjectResult(token) { StatusCode = (int)HttpStatusCode.Created };
                    }
                    else
                    {
                        _logger.LogDebug($"Incorrect Secret for Organisation {accessKey.OrganisationID} with access key: {accessKey.Name}");
                        result = new UnauthorizedResult();
                    }
                }
                else
                {
                    _logger.LogDebug($"No organisation with key: {tokenRequest.username}");
                    result = new UnauthorizedResult();
                }
            }
            else if (string.Compare(tokenRequest.grant_type, "refresh_token", true) == 0)
            {
                OrganisationSecurityTokenHandler handler = _AuthOptions.SecurityTokenValidators.OfType<OrganisationSecurityTokenHandler>().FirstOrDefault();
                JwtSecurityToken securityToken = handler.ReadJwtToken(tokenRequest.refresh_token);

                if (securityToken != null)
                {
                    Claim organisationClaim = securityToken.Claims.ToList().Find(c => c.Type.Equals(OrganisationIdentity.OrganisationClaim));
                    Claim refreshTokenClaim = securityToken.Claims.ToList().Find(c => c.Type.Equals(RefreshTokenClaim));

                    if (organisationClaim != null && refreshTokenClaim != null && refreshTokenClaim.Value.Equals(RefreshTokenExists))
                    {
                        int organisationID;
                        if (int.TryParse(organisationClaim.Value, out organisationID) && organisationID > 0)
                        {
                            OAuthToken token = CreateOAuthToken(organisationID);
                            result = new ObjectResult(token) { StatusCode = (int)HttpStatusCode.Created };
                        }
                        else
                        {
                            _logger.LogDebug($"Failed to parse organisationID in refresh token: {tokenRequest.refresh_token}");
                            result = new BadRequestResult();
                        }
                    }
                    else
                    {
                        _logger.LogDebug($"Refresh token does not have expected claims: {tokenRequest.refresh_token}");
                        result = new BadRequestResult();
                    }
                }
                else
                {
                    _logger.LogDebug($"Invalid refresh token: {tokenRequest.refresh_token}");
                    result = new BadRequestResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }

            return result;
        }