Browse Source

users add/get/delete/login/logout/me

tags/v0.6.0
Robin Thoni 7 years ago
parent
commit
8310abfd0d
39 changed files with 884 additions and 124 deletions
  1. 52
    1
      Luticate2.Auth/Business/LuBusinessExtensions.cs
  2. 88
    0
      Luticate2.Auth/Business/LuTokensBusiness.cs
  3. 119
    21
      Luticate2.Auth/Business/LuUsersBusiness.cs
  4. 27
    13
      Luticate2.Auth/Controllers/LuAuthExtensions.cs
  5. 1
    1
      Luticate2.Auth/Controllers/LuLoggedUserAccessor.cs
  6. 90
    3
      Luticate2.Auth/Controllers/LuUsersController.cs
  7. 13
    0
      Luticate2.Auth/DataAccess/LuAuthDatabaseContext.cs
  8. 57
    0
      Luticate2.Auth/DataAccess/LuTokensDataAccess.cs
  9. 73
    0
      Luticate2.Auth/DataAccess/LuUsersDataAccess.cs
  10. 0
    21
      Luticate2.Auth/DataAccess/Models/ModelsToDbo.cs
  11. 2
    0
      Luticate2.Auth/DataAccess/Models/lu_authentication_sources.cs
  12. 26
    0
      Luticate2.Auth/DataAccess/Models/lu_tokens.cs
  13. 2
    0
      Luticate2.Auth/DataAccess/Models/lu_users.cs
  14. 59
    0
      Luticate2.Auth/DataAccess/ModelsToDbo.cs
  15. 29
    0
      Luticate2.Auth/DataAccess/code-from-ds/code-from-ds.json
  16. 4
    0
      Luticate2.Auth/Dbo/Permissions/LuPermissions.cs
  17. 18
    0
      Luticate2.Auth/Dbo/Tokens/LuTokensAddDbo.cs
  18. 6
    0
      Luticate2.Auth/Dbo/Tokens/LuTokensDbo.cs
  19. 7
    0
      Luticate2.Auth/Dbo/Tokens/LuTokensEditDbo.cs
  20. 9
    0
      Luticate2.Auth/Dbo/Users/LuUsersAddDbo.cs
  21. 7
    0
      Luticate2.Auth/Dbo/Users/LuUsersAddFullDbo.cs
  22. 1
    1
      Luticate2.Auth/Dbo/Users/LuUsersDbo.cs
  23. 7
    0
      Luticate2.Auth/Dbo/Users/LuUsersEditDbo.cs
  24. 15
    0
      Luticate2.Auth/Dbo/Users/LuUsersEditFullDbo.cs
  25. 7
    0
      Luticate2.Auth/Dbo/Users/LuUsersFullDbo.cs
  26. 9
    0
      Luticate2.Auth/Dbo/Users/LuUsersLoginResultDbo.cs
  27. 3
    2
      Luticate2.Auth/Dbo/Users/LuUsersToken.cs
  28. 18
    0
      Luticate2.Auth/Interfaces/Tokens/ILuTokensBusiness.cs
  29. 1
    1
      Luticate2.Auth/Interfaces/Users/ILuLoggedUserAccessor.cs
  30. 13
    8
      Luticate2.Auth/Interfaces/Users/ILuUsersBusiness.cs
  31. 11
    10
      Luticate2.Auth/Middlewares/LuLoggedUserMiddleware.cs
  32. 15
    14
      Luticate2.Auth/project.json
  33. 6
    3
      Luticate2.Utils/Controllers/LuUtilsExtensions.cs
  34. 9
    0
      Luticate2.Utils/Interfaces/IDateTime.cs
  35. 10
    0
      Luticate2.Utils/Utils/SystemDateTime.cs
  36. 22
    0
      TestAuth/Business/LuUsersBusinessTest.cs
  37. 21
    0
      TestAuth/Tests.cs
  38. 1
    0
      TestAuth/project.json
  39. 26
    25
      TestUtils/project.json

+ 52
- 1
Luticate2.Auth/Business/LuBusinessExtensions.cs View File

@@ -1,6 +1,57 @@
1
-namespace Luticate2.Auth.Business
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using Luticate2.Auth.Dbo.Tokens;
5
+using Luticate2.Auth.Dbo.Users;
6
+using Luticate2.Utils.Dbo.Basic;
7
+using Luticate2.Utils.Dbo.Result;
8
+using Luticate2.Utils.Utils;
9
+
10
+namespace Luticate2.Auth.Business
2 11
 {
3 12
     public static class LuBusinessExtensions
4 13
     {
14
+        public static IEnumerable<LuUsersDbo> ToLite(this IEnumerable<LuUsersFullDbo> userRes)
15
+        {
16
+            return userRes.Select(fullDbo => fullDbo.ToLite());
17
+        }
18
+
19
+        public static LuResult<LuUsersDbo> ToLite(this LuResult<LuUsersFullDbo> userRes)
20
+        {
21
+            return userRes.To(dbo => dbo.ToLite());
22
+        }
23
+
24
+        public static LuResult<LuPaginatedDbo<LuUsersDbo>> ToLite(this LuResult<LuPaginatedDbo<LuUsersFullDbo>> userRes)
25
+        {
26
+            return userRes.To(dbo => dbo.To(dbos => dbos.ToLite().ToList()));
27
+        }
28
+
29
+        public static LuUsersDbo ToLite(this LuUsersFullDbo dbo)
30
+        {
31
+            if (dbo == null)
32
+            {
33
+                return null;
34
+            }
35
+            return new LuUsersDbo
36
+            {
37
+                Id = dbo.Id,
38
+                Username = dbo.Username
39
+            };//TODO
40
+        }
41
+
42
+        public static LuUsersToken ToUserToken(this LuTokensDbo dbo)
43
+        {
44
+            if (dbo == null)
45
+            {
46
+                return null;
47
+            }
48
+            return new LuUsersToken
49
+            {
50
+                Data = dbo.Data,
51
+                NotAfter = dbo.NotAfter,
52
+                NotBefore = dbo.NotBefore,
53
+                UserId = dbo.UserId
54
+            };
55
+        }
5 56
     }
6 57
 }

+ 88
- 0
Luticate2.Auth/Business/LuTokensBusiness.cs View File

