account.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package acme
  2. import (
  3. "crypto"
  4. "encoding/base64"
  5. "encoding/json"
  6. "go.step.sm/crypto/jose"
  7. )
  8. // Account is a subset of the internal account type containing only those
  9. // attributes required for responses in the ACME protocol.
  10. type Account struct {
  11. ID string `json:"-"`
  12. Key *jose.JSONWebKey `json:"-"`
  13. Contact []string `json:"contact,omitempty"`
  14. Status Status `json:"status"`
  15. OrdersURL string `json:"orders"`
  16. }
  17. // ToLog enables response logging.
  18. func (a *Account) ToLog() (interface{}, error) {
  19. b, err := json.Marshal(a)
  20. if err != nil {
  21. return nil, WrapErrorISE(err, "error marshaling account for logging")
  22. }
  23. return string(b), nil
  24. }
  25. // IsValid returns true if the Account is valid.
  26. func (a *Account) IsValid() bool {
  27. return Status(a.Status) == StatusValid
  28. }
  29. // KeyToID converts a JWK to a thumbprint.
  30. func KeyToID(jwk *jose.JSONWebKey) (string, error) {
  31. kid, err := jwk.Thumbprint(crypto.SHA256)
  32. if err != nil {
  33. return "", WrapErrorISE(err, "error generating jwk thumbprint")
  34. }
  35. return base64.RawURLEncoding.EncodeToString(kid), nil
  36. }