dpop_options.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package wire
  2. import (
  3. "bytes"
  4. "crypto"
  5. "errors"
  6. "fmt"
  7. "text/template"
  8. "go.step.sm/crypto/pemutil"
  9. )
  10. type DPOPOptions struct {
  11. // Public part of the signing key for DPoP access token in PEM format
  12. SigningKey []byte `json:"key"`
  13. // URI template for the URI the ACME client must call to fetch the DPoP challenge proof (an access token from wire-server)
  14. Target string `json:"target"`
  15. signingKey crypto.PublicKey
  16. target *template.Template
  17. }
  18. func (o *DPOPOptions) GetSigningKey() crypto.PublicKey {
  19. return o.signingKey
  20. }
  21. func (o *DPOPOptions) EvaluateTarget(deviceID string) (string, error) {
  22. if deviceID == "" {
  23. return "", errors.New("deviceID must not be empty")
  24. }
  25. buf := new(bytes.Buffer)
  26. if err := o.target.Execute(buf, struct{ DeviceID string }{DeviceID: deviceID}); err != nil {
  27. return "", fmt.Errorf("failed executing DPoP template: %w", err)
  28. }
  29. return buf.String(), nil
  30. }
  31. func (o *DPOPOptions) validateAndInitialize() (err error) {
  32. o.signingKey, err = pemutil.Parse(o.SigningKey)
  33. if err != nil {
  34. return fmt.Errorf("failed parsing key: %w", err)
  35. }
  36. o.target, err = template.New("DeviceID").Parse(o.Target)
  37. if err != nil {
  38. return fmt.Errorf("failed parsing DPoP template: %w", err)
  39. }
  40. return nil
  41. }