token.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package auth
  2. import (
  3. "time"
  4. )
  5. // TokenInfo the token information model interface
  6. type TokenInfo interface {
  7. New() TokenInfo
  8. GetUserID() string
  9. SetUserID(string)
  10. GetAccess() string
  11. SetAccess(string)
  12. GetAccessCreateAt() time.Time
  13. SetAccessCreateAt(time.Time)
  14. GetAccessExpiresIn() time.Duration
  15. SetAccessExpiresIn(time.Duration)
  16. GetRefresh() string
  17. SetRefresh(string)
  18. GetRefreshCreateAt() time.Time
  19. SetRefreshCreateAt(time.Time)
  20. GetRefreshExpiresIn() time.Duration
  21. SetRefreshExpiresIn(time.Duration)
  22. }
  23. // NewToken create to token model instance
  24. func NewToken() *Token {
  25. return &Token{}
  26. }
  27. // Token token model
  28. type Token struct {
  29. UserID string `bson:"UserID"`
  30. Access string `bson:"Access"`
  31. AccessCreateAt time.Time `bson:"AccessCreateAt"`
  32. AccessExpiresIn time.Duration `bson:"AccessExpiresIn"`
  33. Refresh string `bson:"Refresh"`
  34. RefreshCreateAt time.Time `bson:"RefreshCreateAt"`
  35. RefreshExpiresIn time.Duration `bson:"RefreshExpiresIn"`
  36. }
  37. // New create to token model instance
  38. func (t *Token) New() TokenInfo {
  39. return NewToken()
  40. }
  41. // GetUserID the user id
  42. func (t *Token) GetUserID() string {
  43. return t.UserID
  44. }
  45. // SetUserID the user id
  46. func (t *Token) SetUserID(userID string) {
  47. t.UserID = userID
  48. }
  49. // GetAccess access Token
  50. func (t *Token) GetAccess() string {
  51. return t.Access
  52. }
  53. // SetAccess access Token
  54. func (t *Token) SetAccess(access string) {
  55. t.Access = access
  56. }
  57. // GetAccessCreateAt create Time
  58. func (t *Token) GetAccessCreateAt() time.Time {
  59. return t.AccessCreateAt
  60. }
  61. // SetAccessCreateAt create Time
  62. func (t *Token) SetAccessCreateAt(createAt time.Time) {
  63. t.AccessCreateAt = createAt
  64. }
  65. // GetAccessExpiresIn the lifetime in seconds of the access token
  66. func (t *Token) GetAccessExpiresIn() time.Duration {
  67. return t.AccessExpiresIn
  68. }
  69. // SetAccessExpiresIn the lifetime in seconds of the access token
  70. func (t *Token) SetAccessExpiresIn(exp time.Duration) {
  71. t.AccessExpiresIn = exp
  72. }
  73. // GetRefresh refresh Token
  74. func (t *Token) GetRefresh() string {
  75. return t.Refresh
  76. }
  77. // SetRefresh refresh Token
  78. func (t *Token) SetRefresh(refresh string) {
  79. t.Refresh = refresh
  80. }
  81. // GetRefreshCreateAt create Time
  82. func (t *Token) GetRefreshCreateAt() time.Time {
  83. return t.RefreshCreateAt
  84. }
  85. // SetRefreshCreateAt create Time
  86. func (t *Token) SetRefreshCreateAt(createAt time.Time) {
  87. t.RefreshCreateAt = createAt
  88. }
  89. // GetRefreshExpiresIn the lifetime in seconds of the refresh token
  90. func (t *Token) GetRefreshExpiresIn() time.Duration {
  91. return t.RefreshExpiresIn
  92. }
  93. // SetRefreshExpiresIn the lifetime in seconds of the refresh token
  94. func (t *Token) SetRefreshExpiresIn(exp time.Duration) {
  95. t.RefreshExpiresIn = exp
  96. }