authz.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/acme"
  8. "github.com/smallstep/nosql"
  9. )
  10. // dbAuthz is the base authz type that others build from.
  11. type dbAuthz struct {
  12. ID string `json:"id"`
  13. AccountID string `json:"accountID"`
  14. Identifier acme.Identifier `json:"identifier"`
  15. Status acme.Status `json:"status"`
  16. Token string `json:"token"`
  17. ChallengeIDs []string `json:"challengeIDs"`
  18. Wildcard bool `json:"wildcard"`
  19. CreatedAt time.Time `json:"createdAt"`
  20. ExpiresAt time.Time `json:"expiresAt"`
  21. Error *acme.Error `json:"error"`
  22. }
  23. func (ba *dbAuthz) clone() *dbAuthz {
  24. u := *ba
  25. return &u
  26. }
  27. // getDBAuthz retrieves and unmarshals a database representation of the
  28. // ACME Authorization type.
  29. func (db *DB) getDBAuthz(ctx context.Context, id string) (*dbAuthz, error) {
  30. data, err := db.db.Get(authzTable, []byte(id))
  31. if nosql.IsErrNotFound(err) {
  32. return nil, acme.NewError(acme.ErrorMalformedType, "authz %s not found", id)
  33. } else if err != nil {
  34. return nil, errors.Wrapf(err, "error loading authz %s", id)
  35. }
  36. var dbaz dbAuthz
  37. if err = json.Unmarshal(data, &dbaz); err != nil {
  38. return nil, errors.Wrapf(err, "error unmarshaling authz %s into dbAuthz", id)
  39. }
  40. return &dbaz, nil
  41. }
  42. // GetAuthorization retrieves and unmarshals an ACME authz type from the database.
  43. // Implements acme.DB GetAuthorization interface.
  44. func (db *DB) GetAuthorization(ctx context.Context, id string) (*acme.Authorization, error) {
  45. dbaz, err := db.getDBAuthz(ctx, id)
  46. if err != nil {
  47. return nil, err
  48. }
  49. var chs = make([]*acme.Challenge, len(dbaz.ChallengeIDs))
  50. for i, chID := range dbaz.ChallengeIDs {
  51. chs[i], err = db.GetChallenge(ctx, chID, id)
  52. if err != nil {
  53. return nil, err
  54. }
  55. }
  56. return &acme.Authorization{
  57. ID: dbaz.ID,
  58. AccountID: dbaz.AccountID,
  59. Identifier: dbaz.Identifier,
  60. Status: dbaz.Status,
  61. Challenges: chs,
  62. Wildcard: dbaz.Wildcard,
  63. ExpiresAt: dbaz.ExpiresAt,
  64. Token: dbaz.Token,
  65. Error: dbaz.Error,
  66. }, nil
  67. }
  68. // CreateAuthorization creates an entry in the database for the Authorization.
  69. // Implements the acme.DB.CreateAuthorization interface.
  70. func (db *DB) CreateAuthorization(ctx context.Context, az *acme.Authorization) error {
  71. var err error
  72. az.ID, err = randID()
  73. if err != nil {
  74. return err
  75. }
  76. chIDs := make([]string, len(az.Challenges))
  77. for i, ch := range az.Challenges {
  78. chIDs[i] = ch.ID
  79. }
  80. now := clock.Now()
  81. dbaz := &dbAuthz{
  82. ID: az.ID,
  83. AccountID: az.AccountID,
  84. Status: az.Status,
  85. CreatedAt: now,
  86. ExpiresAt: az.ExpiresAt,
  87. Identifier: az.Identifier,
  88. ChallengeIDs: chIDs,
  89. Token: az.Token,
  90. Wildcard: az.Wildcard,
  91. }
  92. return db.save(ctx, az.ID, dbaz, nil, "authz", authzTable)
  93. }
  94. // UpdateAuthorization saves an updated ACME Authorization to the database.
  95. func (db *DB) UpdateAuthorization(ctx context.Context, az *acme.Authorization) error {
  96. old, err := db.getDBAuthz(ctx, az.ID)
  97. if err != nil {
  98. return err
  99. }
  100. nu := old.clone()
  101. nu.Status = az.Status
  102. nu.Error = az.Error
  103. return db.save(ctx, old.ID, nu, old, "authz", authzTable)
  104. }