Skip to content

Instantly share code, notes, and snippets.

@triangletodd
Created February 7, 2019 23:29
Show Gist options
  • Save triangletodd/a940634dc0aeea6e72994d54eeb5785d to your computer and use it in GitHub Desktop.
Save triangletodd/a940634dc0aeea6e72994d54eeb5785d to your computer and use it in GitHub Desktop.
Golang Verify Slack
package middlewares
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"github.com/gin-gonic/gin"
)
const (
signingKey = "SLACK_SECRET_HERE"
)
// VerifySlackMiddleware is responsible for verifying the X-Slack-Signature
// in a request against our secret.
func VerifySlackMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
rTimestamp := c.Request.Header.Get("X-Slack-Request-Timestamp")
expectedSig := c.Request.Header.Get("X-Slack-Signature")
rawBody, err := c.GetRawData()
if err != nil {
c.AbortWithStatusJSON(502, gin.H{"text": "Unexpected error."})
}
sigBaseString := "v0:" + rTimestamp + ":" + string(rawBody)
h := hmac.New(sha256.New, []byte(signingKey))
h.Write([]byte(sigBaseString))
computedSig := "v0=" + hex.EncodeToString(h.Sum(nil))
if expectedSig != computedSig {
c.AbortWithStatusJSON(401, gin.H{"text": "You shall not pass."})
}
c.Next()
}
}
@cahyowhy
Copy link

did it work ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment