Ejemplo n.º 1
0
        public async Task <ApiEntityPage <TokenJsonEntity> > Login([FromBody] LoginModel credentials)
        {
            // Here should be proper auth via DB with creation of token if auth succeeds

            User user = _userService.GetUser(credentials.Email);

            if (user == null)
            {
                throw new UnauthorizedException("Provided credentials are not valid");
            }

            if (!user.MatchPassword(credentials.Password))
            {
                throw new UnauthorizedException("Provided credentials are not valid");
            }

            UserToken userToken = await _userService.GetNewTokenAsync(user);

            TokenJsonEntity tokenJson = new TokenJsonEntity()
            {
                Token   = userToken.Token,
                Expires = userToken.TokenExpirationDate.Ticks
            };

            ApiEntityPage <TokenJsonEntity> result = new ApiEntityPage <TokenJsonEntity>(tokenJson,
                                                                                         HttpContext.Request.Path.ToString());

            return(result);
        }
Ejemplo n.º 2
0
        public ApiEntityPage <UserApiKeyJsonEntity> CreateApiKey([FromBody] IDictionary <string, string> requestBody)
        {
            // In general it is not a good idea to parse JSON object into the dictionary,
            // but here we need to have only on field with description, so there is
            // no need to create an object. Maybe in the fitire when we decide to have
            // mode fields here - we will convert this to a normal input object.
            string keyDescription = requestBody.ContainsKey("description") ?
                                    requestBody["description"] :
                                    "not set";

            UserApiKey apiKey = _userService.CreateUserApiKey(UserId, keyDescription);

            // Convreting to fancy JSON
            ApiEntityPage <UserApiKeyJsonEntity> result =
                new ApiEntityPage <UserApiKeyJsonEntity>(new UserApiKeyJsonEntity(apiKey), Request.Path.ToString());

            Response.StatusCode = StatusCodes.Status201Created;

            return(result);
        }