@@ -0,0 +1,88 @@
1
+using System;
2
+using System.Security.Cryptography;
3
+using Luticate2.Auth.DataAccess;
4
+using Luticate2.Auth.Dbo.Tokens;
5
+using Luticate2.Auth.Dbo.Users;
6
+using Luticate2.Auth.Interfaces.Tokens;
7
+using Luticate2.Utils.Business;
8
+using Luticate2.Utils.Dbo.Result;
9
+using Luticate2.Utils.Interfaces;
10
+using Luticate2.Utils.Utils;
11
+
12
+namespace Luticate2.Auth.Business
13
+{
14
+    public class LuTokensBusiness : LuCrudBusiness<LuTokensDataAccess, LuTokensAddDbo, LuTokensDbo, LuTokensEditDbo, string>, ILuTokensBusiness
15
+    {
16
+        private readonly IDateTime _dateTime;
17
+
18
+        public LuTokensBusiness(LuTokensDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness, IDateTime dateTime) : base(dataAccess, notificationsBusiness)
19
+        {
20
+            _dateTime = dateTime;
21
+        }
22
+
23
+        public LuResult<LuUsersToken> GetToken(string token)
24
+        {
25
+            return GetSingleById(token).To(dbo => dbo.ToUserToken());
26
+        }
27
+
28
+        public string GenerateId()
29
+        {
30
+            var token = new byte[50];
31
+            using (var rng = RandomNumberGenerator.Create())
32
+            {
33
+                rng.GetBytes(token);
34
+            }
35
+            return Convert.ToBase64String(token).Trim('=');
36
+        }
37
+
38
+        public LuResult<string> RegisterToken(LuUsersToken token)
39
+        {
40
+            string id;
41
+            LuResult<LuTokensDbo> tokenRes;
42
+            do
43
+            {
44
+                id = GenerateId();
45
+                tokenRes = GetSingleById(id);
46
+            } while (tokenRes);
47
+
48
+            if (tokenRes.Status != LuStatus.NotFound)
49
+            {
50
+                return tokenRes.To<string>();
51
+            }
52
+
53
+            return this.AddId(new LuTokensAddDbo
54
+            {
55
+                Data = token.Data,
56
+                Id = id,
57
+                NotAfter = token.NotAfter,
58
+                NotBefore = token.NotBefore,
59
+                UserId = token.UserId
60
+            });
61
+        }
62
+
63
+        public LuResult<LuUsersToken> UnRegisterToken(string token)
64
+        {
65
+            return this.DeleteSingleByIdDbo(token).To(dbo => dbo.ToUserToken());
66
+        }
67
+
68
+        public bool IsTokenValid(LuUsersToken token)
69
+        {
70
+            var now = _dateTime.Now;
71
+            return (token.NotBefore == null || now >= token.NotBefore) &&
72
+                   (token.NotAfter == null || now <= token.NotAfter);
73
+        }
74
+
75
+        public LuResult<string> GenerateToken(LuUsersDbo user)
76
+        {
77
+            var token = new LuUsersToken
78
+            {
79
+                Data = null,//TODO
80
+                NotAfter = null,
81
+                NotBefore = null,
82
+                UserId = user.Id
83
+            };
84
+
85
+            return RegisterToken(token);
86
+        }
87
+    }
88
+}

+ 119
- 21
Luticate2.Auth/Business/LuUsersBusiness.cs View File

@@ -1,54 +1,152 @@
1 1
 using System;
2
+using System.Collections.Generic;
3
+using System.Security.Cryptography;
4
+using System.Text;
5
+using Luticate2.Auth.DataAccess;
2 6
 using Luticate2.Auth.Dbo.Users;
7
+using Luticate2.Auth.Interfaces.Tokens;
3 8
 using Luticate2.Auth.Interfaces.Users;
9
+using Luticate2.Utils.Business;
4 10
 using Luticate2.Utils.Dbo.Result;
11
+using Luticate2.Utils.Interfaces;
5 12
 using Luticate2.Utils.Utils;
13
+using Microsoft.AspNetCore.Cryptography.KeyDerivation;
6 14
 
7 15
 namespace Luticate2.Auth.Business
8 16
 {
9
-    public class LuUsersBusiness : ILuUsersBusiness
17
+    public class LuUsersBusiness : LuCrudBusiness<LuUsersDataAccess, LuUsersAddFullDbo, LuUsersFullDbo, LuUsersEditFullDbo, string>, ILuUsersBusiness
10 18
     {
11
-        public LuResult<UsersToken> GetToken(string token)
19
+        private readonly ILuTokensBusiness _luTokensBusiness;
20
+        private readonly ILuLoggedUserAccessor _luLoggedUserAccessor;
21
+
22
+        public LuUsersBusiness(LuUsersDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness,
23
+            ILuTokensBusiness luTokensBusiness, ILuLoggedUserAccessor luLoggedUserAccessor) : base(dataAccess, notificationsBusiness)
24
+        {
25
+            _luTokensBusiness = luTokensBusiness;
26
+            _luLoggedUserAccessor = luLoggedUserAccessor;
27
+        }
28
+
29
+        public string GenerateSalt()
12 30
         {
13
-            var id = Guid.NewGuid().ToDbo();
14
-            return LuResult<UsersToken>.Ok(new UsersToken
31
+            var salt = new byte[128 / 8];
32
+            using (var rng = RandomNumberGenerator.Create())
15 33
             {
16
-                UserId = id
17
-            });//TODO
34
+                rng.GetBytes(salt);
35
+            }
36
+            return Convert.ToBase64String(salt);
18 37
         }
19 38
 
20
-        public LuResult<string> RegisterToken(UsersToken token)
39
+        public string HashPassword(string password, string salt)
21 40
         {
22
-            return LuResult<string>.Ok("token");//TODO
41
+            var hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(password, Encoding.ASCII.GetBytes(salt),
42
+                KeyDerivationPrf.HMACSHA1, 10000, 256 / 8));
43
+            return hashed;
23 44
         }
24 45
 
25
-        public LuResult<UsersToken> UnRegisterToken(string token)
46
+        public bool VerifyPasswordHash(string password, string hash, string salt)
26 47
         {
27
-            return LuResult<UsersToken>.Ok(new UsersToken());//TODO
48
+            var newHash = HashPassword(password, salt);
49
+            return newHash == hash;
28 50
         }
29 51
 
30
-        public bool IsTokenValid(UsersToken token)
52
+        public LuResult<LuUsersFullDbo> FindByUsername(string username)
31 53
         {
32
-            return true;
54
+            return DataAccess.FindByUsername(username);
33 55
         }
34 56
 
35
-        public LuResult<UsersDbo> GetSingleById(string id)
57
+        public LuResult<LuUsersFullDbo> Register(LuUsersAddDbo user)
36 58
         {
37
-            return LuResult<UsersDbo>.Ok(new UsersDbo
59
+            var userRes = FindByUsername(user.Username);
60
+            if (userRes)
61
+            {
62
+                return LuResult<LuUsersFullDbo>.Error(LuStatus.InputError,
63
+                    $"username: {user.Username}", "Username already exists");
64
+            }
65
+            if (userRes.Status != LuStatus.NotFound)
66
+            {
67
+                return userRes;
68
+            }
69
+            var salt = GenerateSalt();
70
+            var password = HashPassword(user.Password, salt);
71
+            return this.AddDbo(new LuUsersAddFullDbo
38 72
             {
39
-                Id = id,
40
-                Username = "user-" + id.Split('-')[0]
41
-            });//TODO
73
+                AuthenticationSourceId = Guid.Empty.ToDbo(),
74
+                Data = null,
75
+                Password = password,
76
+                Salt = salt,
77
+                Username = user.Username
78
+            });
42 79
         }
43 80
 
44
-        public string HashPassword(string password, string salt)
81
+        public LuResult<LuUsersFullDbo> Edit(string id, LuUsersEditDbo user)
45 82
         {
46
-            return "";//TODO
83
+            throw new NotImplementedException();
47 84
         }
48 85
 
