aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/UserController.cs
blob: 45242ce306fa5bc073cc42d145c41d6ef54d34db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Timeline.Entities;
using Timeline.Services;

namespace Timeline.Controllers
{
    [Route("api/[controller]")]
    public class UserController : Controller
    {
        private static class LoggingEventIds
        {
            public const int LogInSucceeded = 4000;
            public const int LogInFailed = 4001;
        }

        public class UserCredentials
        {
            public string Username { get; set; }
            public string Password { get; set; }
        }

        public class CreateTokenResult
        {
            public string Token { get; set; }
            public UserInfo UserInfo { get; set; }
        }

        public class TokenValidationRequest
        {
            public string Token { get; set; }
        }

        private readonly IUserService _userService;
        private readonly IJwtService _jwtService;
        private readonly ILogger<UserController> _logger;

        public UserController(IUserService userService, IJwtService jwtService, ILogger<UserController> logger)
        {
            _userService = userService;
            _jwtService = jwtService;
            _logger = logger;
        }

        [HttpPost("[action]")]
        [AllowAnonymous]
        public ActionResult<CreateTokenResult> CreateToken([FromBody] UserCredentials credentials)
        {
            var user = _userService.Authenticate(credentials.Username, credentials.Password);

            if (user == null) {
                _logger.LogInformation(LoggingEventIds.LogInFailed, "Attemp to login with username: {} and password: {} failed.", credentials.Username, credentials.Password);
                return BadRequest();
            }

            _logger.LogInformation(LoggingEventIds.LogInSucceeded, "Login with username: {} succeeded.", credentials.Username);

            var result = new CreateTokenResult
            {
                Token = _jwtService.GenerateJwtToken(user),
                UserInfo = user.GetUserInfo()
            };

            return Ok(result);
        }

        [HttpPost("[action]")]
        [Consumes("text/plain")]
        [AllowAnonymous]
        public ActionResult<TokenValidationResult> ValidateToken([FromBody] string token)
        {
            var result = _jwtService.ValidateJwtToken(token);
            return Ok(result);
        }

        [HttpPost("[action]")]
        [Consumes("application/json")]
        [AllowAnonymous]
        public ActionResult<TokenValidationResult> ValidateToken([FromBody] TokenValidationRequest request)
        {
            var result = _jwtService.ValidateJwtToken(request.Token);
            return Ok(result);
        }
    }
}