-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
38 lines (31 loc) · 941 Bytes
/
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
package fonbnk
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"net/http"
"time"
)
func (fc *FonbnkClient) setAuthHeaders(req *http.Request) error {
timestamp := fmt.Sprintf("%d", time.Now().UnixMilli())
signature, err := generateSignature(fc.clientSecret, timestamp, req.URL.RequestURI())
log.Println(req.URL.RequestURI())
if err != nil {
return err
}
req.Header.Set("x-client-id", fc.clientID)
req.Header.Set("x-timestamp", timestamp)
req.Header.Set("x-signature", signature)
return nil
}
func generateSignature(clientSecret string, timestamp string, endpoint string) (string, error) {
decodedSecret, err := base64.RawStdEncoding.DecodeString(clientSecret)
if err != nil {
return "", fmt.Errorf("failed to decode client secret: %v", err)
}
h := hmac.New(sha256.New, decodedSecret)
h.Write([]byte(timestamp + ":" + endpoint))
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}