49
-        public bool VerifyPasswordHash(string password, string hash, string salt)
86
+        public LuResult<LuUsersLoginResultDbo> Login(string username, string password)
87
+        {
88
+            var userRes = FindByUsername(username);
89
+            if (userRes.Status == LuStatus.NotFound)
90
+            {
91
+                return LuResult<LuUsersLoginResultDbo>.Error(LuStatus.LoginError,
92
+                    $"unknown username; username: {username}", "Invalid username or password");
93
+            }
94
+            if (!userRes)
95
+            {
96
+                return userRes.To<LuUsersLoginResultDbo>();
97
+            }
98
+            if (userRes.Data.Password == null)
99
+            {
100
+                return LuResult<LuUsersLoginResultDbo>.Error(LuStatus.LoginError,
101
+                    $"null password; username: {username}", "Invalid username or password");
102
+            }
103
+            if (!VerifyPasswordHash(password, userRes.Data.Password, userRes.Data.Salt))
104
+            {
105
+                return LuResult<LuUsersLoginResultDbo>.Error(LuStatus.LoginError,
106
+                    $"invalid password; username: {username}", "Invalid username or password");
107
+            }
108
+
109
+            var tokenRes = _luTokensBusiness.GenerateToken(userRes.Data.ToLite());
110
+            if (!tokenRes)
111
+            {
112
+                return tokenRes.To<LuUsersLoginResultDbo>();
113
+            }
114
+
115
+            return LuResult<LuUsersLoginResultDbo>.Ok(new LuUsersLoginResultDbo
116
+            {
117
+                Token = tokenRes.Data,
118
+                User = userRes.Data.ToLite()
119
+            });
120
+        }
121
+
122
+        public LuResult<bool> Logout(string token)
50 123
         {
51
-            return true;//TODO
124
+            if (token == null)
125
+            {
126
+                return LuResult<bool>.Ok(true);
127
+            }
128
+            return _luTokensBusiness.UnRegisterToken(token).To(usersToken => true);
129
+        }
130
+
131
+        public LuResult<LuUsersFullDbo> Me()
132
+        {
133
+            return LuResult<LuUsersFullDbo>.Ok(_luLoggedUserAccessor.GetLoggedUser());
134
+        }
135
+
136
+        LuResult<T> ILuCrudInterface<LuUsersAddFullDbo, LuUsersFullDbo, LuUsersEditFullDbo, string>.DeleteSingleById<T>(string id, Func<LuUsersFullDbo, T> returnFunc)
137
+        {
138
+            var loggedUser = _luLoggedUserAccessor.GetLoggedUser();
139
+            if (id == Guid.Empty.ToDbo())
140
+            {
141
+                return LuResult<T>.Error(LuStatus.InputError,
142
+                    $"loggedUser: {loggedUser.Username}", "Can not remove anonymous user");
143
+            }
144
+            if (id == loggedUser.Id)
145
+            {
146
+                return LuResult<T>.Error(LuStatus.InputError,
147
+                    $"loggedUser: {loggedUser.Username}", "Can not remove yourself");
148
+            }
149
+            return base.DeleteSingleById(id, returnFunc);
52 150
         }
53 151
     }
54 152
 }

+ 27
- 13
Luticate2.Auth/Controllers/LuAuthExtensions.cs View File

@@ -5,6 +5,7 @@ using Luticate2.Auth.DataAccess;
5 5
 using Luticate2.Auth.Dbo.Users;
6 6
 using Luticate2.Auth.Interfaces.Groups;
7 7
 using Luticate2.Auth.Interfaces.Permissions;
8
+using Luticate2.Auth.Interfaces.Tokens;
8 9
 using Luticate2.Auth.Interfaces.Users;
9 10
 using Luticate2.Auth.Middlewares;
10 11
 using Luticate2.Utils.Controllers;
@@ -14,6 +15,7 @@ using Microsoft.AspNetCore.Http;
14 15
 using Microsoft.AspNetCore.Mvc;
15 16
 using Microsoft.EntityFrameworkCore;
16 17
 using Microsoft.Extensions.DependencyInjection;
18
+using Microsoft.Extensions.DependencyInjection.Extensions;
17 19
 
18 20
 namespace Luticate2.Auth.Controllers
19 21
 {
@@ -23,25 +25,31 @@ namespace Luticate2.Auth.Controllers
23 25
 
24 26
         public const string LuticateItemsLoggedUser = "loggedUser";
25 27
 
28
+        public const string TokenCookieName = "luticate2-token";
29
+
26 30
         public static IServiceCollection AddLuticateAuth(this IServiceCollection services,
27 31
             Action<LuUtilsOptionsDbo> optionsDelegate, Action<DbContextOptionsBuilder> optionsAction)
28 32
         {
29 33
             services.AddLuticateUtils(optionsDelegate);
30 34
 
31
-            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
35
+            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
36
+
37
+            services.TryAddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
38
+            services.TryAddScoped<LuAttrLoggedUserAccessor>();
39
+            services.TryAddScoped<LuAttrArgumentAccessor>();
32 40
 
33
-            services.AddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
34
-            services.AddScoped<LuAttrLoggedUserAccessor>();
35
-            services.AddScoped<LuAttrArgumentAccessor>();
41
+            services.TryAddTransient<LuGroupsController>();
42
+            services.TryAddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
43
+            services.TryAddTransient<LuGroupsDataAccess>();
36 44
 
37
-            services.AddTransient<LuGroupsController>();
38
-            services.AddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
39
-            services.AddTransient<LuGroupsDataAccess>();
45
+            services.TryAddTransient<LuUsersController>();
46
+            services.TryAddTransient<ILuUsersBusiness, LuUsersBusiness>();
47
+            services.TryAddTransient<LuUsersDataAccess>();
40 48
 
41
-            services.AddTransient<LuUsersController>();
42
-            services.AddTransient<ILuUsersBusiness, LuUsersBusiness>();
49
+            services.TryAddTransient<ILuTokensBusiness, LuTokensBusiness>();
50
+            services.TryAddTransient<LuTokensDataAccess>();
43 51
 
44
-            services.AddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
52
+            services.TryAddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
45 53
 
46 54
             services.AddDbContext<LuAuthDatabaseContext>(options =>
47 55
             {
@@ -73,14 +81,20 @@ namespace Luticate2.Auth.Controllers
73 81
             return app;
74 82
         }
75 83
 
76
-        public static UsersDbo GetLuLoggedUser(this HttpContext context)
84
+        public static LuUsersFullDbo GetLuLoggedUser(this HttpContext context)
77 85
         {
78
-            return context.GetLuItems()[LuticateItemsLoggedUser] as UsersDbo;
86
+            return context.GetLuItems()[LuticateItemsLoggedUser] as LuUsersFullDbo;
79 87
         }
80 88
 
81
-        public static void SetLuLoggedUser(this HttpContext context, UsersDbo user)
89
+        public static void SetLuLoggedUser(this HttpContext context, LuUsersFullDbo user)
82 90
         {
83 91
             context.GetLuItems()[LuticateItemsLoggedUser] = user;
84 92
         }
93
+
94
+        public static string GetLuUserToken(this HttpContext context)
95
+        {
96
+            var token = context.Request.Cookies[TokenCookieName];
97
+            return string.IsNullOrWhiteSpace(token) ? null : token;
98
+        }
85 99
     }
86 100
 }

+ 1
- 1
Luticate2.Auth/Controllers/LuLoggedUserAccessor.cs View File

@@ -13,7 +13,7 @@ namespace Luticate2.Auth.Controllers
13 13
             _httpContextAccessor = httpContextAccessor;
14 14
         }
15 15
 
16
-        public UsersDbo GetLoggedUser()
16
+        public LuUsersFullDbo GetLoggedUser()
17 17
         {
18 18
             return _httpContextAccessor.HttpContext.GetLuLoggedUser();
19 19
         }

