Пример #1
0
 public Model.AccessKey ToModel()
 {
     Model.AccessKey result = new Model.AccessKey();
     result.Name = Name;
     result.Key = Key;
     result.Secret = Secret;
     return result;
 }
Пример #2
0
 public Model.AccessKey ToModel()
 {
     Model.AccessKey result = new Model.AccessKey();
     result.Name   = Name;
     result.Key    = Key;
     result.Secret = Secret;
     return(result);
 }
Пример #3
0
        public IActionResult RemoveAccessKey(string key)
        {
            IActionResult result;

            Model.AccessKey accessKey = BusinessLogicFactory.AccessKeys.GetAccessKey(key);
            if (accessKey == null)
            {
                result = new NotFoundResult();
            }
            else
            {
                BusinessLogicFactory.AccessKeys.SaveAccessKey(accessKey, Model.TObjectState.Delete);
                result = new NoContentResult();
            }
            return(result);
        }
Пример #4
0
        public IActionResult UpdateAccessKey(string key, [FromBody] ServiceModels.AccessKey accessKey)
        {
            IActionResult result;

            Model.AccessKey existingAccessKey = BusinessLogicFactory.AccessKeys.GetAccessKey(key);
            if (existingAccessKey == null)
            {
                result = new NotFoundResult();
            }
            else
            {
                existingAccessKey.Name = accessKey.Name;
                BusinessLogicFactory.AccessKeys.SaveAccessKey(existingAccessKey, Model.TObjectState.Update);
                result = new NoContentResult();
            }
            return(result);
        }
Пример #5
0
        public IActionResult GetAccessKey(string key)
        {
            IActionResult result;

            Model.AccessKey accessKey = BusinessLogicFactory.AccessKeys.GetAccessKey(key);
            if (accessKey == null)
            {
                result = new NotFoundResult();
            }
            else
            {
                ServiceModels.AccessKey response = new ServiceModels.AccessKey(accessKey);
                string rootUrl = Request.GetRootUrl();
                response.AddSelfLink(string.Concat(rootUrl, "/accesskeys/", response.Key), true, true);
                result = Request.GetObjectResult(response);
            }
            return(result);
        }
Пример #6
0
        public IActionResult AddAccessKey([FromBody] ServiceModels.AccessKey accessKey)
        {
            IActionResult result;

            if (accessKey == null)
            {
                result = new BadRequestResult();
            }
            else
            {
                Model.AccessKey item = accessKey.ToModel();
                item.OrganisationID = User.GetOrganisationID();
                BusinessLogicFactory.AccessKeys.SaveAccessKey(item, Model.TObjectState.Add);
                ServiceModels.AccessKey response = new ServiceModels.AccessKey(item);
                response.Secret = item.Secret;
                string rootUrl = Request.GetRootUrl();
                response.AddSelfLink(string.Concat(rootUrl, "/accesskeys/", response.Key), true, true);
                result = Request.GetObjectResult(response, HttpStatusCode.Created);
            }
            return(result);
        }
Пример #7
0
        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);
        }
Пример #8
0
 public AccessKey(Model.AccessKey item)
 {
     Name = item.Name;
     Key  = item.Key;
 }