DefaultTokenOptionsProvider.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using Wicture.DbRESTFul.Configuration;
  3. using Wicture.DbRESTFul.Resources.Api;
  4. namespace Wicture.DbRESTFul.Authentication
  5. {
  6. public class DefaultTokenOptionsProvider : ITokenOptionsProvider
  7. {
  8. private readonly ISectionConfigProvider _sectionConfigProvider;
  9. private static TokenOptions _default = new TokenOptions()
  10. {
  11. TokenRequestPath = "/token",
  12. RefreshTokenRequestPath = "/token/refresh",
  13. SecretKey = "Wicture@Micr0serv!ces^Secretkeys", //"Wicture@Micr0serv!ce^Secret",
  14. Audience = "DbRESTFulApi",
  15. Issuer = "Wicture",
  16. CookieName = "access_token",
  17. UseJwtCookie = true,
  18. Expiration = TimeSpan.FromHours(24),
  19. LoginSchema = new Schema[]
  20. {
  21. new Schema { name = "username", nullable = false, type = SchemaType.String },
  22. new Schema { name = "password", nullable = false, type = SchemaType.String },
  23. new Schema { name = "tag", nullable = true, type = SchemaType.String }
  24. }
  25. };
  26. public DefaultTokenOptionsProvider(ISectionConfigProvider sectionConfigProvider)
  27. {
  28. _sectionConfigProvider = sectionConfigProvider;
  29. }
  30. public virtual TokenOptions Provide()
  31. {
  32. var option = _sectionConfigProvider.Provide<TokenOptions>();
  33. if (option == null) return _default;
  34. if (string.IsNullOrEmpty(option.TokenRequestPath)) option.TokenRequestPath = _default.TokenRequestPath;
  35. if (string.IsNullOrEmpty(option.RefreshTokenRequestPath)) option.RefreshTokenRequestPath = _default.RefreshTokenRequestPath;
  36. if (string.IsNullOrEmpty(option.SecretKey)) option.SecretKey = _default.SecretKey;
  37. if (string.IsNullOrEmpty(option.Audience)) option.Audience = _default.Audience;
  38. if (string.IsNullOrEmpty(option.Issuer)) option.Issuer = _default.Issuer;
  39. if (option.Expiration == default) option.Expiration = _default.Expiration;
  40. if (!(option.LoginSchema?.Length > 0)) option.LoginSchema = _default.LoginSchema;
  41. return option;
  42. }
  43. }
  44. }