+ 90
- 3
Luticate2.Auth/Controllers/LuUsersController.cs View File

@@ -1,15 +1,102 @@
1
-using Luticate2.Utils.Controllers;
1
+using System.ComponentModel.DataAnnotations;
2
+using Luticate2.Auth.Attributes;
3
+using Luticate2.Auth.Business;
4
+using Luticate2.Auth.Dbo.Permissions;
5
+using Luticate2.Auth.Dbo.Users;
6
+using Luticate2.Auth.Interfaces.Users;
7
+using Luticate2.Utils.Controllers;
2 8
 using Luticate2.Utils.Dbo.Basic;
9
+using Luticate2.Utils.Dbo.PaginatedRequest;
10
+using Luticate2.Utils.Dbo.Result;
11
+using Luticate2.Utils.Utils;
12
+using Microsoft.AspNetCore.Http;
3 13
 using Microsoft.AspNetCore.Mvc;
4 14
 using Microsoft.Extensions.Options;
5 15
 
6 16
 namespace Luticate2.Auth.Controllers
7 17
 {
8
-    [Route(LuAuthExtensions.RoutePrefix)]
18
+    [Route(LuAuthExtensions.RoutePrefix + "/[controller]")]
9 19
     public class LuUsersController : LuController
10 20
     {
11
-        public LuUsersController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(luUtilsOptionsDbo)
21
+        private const string EntityType = LuEntityTypes.LuUsers;
22
+
23
+        private const string ReadPermission = LuPermissions.LuGroupsRead;
24
+
25
+        private const string WritePermission = LuPermissions.LuGroupsWrite;
26
+
27
+        private readonly ILuUsersBusiness _busines;
28
+
29
+        public LuUsersController(ILuUsersBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(luUtilsOptionsDbo)
30
+        {
31
+            _busines = busines;
32
+        }
33
+
34
+        [HttpGet("{id}")]
35
+        [LuPermission(ReadPermission, EntityType)]
36
+        public LuApiWrapperDbo<LuUsersDbo> GetSingleById([LuPermissionArg][Required]string id)
37
+        {
38
+            return Handle(_busines.GetSingleById(id).ToLite());
39
+        }
40
+
41
+        [HttpGet]
42
+        [LuPermission(ReadPermission, EntityType)]
43
+        public LuApiWrapperDbo<LuPaginatedDbo<LuUsersDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
44
+        {
45
+            return Handle(_busines.GetMultiple(request).ToLite());
46
+        }
47
+
48
+        [HttpPost]
49
+        [LuPermission(WritePermission, EntityType)]
50
+        public LuApiWrapperDbo<LuUsersDbo> Register([Required]LuUsersAddDbo data)
51
+        {
52
+            return Handle(_busines.Register(data).ToLite());
53
+        }
54
+
55
+        [HttpPost("{id}")]
56
+        [LuPermission(WritePermission, EntityType)]
57
+        public LuApiWrapperDbo<LuUsersDbo> Edit([LuPermissionArg][Required]string id, [Required]LuUsersEditDbo data)
58
+        {
59
+            return Handle(_busines.Edit(id, data).ToLite());
60
+        }
61
+
62
+        [HttpDelete("{id}")]
63
+        [LuPermission(WritePermission, EntityType)]
64
+        public LuApiWrapperDbo<LuUsersDbo> Delete([LuPermissionArg][Required]string id)
65
+        {
66
+            return Handle(_busines.DeleteSingleByIdDbo(id).ToLite());
67
+        }
68
+
69
+        [HttpPost("login")]
70
+        public LuApiWrapperDbo<LuUsersLoginResultDbo> Login([Required]string username, [Required]string password)
71
+        {
72
+            var loginRes = _busines.Login(username, password);
73
+            if (loginRes)
74
+            {
75
+                Response.Cookies.Append(LuAuthExtensions.TokenCookieName, loginRes.Data.Token, new CookieOptions
76
+                {
77
+                    HttpOnly = true,
78
+                    Secure = true
79
+                });
80
+            }
81
+            return Handle(loginRes);
82
+        }
83
+
84
+        [HttpPost("logout")]
85
+        public LuApiWrapperDbo<bool> Logout()
86
+        {
87
+            var token = HttpContext.GetLuUserToken();
88
+            if (token != null)
89
+            {
90
+                Response.Cookies.Delete(LuAuthExtensions.TokenCookieName);
91
+                return Handle(_busines.Logout(token));
92
+            }
93
+            return Handle(LuResult<bool>.Ok(true));
94
+        }
95
+
96
+        [HttpGet("me")]
97
+        public LuApiWrapperDbo<LuUsersDbo> Me()
12 98
         {
99
+            return Handle(_busines.Me().ToLite());
13 100
         }
14 101
     }
15 102
 }

+ 13
- 0
Luticate2.Auth/DataAccess/LuAuthDatabaseContext.cs View File

@@ -48,6 +48,17 @@ namespace Luticate2.Auth.DataAccess
48 48
             
49 49
             
50 50
             
51
+            modelBuilder.Entity<lu_tokens>()
52
+                .HasKey(c => new { c.id });
53
+            
54
+            
55
+            modelBuilder.Entity<lu_tokens>()
56
+                .HasOne(e => e.fk_lu_users)
57
+                .WithMany(e => e.lu_tokens_fk)
58
+                .HasForeignKey("user_id")
59
+                .HasConstraintName("lu_tokens_user_id_fkey");
60
+            
61
+            
51 62
             modelBuilder.Entity<lu_users>()
52 63
                 .HasKey(c => new { c.id });
53 64
             
@@ -86,6 +97,8 @@ namespace Luticate2.Auth.DataAccess
86 97
         
87 98
         public virtual DbSet<lu_groups> lu_groups { get; set; }
88 99
         
100
+        public virtual DbSet<lu_tokens> lu_tokens { get; set; }
101
+        
89 102
         public virtual DbSet<lu_users> lu_users { get; set; }
90 103
         
91 104
         public virtual DbSet<lu_verb_users_groups> lu_verb_users_groups { get; set; }

+ 57
- 0
Luticate2.Auth/DataAccess/LuTokensDataAccess.cs View File

@@ -0,0 +1,57 @@
1
+using System;
2
+using System.Linq.Expressions;
3
+using Luticate2.Auth.DataAccess.Models;
4
+using Luticate2.Auth.Dbo.Tokens;
5
+using Luticate2.Utils.DataAccess;
6
+using Luticate2.Utils.Dbo.Filter;
7
+using Luticate2.Utils.Utils;
8
+using Microsoft.EntityFrameworkCore;
9
+using Newtonsoft.Json;
10
+
11
+namespace Luticate2.Auth.DataAccess
12
+{
13
+    public class LuTokensDataAccess : LuEfCrudDataAccess<lu_tokens, LuTokensAddDbo, LuTokensDbo, LuTokensEditDbo, LuAuthDatabaseContext, string>
14
+    {
15
+        public LuTokensDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
16
+        {
17
+        }
18
+
19
+        protected override object GetId(string id)
20
+        {
21
+            return id;
22
+        }
23
+
24
+        protected override DbSet<lu_tokens> GetTable(LuAuthDatabaseContext db)
25
+        {
26
+            return db.lu_tokens;
27
+        }
28
+
29
+        protected override Expression<Func<lu_tokens, bool>> GetFilterExpression(LuFilterDbo filter)
30
+        {
31
+            var userId = filter.GetFilterString("userId", null);
32
+            return tokens => (userId == null || tokens.user_id.ToString() == userId);
33
+        }
34
+
35
+        protected override lu_tokens GetModelFromTCreate(LuTokensAddDbo obj)
36
+        {
37
+            return new lu_tokens
38
+            {
39
+                data = JsonConvert.SerializeObject(obj.Data),
40
+                id = obj.Id,
41
+                notAfter = obj.NotAfter,
42
+                notBefore = obj.NotBefore,
43
+                user_id = obj.UserId.ToGuid()
44
+            };
45
+        }
46
+
47
+        protected override void EditModelFromTUpdate(LuTokensEditDbo obj, lu_tokens model)
48
+        {
49
+            throw new NotImplementedException();
50
+        }
51
+
52
+        protected override LuTokensDbo GetDboFromModel(lu_tokens model)
53
+        {
54
+            return model.ToDbo();
55
+        }
56
+    }
57
+}

+ 73
- 0
Luticate2.Auth/DataAccess/LuUsersDataAccess.cs View File

@@ -0,0 +1,73 @@
1
+using System;
2
+using System.Linq;
3
+using Luticate2.Auth.DataAccess.Models;
4
+using Luticate2.Auth.Dbo.Users;
5
+using Luticate2.Utils.DataAccess;
6
+using Luticate2.Utils.Dbo.Result;
7
+using Luticate2.Utils.Utils;
8
+using Microsoft.EntityFrameworkCore;
9
+using Newtonsoft.Json;
10
+using Npgsql;
11
+
12
+namespace Luticate2.Auth.DataAccess
13
+{
14
+    public class LuUsersDataAccess : LuEfCrudDataAccess<lu_users, LuUsersAddFullDbo, LuUsersFullDbo, LuUsersEditFullDbo, LuAuthDatabaseContext, string>
15
+    {
16
+        public LuUsersDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
17
+        {
18
+        }
19
+
20
+        protected override DbSet<lu_users> GetTable(LuAuthDatabaseContext db)
21
+        {
22
+            return db.lu_users;
23
+        }
24
+
25
+        protected override lu_users GetModelFromTCreate(LuUsersAddFullDbo obj)
26
+        {
27
+            return new lu_users
28
+            {
29
+                authentication_source_id = obj.AuthenticationSourceId.ToGuid(),
30
+                data = JsonConvert.SerializeObject(obj.Data),
31
+                password = obj.Password,
32
+                salt = obj.Salt,
33
+                username = obj.Username
34
+            };
35
+        }
36
+
37
+        protected override LuResult<T> HandleError<T>(Exception e)
38
+        {
39
+            if (e is DbUpdateException && e.InnerException is PostgresException)
40
+            {
41
+                var pge = (PostgresException) e.InnerException;
42
+                if (pge.ConstraintName == "lu_users_authentication_source_id_username_key")
43
+                {
44
+                    return LuResult<T>.Error(LuStatus.InputError, e, "Username already exists");
45
+                }
46
+            }
47
+            return null;
48
+        }
49
+
50
+        protected override void EditModelFromTUpdate(LuUsersEditFullDbo obj, lu_users model)
51
+        {
52
+            throw new NotImplementedException();//TODO
53
+        }
54
+
55
+        protected override LuUsersFullDbo GetDboFromModel(lu_users model)
56
+        {
57
+            return model.ToDbo();
58
+        }
59
+
60
+        public LuResult<LuUsersFullDbo> FindByUsername(string username)
61
+        {
62
+            return Execute((context, set) =>
63
+            {
64
+                var user = set.FirstOrDefault(users => users.username.ToLower() == username.ToLower())?.ToDbo();
65
+                if (user != null)
66
+                {
67
+                    return LuResult<LuUsersFullDbo>.Ok(user);
68
+                }
69
+                return LuResult<LuUsersFullDbo>.Error(LuStatus.NotFound, $"username: {username}", "Username not found");
70
+            });
71
+        }
72
+    }
73
+}

+ 0
- 21
Luticate2.Auth/DataAccess/Models/ModelsToDbo.cs View File

@@ -1,21 +0,0 @@
1
-using Luticate2.Auth.Dbo.Groups;
2
-using Luticate2.Utils.Utils;
3
-
4
-namespace Luticate2.Auth.DataAccess.Models
5
-{
6
-    public static class ModelsToDbo
7
-    {
8
-        public static LuGroupsDbo ToDbo(this lu_groups model)
9
-        {
10
-            if (model == null)
11
-            {
12
-                return null;
13
-            }
14
-            return new LuGroupsDbo
15
-            {
16
-                Id = model.id.ToDbo(),
17
-                Name = model.name
18
-            };
19
-        }
20
-    }
21
-}

+ 2
- 0
Luticate2.Auth/DataAccess/Models/lu_authentication_sources.cs View File

@@ -16,6 +16,8 @@ namespace Luticate2.Auth.DataAccess.Models
16 16
         
17 17
         public string data { get; set; }
18 18
         
19
+        public string realm { get; set; }
20
+        
19 21
         
20 22
         
21 23
         public virtual IList<lu_users> lu_users_fk { get; set; }

+ 26
- 0
Luticate2.Auth/DataAccess/Models/lu_tokens.cs View File

@@ -0,0 +1,26 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.ComponentModel.DataAnnotations;
4
+using System.ComponentModel.DataAnnotations.Schema;
5
+
6
+namespace Luticate2.Auth.DataAccess.Models
7
+{
8
+    public partial class lu_tokens
9
+    {
10
+        
11
+        public string id { get; set; }
12
+        
13
+        public DateTime? notBefore { get; set; }
14
+        
15
+        public DateTime? notAfter { get; set; }
16
+        
17
+        public Guid user_id { get; set; }
18
+        
19
+        public string data { get; set; }
20
+        
21
+        
22
+        public virtual lu_users fk_lu_users { get; set; }
23
+        
24
+        
25
+    }
26
+}

+ 2
- 0
Luticate2.Auth/DataAccess/Models/lu_users.cs View File

@@ -24,6 +24,8 @@ namespace Luticate2.Auth.DataAccess.Models
24 24
         public virtual lu_authentication_sources fk_lu_authentication_sources { get; set; }
25 25
         
26 26
         
27
+        public virtual IList<lu_tokens> lu_tokens_fk { get; set; }
28
+        
27 29
         public virtual IList<lu_verb_users_groups> lu_verb_users_groups_fk { get; set; }
28 30
         
29 31
     }

