session.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 dataprovider
  15. import (
  16. "errors"
  17. "fmt"
  18. )
  19. // SessionType defines the supported session types
  20. type SessionType int
  21. // Supported session types
  22. const (
  23. SessionTypeOIDCAuth SessionType = iota + 1
  24. SessionTypeOIDCToken
  25. SessionTypeResetCode
  26. )
  27. // Session defines a shared session persisted in the data provider
  28. type Session struct {
  29. Key string
  30. Data any
  31. Type SessionType
  32. Timestamp int64
  33. }
  34. func (s *Session) validate() error {
  35. if s.Key == "" {
  36. return errors.New("unable to save a session with an empty key")
  37. }
  38. if s.Type < SessionTypeOIDCAuth || s.Type > SessionTypeResetCode {
  39. return fmt.Errorf("invalid session type: %v", s.Type)
  40. }
  41. return nil
  42. }