Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LuTokensBusiness.cs 3.4KB

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