Exemplo n.º 1
0
        public async Task <object> Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (model.expires_on == null)
            {
                throw new ApiArgumentException("expires_on");
            }

            string   purpose   = DynamicHelper.Value(model.purpose) ?? string.Empty;
            DateTime?expiresOn = DynamicHelper.Value(model.expires_on) != String.Empty ? DynamicHelper.To <DateTime>(model.expires_on) : null;

            ApiToken token = _keyProvider.GenerateKey(purpose);

            token.Key.ExpiresOn = expiresOn;

            await _keyProvider.SaveKey(token.Key);

            //
            // Create response
            dynamic key = ApiKeyHelper.ToJsonModel(token);

            return(Created(ApiKeyHelper.GetLocation(key.id), key));
        }
Exemplo n.º 2
0
        public object Get(string id)
        {
            ApiKey key = _keyProvider.GetKey(id);

            if (key == null)
            {
                return(NotFound());
            }

            SetAntiForgeryTokens();

            return(ApiKeyHelper.ToJsonModel(key));
        }
Exemplo n.º 3
0
        public async Task <object> Get()
        {
            SetAntiForgeryTokens();

            IEnumerable <ApiKey> keys = await _keyProvider.GetAllKeys();

            // Set HTTP header for total count
            Context.Response.SetItemsCount(keys.Count());

            return(new {
                api_keys = keys.Select(k => ApiKeyHelper.ToJsonModel(k))
            });
        }
Exemplo n.º 4
0
        public async Task <object> Patch(string id, [FromBody] dynamic model)
        {
            ApiKey key = _keyProvider.GetKey(id);

            if (key == null)
            {
                return(NotFound());
            }

            ApiKeyHelper.Update(key, model);

            await _keyProvider.SaveKey(key);

            return(ApiKeyHelper.ToJsonModel(key));
        }
        public static dynamic ToJsonModel(ApiKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            dynamic obj = new {
                id         = key.Id,
                expires_on = (object)key.ExpiresOn ?? string.Empty,
                type       = key.TokenType,
                api_key    = ApiKeyHelper.ToJsonModelRef(key)
            };

            return(Core.Environment.Hal.Apply(Defines.AccessTokensResource.Guid, obj, true));
        }
        public static dynamic ToJsonModel(ApiToken token)
        {
            if (token.Key == null)
            {
                throw new ArgumentNullException(nameof(token.Key));
            }

            if (string.IsNullOrEmpty(token.Token))
            {
                throw new ArgumentNullException(nameof(token.Token));
            }

            dynamic obj = new {
                id         = token.Key.Id,
                created_on = DateTime.UtcNow,
                expires_on = (object)token.Key.ExpiresOn ?? string.Empty,
                value      = token.Token,
                type       = token.Key.TokenType,
                api_key    = ApiKeyHelper.ToJsonModelRef(token.Key)
            };

            return(Core.Environment.Hal.Apply(Defines.AccessTokensResource.Guid, obj, true));
        }
Exemplo n.º 7
0
        private void ConfigureApiKeys()
        {
            Environment.Host.RouteBuilder.MapWebApiRoute(Defines.ApiKeysResource.Guid, $"{Defines.APIKEYS_PATH}/{{id?}}", new { controller = "ApiKeys" });

            Environment.Hal.ProvideLink(Defines.ApiKeysResource.Guid, "self", t => new { href = ApiKeyHelper.GetLocation(t.id) });
        }