| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "crypto/hmac" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "hash" |
| 9 | "net/http" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // Signature headers |
| 16 | const ( |
| 17 | hSignature = "X-Slack-Signature" |
| 18 | hTimestamp = "X-Slack-Request-Timestamp" |
| 19 | ) |
| 20 | |
| 21 | // SecretsVerifier contains the information needed to verify that the request comes from Slack |
| 22 | type SecretsVerifier struct { |
| 23 | d Debug |
| 24 | signature []byte |
| 25 | hmac hash.Hash |
| 26 | } |
| 27 | |
| 28 | func unsafeSignatureVerifier(header http.Header, secret string) (_ SecretsVerifier, err error) { |
| 29 | var ( |
| 30 | bsignature []byte |
| 31 | ) |
| 32 | |
| 33 | signature := header.Get(hSignature) |
| 34 | stimestamp := header.Get(hTimestamp) |
| 35 | |
| 36 | if signature == "" || stimestamp == "" { |
| 37 | return SecretsVerifier{}, ErrMissingHeaders |
| 38 | } |
| 39 | |
| 40 | if bsignature, err = hex.DecodeString(strings.TrimPrefix(signature, "v0=")); err != nil { |
| 41 | return SecretsVerifier{}, err |
| 42 | } |
| 43 | |
| 44 | hash := hmac.New(sha256.New, []byte(secret)) |
| 45 | if _, err = hash.Write([]byte(fmt.Sprintf("v0:%s:", stimestamp))); err != nil { |
| 46 | return SecretsVerifier{}, err |
| 47 | } |
| 48 | |
| 49 | return SecretsVerifier{ |
| 50 | signature: bsignature, |
| 51 | hmac: hash, |
| 52 | }, nil |
| 53 | } |
| 54 | |
| 55 | // NewSecretsVerifier returns a SecretsVerifier object in exchange for an http.Header object and signing secret |
| 56 | func NewSecretsVerifier(header http.Header, secret string) (sv SecretsVerifier, err error) { |
| 57 | var ( |
| 58 | timestamp int64 |
| 59 | ) |
| 60 | |
| 61 | stimestamp := header.Get(hTimestamp) |
| 62 | |
| 63 | if sv, err = unsafeSignatureVerifier(header, secret); err != nil { |
| 64 | return SecretsVerifier{}, err |
| 65 | } |
| 66 | |
| 67 | if timestamp, err = strconv.ParseInt(stimestamp, 10, 64); err != nil { |
| 68 | return SecretsVerifier{}, err |
| 69 | } |
| 70 | |
| 71 | diff := absDuration(time.Since(time.Unix(timestamp, 0))) |
| 72 | if diff > 5*time.Minute { |
| 73 | return SecretsVerifier{}, ErrExpiredTimestamp |
| 74 | } |
| 75 | |
| 76 | return sv, err |
| 77 | } |
| 78 | |
| 79 | func (v *SecretsVerifier) WithDebug(d Debug) *SecretsVerifier { |
| 80 | v.d = d |
| 81 | return v |
| 82 | } |
| 83 | |
| 84 | func (v *SecretsVerifier) Write(body []byte) (n int, err error) { |
| 85 | return v.hmac.Write(body) |
| 86 | } |
| 87 | |
| 88 | // Ensure compares the signature sent from Slack with the actual computed hash to judge validity |
| 89 | func (v SecretsVerifier) Ensure() error { |
| 90 | computed := v.hmac.Sum(nil) |
| 91 | // use hmac.Equal prevent leaking timing information. |
| 92 | if hmac.Equal(computed, v.signature) { |
| 93 | return nil |
| 94 | } |
| 95 | if v.d != nil && v.d.Debug() { |
| 96 | v.d.Debugln(fmt.Sprintf("Expected signing signature: %s, but computed: %s", hex.EncodeToString(v.signature), hex.EncodeToString(computed))) |
| 97 | } |
| 98 | return fmt.Errorf("Computed unexpected signature of: %s", hex.EncodeToString(computed)) |
| 99 | } |
| 100 | |
| 101 | func abs64(n int64) int64 { |
| 102 | y := n >> 63 |
| 103 | return (n ^ y) - y |
| 104 | } |
| 105 | |
| 106 | func absDuration(n time.Duration) time.Duration { |
| 107 | return time.Duration(abs64(int64(n))) |
| 108 | } |