-
Notifications
You must be signed in to change notification settings - Fork 8
/
auth.go
48 lines (40 loc) · 1.21 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package houndify
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"strings"
"time"
)
type authInfo struct {
houndClientAuth string
houndRequestAuth string
timeStamp int64
}
func generateAuthValues(clientID, clientKey, userID, requestID string) (
houndClientAuth, houndRequestAuth string, timeStamp int64, returnErr error) {
timeStamp = time.Now().Unix()
// base64 decode key
decodedClientKey, err := base64.StdEncoding.DecodeString(unescapeBase64Url(clientKey))
if err != nil {
fmt.Println(err)
returnErr = errors.New("failed to decode client key")
return
}
// sign
hmac := hmac.New(sha256.New, decodedClientKey)
hmac.Write([]byte(userID + ";" + requestID + fmt.Sprintf("%d", timeStamp)))
signature := escapeBase64Url(base64.StdEncoding.EncodeToString([]byte(hmac.Sum(nil))))
houndClientAuth = fmt.Sprintf("%s;%d;%s", clientID, timeStamp, signature)
houndRequestAuth = userID + ";" + requestID
returnErr = nil
return
}
func unescapeBase64Url(input string) string {
return strings.Replace(strings.Replace(input, "-", "+", -1), "_", "/", -1)
}
func escapeBase64Url(input string) string {
return strings.Replace(strings.Replace(input, "+", "-", -1), "/", "_", -1)
}