+ 59
- 0
Luticate2.Auth/DataAccess/ModelsToDbo.cs View File

@@ -0,0 +1,59 @@
1
+using System.Collections.Generic;
2
+using Luticate2.Auth.DataAccess.Models;
3
+using Luticate2.Auth.Dbo.Groups;
4
+using Luticate2.Auth.Dbo.Tokens;
5
+using Luticate2.Auth.Dbo.Users;
6
+using Luticate2.Utils.Utils;
7
+using Newtonsoft.Json;
8
+
9
+namespace Luticate2.Auth.DataAccess
10
+{
11
+    public static class ModelsToDbo
12
+    {
13
+        public static LuGroupsDbo ToDbo(this lu_groups model)
14
+        {
15
+            if (model == null)
16
+            {
17
+                return null;
18
+            }
19
+            return new LuGroupsDbo
20
+            {
21
+                Id = model.id.ToDbo(),
22
+                Name = model.name
23
+            };
24
+        }
25
+
26
+        public static LuUsersFullDbo ToDbo(this lu_users model)
27
+        {
28
+            if (model == null)
29
+            {
30
+                return null;
31
+            }
32
+            return new LuUsersFullDbo
33
+            {
34
+                AuthenticationSourceId = model.authentication_source_id.ToDbo(),
35
+                Data = JsonConvert.DeserializeObject<IDictionary<string, object>>(model.data),
36
+                Id = model.id.ToDbo(),
37
+                Password = model.password,
38
+                Salt = model.salt,
39
+                Username = model.username
40
+            };
41
+        }
42
+
43
+        public static LuTokensDbo ToDbo(this lu_tokens model)
44
+        {
45
+            if (model == null)
46
+            {
47
+                return null;
48
+            }
49
+            return new LuTokensDbo
50
+            {
51
+                Data = JsonConvert.DeserializeObject<IDictionary<string, object>>(model.data),
52
+                Id = model.id,
53
+                NotAfter = model.notAfter,
54
+                NotBefore = model.notBefore,
55
+                UserId = model.user_id.ToDbo()
56
+            };
57
+        }
58
+    }
59
+}

