token.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "crypto/sha256"
  17. "encoding/hex"
  18. "sync"
  19. "time"
  20. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  21. "github.com/drakkan/sftpgo/v2/internal/logger"
  22. "github.com/drakkan/sftpgo/v2/internal/util"
  23. )
  24. func newTokenManager(isShared int) tokenManager {
  25. if isShared == 1 {
  26. logger.Info(logSender, "", "using provider token manager")
  27. return &dbTokenManager{}
  28. }
  29. logger.Info(logSender, "", "using memory token manager")
  30. return &memoryTokenManager{}
  31. }
  32. type tokenManager interface {
  33. Add(token string, expiresAt time.Time)
  34. Get(token string) bool
  35. Cleanup()
  36. }
  37. type memoryTokenManager struct {
  38. invalidatedJWTTokens sync.Map
  39. }
  40. func (m *memoryTokenManager) Add(token string, expiresAt time.Time) {
  41. m.invalidatedJWTTokens.Store(token, expiresAt)
  42. }
  43. func (m *memoryTokenManager) Get(token string) bool {
  44. _, ok := m.invalidatedJWTTokens.Load(token)
  45. return ok
  46. }
  47. func (m *memoryTokenManager) Cleanup() {
  48. m.invalidatedJWTTokens.Range(func(key, value any) bool {
  49. exp, ok := value.(time.Time)
  50. if !ok || exp.Before(time.Now().UTC()) {
  51. m.invalidatedJWTTokens.Delete(key)
  52. }
  53. return true
  54. })
  55. }
  56. type dbTokenManager struct{}
  57. func (m *dbTokenManager) getKey(token string) string {
  58. digest := sha256.Sum256([]byte(token))
  59. return hex.EncodeToString(digest[:])
  60. }
  61. func (m *dbTokenManager) Add(token string, expiresAt time.Time) {
  62. key := m.getKey(token)
  63. data := map[string]string{
  64. "jwt": token,
  65. }
  66. session := dataprovider.Session{
  67. Key: key,
  68. Data: data,
  69. Type: dataprovider.SessionTypeInvalidToken,
  70. Timestamp: util.GetTimeAsMsSinceEpoch(expiresAt),
  71. }
  72. dataprovider.AddSharedSession(session) //nolint:errcheck
  73. }
  74. func (m *dbTokenManager) Get(token string) bool {
  75. key := m.getKey(token)
  76. _, err := dataprovider.GetSharedSession(key)
  77. return err == nil
  78. }
  79. func (m *dbTokenManager) Cleanup() {
  80. dataprovider.CleanupSharedSessions(dataprovider.SessionTypeInvalidToken, time.Now()) //nolint:errcheck
  81. }