apikey.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. var ErrDuplicateApiKey = errors.New("API Key Organization ID And Name Must Be Unique")
  9. type ApiKey struct {
  10. Id int64
  11. OrgId int64
  12. Name string
  13. Key string
  14. Role RoleType
  15. Created time.Time
  16. Updated time.Time
  17. Expires *int64
  18. }
  19. // ---------------------
  20. // COMMANDS
  21. type AddApiKeyCommand struct {
  22. Name string `json:"name" binding:"Required"`
  23. Role RoleType `json:"role" binding:"Required"`
  24. OrgId int64 `json:"-"`
  25. Key string `json:"-"`
  26. SecondsToLive int64 `json:"secondsToLive"`
  27. Result *ApiKey `json:"-"`
  28. }
  29. type UpdateApiKeyCommand struct {
  30. Id int64 `json:"id"`
  31. Name string `json:"name"`
  32. Role RoleType `json:"role"`
  33. OrgId int64 `json:"-"`
  34. }
  35. type DeleteApiKeyCommand struct {
  36. Id int64 `json:"id"`
  37. OrgId int64 `json:"-"`
  38. }
  39. // ----------------------
  40. // QUERIES
  41. type GetApiKeysQuery struct {
  42. OrgId int64
  43. IncludeInvalid bool
  44. Result []*ApiKey
  45. }
  46. type GetApiKeyByNameQuery struct {
  47. KeyName string
  48. OrgId int64
  49. Result *ApiKey
  50. }
  51. type GetApiKeyByIdQuery struct {
  52. ApiKeyId int64
  53. Result *ApiKey
  54. }
  55. // ------------------------
  56. // DTO & Projections
  57. type ApiKeyDTO struct {
  58. Id int64 `json:"id"`
  59. Name string `json:"name"`
  60. Role RoleType `json:"role"`
  61. Expiration *time.Time `json:"expiration,omitempty"`
  62. }