webhook.go 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package webhook
  2. import (
  3. "net/http"
  4. "go.step.sm/linkedca"
  5. "github.com/smallstep/certificates/authority/provisioner"
  6. "github.com/smallstep/certificates/webhook"
  7. )
  8. type Controller struct {
  9. client *http.Client
  10. webhooks []*provisioner.Webhook
  11. }
  12. func New(webhooks []*provisioner.Webhook) (*Controller, error) {
  13. return &Controller{
  14. client: http.DefaultClient,
  15. webhooks: webhooks,
  16. }, nil
  17. }
  18. func (c *Controller) Validate(challenge string) error {
  19. if c == nil {
  20. return nil
  21. }
  22. for _, wh := range c.webhooks {
  23. if wh.Kind != linkedca.Webhook_SCEPCHALLENGE.String() {
  24. continue
  25. }
  26. if !c.isCertTypeOK(wh) {
  27. continue
  28. }
  29. req := &webhook.RequestBody{
  30. SCEPChallenge: challenge,
  31. }
  32. resp, err := wh.Do(c.client, req, nil) // TODO(hs): support templated URL?
  33. if err != nil {
  34. return err
  35. }
  36. if !resp.Allow {
  37. return provisioner.ErrWebhookDenied
  38. }
  39. }
  40. return nil
  41. }
  42. func (c *Controller) isCertTypeOK(wh *provisioner.Webhook) bool {
  43. return linkedca.Webhook_X509.String() == wh.CertType
  44. }