+ 29
- 0
Luticate2.Auth/DataAccess/code-from-ds/code-from-ds.json View File

@@ -21,6 +21,10 @@
21 21
                     {
22 22
                         "column": "data",
23 23
                         "selected": true
24
+                    },
25
+                    {
26
+                        "column": "realm",
27
+                        "selected": true
24 28
                     }
25 29
                 ],
26 30
                 "table": "lu_authentication_sources"
@@ -38,6 +42,31 @@
38 42
                 ],
39 43
                 "table": "lu_groups"
40 44
             },
45
+            {
46
+                "columns": [
47
+                    {
48
+                        "column": "id",
49
+                        "selected": true
50
+                    },
51
+                    {
52
+                        "column": "notBefore",
53
+                        "selected": true
54
+                    },
55
+                    {
56
+                        "column": "notAfter",
57
+                        "selected": true
58
+                    },
59
+                    {
60
+                        "column": "user_id",
61
+                        "selected": true
62
+                    },
63
+                    {
64
+                        "column": "data",
65
+                        "selected": true
66
+                    }
67
+                ],
68
+                "table": "lu_tokens"
69
+            },
41 70
             {
42 71
                 "columns": [
43 72
                     {

+ 4
- 0
Luticate2.Auth/Dbo/Permissions/LuPermissions.cs View File

@@ -5,5 +5,9 @@
5 5
         public const string LuGroupsRead = "LU_GROUPS_READ";
6 6
 
7 7
         public const string LuGroupsWrite = "LU_GROUPS_WRITE";
8
+
9
+        public const string LuUsersRead = "LU_USERS_READ";
10
+
11
+        public const string LuUsersWrite = "LU_USERS_WRITE";
8 12
     }
9 13
 }

+ 18
- 0
Luticate2.Auth/Dbo/Tokens/LuTokensAddDbo.cs View File

@@ -0,0 +1,18 @@
1
+using System;
2
+using System.Collections.Generic;
3
+
4
+namespace Luticate2.Auth.Dbo.Tokens
5
+{
6
+    public class LuTokensAddDbo : LuTokensEditDbo
7
+    {
8
+        public string Id { get; set; }
9
+
10
+        public DateTime? NotBefore { get; set; }
11
+
12
+        public DateTime? NotAfter { get; set; }
13
+
14
+        public IDictionary<string, object> Data { get; set; }
15
+
16
+        public string UserId { get; set; }
17
+    }
18
+}

+ 6
- 0
Luticate2.Auth/Dbo/Tokens/LuTokensDbo.cs View File

@@ -0,0 +1,6 @@
1
+namespace Luticate2.Auth.Dbo.Tokens
2
+{
3
+    public class LuTokensDbo : LuTokensAddDbo
4
+    {
5
+    }
6
+}

+ 7
- 0
Luticate2.Auth/Dbo/Tokens/LuTokensEditDbo.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Auth.Dbo.Tokens
2
+{
3
+    public class LuTokensEditDbo
4
+    {
5
+
6
+    }
7
+}

+ 9
- 0
Luticate2.Auth/Dbo/Users/LuUsersAddDbo.cs View File

@@ -0,0 +1,9 @@
1
+namespace Luticate2.Auth.Dbo.Users
2
+{
3
+    public class LuUsersAddDbo
4
+    {
5
+        public string Username { get; set; }
6
+
7
+        public string Password { get; set; }
8
+    }
9
+}

+ 7
- 0
Luticate2.Auth/Dbo/Users/LuUsersAddFullDbo.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Auth.Dbo.Users
2
+{
3
+    public class LuUsersAddFullDbo : LuUsersEditFullDbo
4
+    {
5
+        public string AuthenticationSourceId { get; set; }
6
+    }
7
+}

Luticate2.Auth/Dbo/Users/UsersDbo.cs → Luticate2.Auth/Dbo/Users/LuUsersDbo.cs View File

@@ -1,6 +1,6 @@
1 1
 namespace Luticate2.Auth.Dbo.Users
2 2
 {
3
-    public class UsersDbo
3
+    public class LuUsersDbo
4 4
     {
5 5
         public string Id { get; set; }
6 6
 

+ 7
- 0
Luticate2.Auth/Dbo/Users/LuUsersEditDbo.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Auth.Dbo.Users
2
+{
3
+    public class LuUsersEditDbo
4
+    {
5
+
6
+    }
7
+}

+ 15
- 0
Luticate2.Auth/Dbo/Users/LuUsersEditFullDbo.cs View File

@@ -0,0 +1,15 @@
1
+using System.Collections.Generic;
2
+
3
+namespace Luticate2.Auth.Dbo.Users
4
+{
5
+    public class LuUsersEditFullDbo
6
+    {
7
+        public string Username { get; set; }
8
+
9
+        public string Password { get; set; }
10
+
11
+        public string Salt { get; set; }
12
+
13
+        public IDictionary<string, object> Data { get; set; }
14
+    }
15
+}

+ 7
- 0
Luticate2.Auth/Dbo/Users/LuUsersFullDbo.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Auth.Dbo.Users
2
+{
3
+    public class LuUsersFullDbo : LuUsersAddFullDbo
4
+    {
5
+        public string Id { get; set; }
6
+    }
7
+}

+ 9
- 0
Luticate2.Auth/Dbo/Users/LuUsersLoginResultDbo.cs View File

@@ -0,0 +1,9 @@
1
+namespace Luticate2.Auth.Dbo.Users
2
+{
3
+    public class LuUsersLoginResultDbo
4
+    {
5
+        public LuUsersDbo User { get; set; }
6
+
7
+        public string Token { get; set; }
8
+    }
9
+}

Luticate2.Auth/Dbo/Users/UsersToken.cs → Luticate2.Auth/Dbo/Users/LuUsersToken.cs View File

@@ -1,8 +1,9 @@
1 1
 using System;
2
+using System.Collections.Generic;
2 3
 
3 4
 namespace Luticate2.Auth.Dbo.Users
4 5
 {
5
-    public class UsersToken
6
+    public class LuUsersToken
6 7
     {
7 8
         public string UserId { get; set; }
8 9
 
@@ -10,6 +11,6 @@ namespace Luticate2.Auth.Dbo.Users
10 11
 
11 12
         public DateTime? NotAfter { get; set; }
12 13
 
13
-        public object Data { get; set; }
14
+        public IDictionary<string, object> Data { get; set; }
14 15
     }
15 16
 }

+ 18
- 0
Luticate2.Auth/Interfaces/Tokens/ILuTokensBusiness.cs View File

@@ -0,0 +1,18 @@
1
+using Luticate2.Auth.Dbo.Users;
2
+using Luticate2.Utils.Dbo.Result;
3
+
4
+namespace Luticate2.Auth.Interfaces.Tokens
5
+{
6
+    public interface ILuTokensBusiness
7
+    {
8
+        LuResult<LuUsersToken> GetToken(string token);
9
+
10
+        LuResult<string> RegisterToken(LuUsersToken token);
11
+
12
+        LuResult<LuUsersToken> UnRegisterToken(string token);
13
+
14
+        bool IsTokenValid(LuUsersToken token);
15
+
16
+        LuResult<string> GenerateToken(LuUsersDbo user);
17
+    }
18
+}

+ 1
- 1
Luticate2.Auth/Interfaces/Users/ILuLoggedUserAccessor.cs View File

@@ -4,6 +4,6 @@ namespace Luticate2.Auth.Interfaces.Users
4 4
 {
5 5
     public interface ILuLoggedUserAccessor
6 6
     {
7
-        UsersDbo GetLoggedUser();
7
+        LuUsersFullDbo GetLoggedUser();
8 8
     }
9 9
 }

+ 13
- 8
Luticate2.Auth/Interfaces/Users/ILuUsersBusiness.cs View File

@@ -1,22 +1,27 @@
1 1
 using Luticate2.Auth.Dbo.Users;
2 2
 using Luticate2.Utils.Dbo.Result;
3
+using Luticate2.Utils.Interfaces;
3 4
 
4 5
 namespace Luticate2.Auth.Interfaces.Users
5 6
 {
6
-    public interface ILuUsersBusiness
7
+    public interface ILuUsersBusiness : ILuCrudInterface<LuUsersAddFullDbo, LuUsersFullDbo, LuUsersEditFullDbo, string>
7 8
     {
8
-        LuResult<UsersToken> GetToken(string token);
9
+        string GenerateSalt();
9 10
 
10
-        LuResult<string> RegisterToken(UsersToken token);
11
+        string HashPassword(string password, string salt);
11 12
 
12
-        LuResult<UsersToken> UnRegisterToken(string token);
13
+        bool VerifyPasswordHash(string password, string hash, string salt);
13 14
 
14
-        bool IsTokenValid(UsersToken token);
15
+        LuResult<LuUsersLoginResultDbo> Login(string username, string password);
15 16
 
16
-        LuResult<UsersDbo> GetSingleById(string id);
17
+        LuResult<bool> Logout(string token);
17 18
 
18
-        string HashPassword(string password, string salt);
19
+        LuResult<LuUsersFullDbo> FindByUsername(string username);
19 20
 
20
-        bool VerifyPasswordHash(string password, string hash, string salt);
21
+        LuResult<LuUsersFullDbo> Register(LuUsersAddDbo user);
22
+
23
+        LuResult<LuUsersFullDbo> Edit(string id, LuUsersEditDbo user);
24
+
25
+        LuResult<LuUsersFullDbo> Me();
21 26
     }
22 27
 }

+ 11
- 10
Luticate2.Auth/Middlewares/LuLoggedUserMiddleware.cs View File

@@ -1,8 +1,8 @@
1 1
 using System;
2
+using Luticate2.Auth.Business;
2 3
 using Luticate2.Auth.Controllers;
3
-using Luticate2.Auth.Dbo.Users;
4
+using Luticate2.Auth.Interfaces.Tokens;
4 5
 using Luticate2.Auth.Interfaces.Users;
5
-using Luticate2.Utils.Controllers;
6 6
 using Luticate2.Utils.Dbo.Result;
7 7
 using Luticate2.Utils.Utils;
8 8
 using Microsoft.AspNetCore.Mvc.Filters;
@@ -11,31 +11,32 @@ namespace Luticate2.Auth.Middlewares
11 11
 {
12 12
     public class LuLoggedUserMiddleware : IActionFilter
13 13
     {
14
-        public const string TokenCookieName = "luticate2-token";
15 14
 
16 15
         private readonly ILuUsersBusiness _luUsersBusiness;
16
+        private readonly ILuTokensBusiness _luTokensBusiness;
17 17
 
18
-        public LuLoggedUserMiddleware(ILuUsersBusiness luUsersBusiness)
18
+        public LuLoggedUserMiddleware(ILuUsersBusiness luUsersBusiness, ILuTokensBusiness luTokensBusiness)
19 19
         {
20 20
             _luUsersBusiness = luUsersBusiness;
21
+            _luTokensBusiness = luTokensBusiness;
21 22
         }
22 23
 
23 24
         public void OnActionExecuting(ActionExecutingContext context)
24 25
         {
25
-            var token = context.HttpContext.Request.Cookies[TokenCookieName];
26
+            var token = context.HttpContext.GetLuUserToken();
26 27
             var userId = Guid.Empty.ToDbo();
27
-            if (!string.IsNullOrWhiteSpace(token))
28
+            if (token != null)
28 29
             {
29
-                var tokenRes = _luUsersBusiness.GetToken(token);
30
+                var tokenRes = _luTokensBusiness.GetToken(token);
30 31
                 if (tokenRes.Status == LuStatus.NotFound)
31 32
                 {
32
-                    LuResult<object>.Error(LuStatus.LoginError, $"token: {token}", "Invalid session").Throw();
33
+                    LuResult<object>.Error(LuStatus.LoginError, $"unknown token: {token}", "Invalid session").Throw();
33 34
                 }
34 35
                 tokenRes.ThrowIfNotSuccess();
35
-                var tokenValid = _luUsersBusiness.IsTokenValid(tokenRes.Data);
36
+                var tokenValid = _luTokensBusiness.IsTokenValid(tokenRes.Data);
36 37
                 if (!tokenValid)
37 38
                 {
38
-                    LuResult<object>.Error(LuStatus.LoginError, $"token: {token}", "Invalid session").Throw();
39
+                    LuResult<object>.Error(LuStatus.LoginError, $"invalid token: {token}", "Invalid session").Throw();
39 40
                 }
40 41
                 userId = tokenRes.Data.UserId;
41 42
             }

+ 15
- 14
Luticate2.Auth/project.json View File

@@ -1,17 +1,18 @@
1 1
 {
2
-    "version": "0.5.0",
3
-    "buildOptions": {
4
-        "debugType": "portable"
5
-    },
6
-    "dependencies": {
7
-        "Luticate2.Utils": "0.5.*"
8
-    },
9
-    "frameworks": {
10
-        "netcoreapp1.0": {
11
-            "imports": [
12
-                "dotnet5.6",
13
-                "portable-net45+win8"
14
-            ]
15
-        }
2
+  "version": "0.5.0",
3
+  "buildOptions": {
4
+    "debugType": "portable"
5
+  },
6
+  "dependencies": {
7
+    "Luticate2.Utils": "0.5.*",
8
+    "Microsoft.AspNetCore.Cryptography.KeyDerivation": "1.1.1"
9
+  },
10
+  "frameworks": {
11
+    "netcoreapp1.0": {
12
+      "imports": [
13
+        "dotnet5.6",
14
+        "portable-net45+win8"
15
+      ]
16 16
     }
17
+  }
17 18
 }

+ 6
- 3
Luticate2.Utils/Controllers/LuUtilsExtensions.cs View File

@@ -11,6 +11,7 @@ using Luticate2.Utils.Dbo.Result;
11 11
 using Luticate2.Utils.Hubs;
12 12
 using Luticate2.Utils.Interfaces;
13 13
 using Luticate2.Utils.Middlewares;
14
+using Luticate2.Utils.Utils;
14 15
 using Microsoft.AspNetCore.Builder;
15 16
 using Microsoft.AspNetCore.Http;
16 17
 using Microsoft.AspNetCore.Mvc;
@@ -38,9 +39,11 @@ namespace Luticate2.Utils.Controllers
38 39
                 options.Hubs.EnableDetailedErrors = true;
39 40
             });
40 41
 
41
-            services.AddScoped<LuEfTransactionScope>();
42
-            services.AddSingleton<LuHubConnectionTracker>();
43
-            services.AddSingleton<ILuNotificationsBusiness, LuNotificationsBusiness>();
42
+            services.TryAddSingleton<IDateTime, SystemDateTime>();
43
+
44
+            services.TryAddScoped<LuEfTransactionScope>();
45
+            services.TryAddSingleton<LuHubConnectionTracker>();
46
+            services.TryAddSingleton<ILuNotificationsBusiness, LuNotificationsBusiness>();
44 47
             services.Configure(optionsDelegate);
45 48
             return services;
46 49
         }

+ 9
- 0
Luticate2.Utils/Interfaces/IDateTime.cs View File

@@ -0,0 +1,9 @@
1
+using System;
2
+
3
+namespace Luticate2.Utils.Interfaces
4
+{
5
+    public interface IDateTime
6
+    {
7
+        DateTime Now { get; }
8
+    }
9
+}

+ 10
- 0
Luticate2.Utils/Utils/SystemDateTime.cs View File

@@ -0,0 +1,10 @@
1
+using System;
2
+using Luticate2.Utils.Interfaces;
3
+
4
+namespace Luticate2.Utils.Utils
5
+{
6
+    public class SystemDateTime : IDateTime
7
+    {
8
+        public DateTime Now => DateTime.Now;
9
+    }
10
+}

+ 22
- 0
TestAuth/Business/LuUsersBusinessTest.cs View File

@@ -0,0 +1,22 @@
1
+using Luticate2.Auth.Interfaces.Users;
2
+using Xunit;
3
+
4
+namespace TestAuth.Business
5
+{
6
+    public class LuUsersBusinessTest
7
+    {
8
+        [Fact]
9
+        public void TestPassword()
10
+        {
11
+            Tests.TestRealDb<ILuUsersBusiness>(business =>
12
+            {
13
+                var password = "test42";
14
+                var salt = business.GenerateSalt();
15
+                var hashed = business.HashPassword(password, salt);
16
+                Assert.True(business.VerifyPasswordHash(password, hashed, salt));
17
+                Assert.False(business.VerifyPasswordHash(password + "0", hashed, salt));
18
+                Assert.False(business.VerifyPasswordHash("0" + password, hashed, salt));
19
+            });
20
+        }
21
+    }
22
+}

+ 21
- 0
TestAuth/Tests.cs View File

@@ -2,11 +2,31 @@
2 2
 using Luticate2.Auth.Controllers;
3 3
 using Luticate2.Auth.DataAccess;
4 4
 using Luticate2.Utils.DataAccess;
5
+using Luticate2.Utils.Interfaces;
5 6
 using Microsoft.EntityFrameworkCore;
6 7
 using Microsoft.Extensions.DependencyInjection;
7 8
 
8 9
 namespace TestAuth
9 10
 {
11
+    public class DummyNotificationsBusiness : ILuNotificationsBusiness
12
+    {
13
+        public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
14
+        {
15
+        }
16
+
17
+        public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
18
+        {
19
+        }
20
+
21
+        public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
22
+        {
23
+        }
24
+
25
+        public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
26
+        {
27
+        }
28
+    }
29
+
10 30
     public class Tests
11 31
     {
12 32
         public const string RealDbConnectionString =
@@ -15,6 +35,7 @@ namespace TestAuth
15 35
         public static IServiceProvider BuildRealDbServiceProvider()
16 36
         {
17 37
             IServiceCollection serviceCollection = new ServiceCollection();
38
+            serviceCollection.AddSingleton<ILuNotificationsBusiness, DummyNotificationsBusiness>();
18 39
             serviceCollection.AddLuticateAuth(dbo =>
19 40
             {
20 41
                 dbo.Version = "tests";

+ 1
- 0
TestAuth/project.json View File

@@ -6,6 +6,7 @@
6 6
     "dependencies": {
7 7
         "dotnet-test-xunit": "1.0.0-rc2-*",
8 8
         "Luticate2.Auth": "0.5.*",
9
+        "Microsoft.DotNet.InternalAbstractions": "1.0.500-preview2-1-003177",
9 10
         "Moq": "4.6.38-alpha",
10 11
         "System.Runtime.Serialization.Primitives": "4.1.1",
11 12
         "xunit": "2.1.0"

+ 26
- 25
TestUtils/project.json View File

@@ -1,28 +1,29 @@
1
-{
2
-    "version": "0.1.0",
3
-    "buildOptions": {
4
-        "debugType": "portable"
5
-    },
6
-    "dependencies": {
7
-        "dotnet-test-xunit": "1.0.0-rc2-*",
8
-        "Luticate2.Utils": "0.5.*",
9
-        "Moq": "4.6.38-alpha",
10
-        "System.Runtime.Serialization.Primitives": "4.1.1",
11
-        "xunit": "2.1.0"
12
-    },
13
-    "testRunner": "xunit",
14
-    "frameworks": {
15
-        "netcoreapp1.0": {
16
-            "dependencies": {
17
-                "Microsoft.NETCore.App": {
18
-                    "type": "platform",
19
-                    "version": "1.0.1"
20
-                }
21
-            },
22
-            "imports": [
23
-                "dotnet5.4",
24
-                "portable-net451+win8"
25
-            ]
1
+{
2
+  "version": "0.1.0",
3
+  "buildOptions": {
4
+    "debugType": "portable"
5
+  },
6
+  "dependencies": {
7
+    "dotnet-test-xunit": "1.0.0-rc2-*",
8
+    "Luticate2.Utils": "0.5.*",
9
+    "Microsoft.DotNet.InternalAbstractions": "1.0.500-preview2-1-003177",
10
+    "Moq": "4.6.38-alpha",
11
+    "System.Runtime.Serialization.Primitives": "4.1.1",
12
+    "xunit": "2.1.0"
13
+  },
14
+  "testRunner": "xunit",
15
+  "frameworks": {
16
+    "netcoreapp1.0": {
17
+      "dependencies": {
18
+        "Microsoft.NETCore.App": {
19
+          "type": "platform",
20
+          "version": "1.0.1"
26 21
         }
22
+      },
23
+      "imports": [
24
+        "dotnet5.4",
25
+        "portable-net451+win8"
26
+      ]
27 27
     }
28
+  }
28 29
 }

Loading…
Cancel
Save