Capstone/service/controllers/pco_auth_middleware.go

89 lines
2.5 KiB
Go
Raw Permalink Normal View History

package controllers
import (
2023-11-18 19:17:14 -05:00
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
2023-11-18 19:17:14 -05:00
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
"github.com/gin-gonic/gin"
2023-11-18 19:17:14 -05:00
"github.com/google/jsonapi"
)
const PCO_VALIDATE_HEADER = "X-PCO-Webhooks-Authenticity"
func ValidatePcoWebhook(c *gin.Context) {
//get remote version from header
remoteDigestStr := c.GetHeader(PCO_VALIDATE_HEADER)
if remoteDigestStr == "" {
log.Warnf("Request was sent with no %s header. Rejecting", PCO_VALIDATE_HEADER)
c.AbortWithStatus(401)
return
}
pcoSig := make([]byte, len(remoteDigestStr)/2)
_, err := hex.Decode(pcoSig, []byte(remoteDigestStr))
2023-11-18 19:17:14 -05:00
if err != nil {
log.WithError(err).Error("Failed to decode byte digest")
_ = c.AbortWithError(501, err)
return
}
//clone request body to harmlessly inspect the body
bodyCopy := bytes.NewBuffer([]byte{})
_, err = io.Copy(bodyCopy, c.Request.Body)
if err != nil {
log.WithError(err).Error("Failed to copy body while validating PCO webhook")
2023-11-18 19:17:14 -05:00
_ = c.AbortWithError(501, err)
return
}
body := bodyCopy.Bytes()
c.Request.Body = io.NopCloser(bytes.NewReader(body))
//Get secret
2023-11-18 19:17:14 -05:00
key, err := getAuthSecret(c, body)
if err != nil {
log.WithError(err).Error("Failed to find auth secret for event. It may not be setup")
_ = c.AbortWithError(501, err)
return
}
//Get HMAC
hmacSig := hmac.New(sha256.New, []byte(key))
hmacSig.Write(body)
if !hmac.Equal(hmacSig.Sum(nil), pcoSig) {
log.Warn("")
c.AbortWithStatus(401)
}
}
2023-11-18 19:17:14 -05:00
func getAuthSecret(c *gin.Context, body []byte) (string, error) {
userObjectId := userIdFromContext(c)
log.Debug(string(body))
2023-11-18 19:17:14 -05:00
//Pco is weird and sends a data array instead of an object. Yet there is only one event. Fun times
event, err := jsonapi.UnmarshalManyPayload[webhooks.EventDelivery](bytes.NewBuffer(body))
2023-11-18 19:17:14 -05:00
if err != nil {
return "", errors.Join(fmt.Errorf("Failed to unmarshall event delivery from PCO"), err)
2023-11-18 19:17:14 -05:00
}
if len(event) == 0 {
return "", fmt.Errorf("There are no events in the delivery. Something is wrong")
}
webhook, err := mongo.FindPcoSubscriptionForUser(*userObjectId, event[0].Name)
2023-11-18 19:17:14 -05:00
if err != nil {
return "", errors.Join(fmt.Errorf("Failed to find pco subscription for user: %s and event: %s", userObjectId.Hex(), event[0].Name), err)
}
if webhook == nil {
return "", fmt.Errorf("Could not find subscription for user: %s and name %s", userObjectId.Hex(), event[0].Name)
2023-11-18 19:17:14 -05:00
}
return webhook.Details.AuthenticitySecret, nil
}