apikey.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. var ErrInvalidApiKey = errors.New("Invalid API Key")
  7. var ErrInvalidApiKeyExpiration = errors.New("Negative value for SecondsToLive")
  8. type ApiKey struct {
  9. Id int64
  10. OrgId int64
  11. Name string
  12. Key string
  13. Role RoleType
  14. Created time.Time
  15. Updated time.Time
  16. Expires *int64
  17. }
  18. // ---------------------
  19. // COMMANDS
  20. type AddApiKeyCommand struct {
  21. Name string `json:"name" binding:"Required"`
  22. Role RoleType `json:"role" binding:"Required"`
  23. OrgId int64 `json:"-"`
  24. Key string `json:"-"`
  25. SecondsToLive int64 `json:"secondsToLive"`
  26. Result *ApiKey `json:"-"`
  27. }
  28. type UpdateApiKeyCommand struct {
  29. Id int64 `json:"id"`
  30. Name string `json:"name"`
  31. Role RoleType `json:"role"`
  32. OrgId int64 `json:"-"`
  33. }
  34. type DeleteApiKeyCommand struct {
  35. Id int64 `json:"id"`
  36. OrgId int64 `json:"-"`
  37. }
  38. // ----------------------
  39. // QUERIES
  40. type GetApiKeysQuery struct {
  41. OrgId int64
  42. IncludeInvalid bool
  43. Result []*ApiKey
  44. }
  45. type GetApiKeyByNameQuery struct {
  46. KeyName string
  47. OrgId int64
  48. Result *ApiKey
  49. }
  50. type GetApiKeyByIdQuery struct {
  51. ApiKeyId int64
  52. Result *ApiKey
  53. }
  54. // ------------------------
  55. // DTO & Projections
  56. type ApiKeyDTO struct {
  57. Id int64 `json:"id"`
  58. Name string `json:"name"`
  59. Role RoleType `json:"role"`
  60. Expiration *time.Time `json:"expiration,omitempty"`
  61. }