dpop_options.go 1.1 KB

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