-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
47 lines (40 loc) · 1.05 KB
/
session.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
package hyper
import (
"github.com/golang-jwt/jwt/v5"
"net/http"
"time"
)
type Session struct {
ApiKey string
JwtKey []byte
Client *http.Client
}
// NewSession creates a new Session that can be used to make requests to the Hyper Solutions API.
func NewSession(apiKey string) *Session {
return &Session{
ApiKey: apiKey,
Client: http.DefaultClient,
}
}
// WithJwtKey adds the JWT Key to the session. If not empty, a signature will be added to each request.
func (s *Session) WithJwtKey(jwt string) *Session {
s.JwtKey = []byte(jwt)
return s
}
// WithClient sets a new client that will be used to make requests to the Hyper Solutions API.
func (s *Session) WithClient(client *http.Client) *Session {
s.Client = client
return s
}
func (s *Session) generateSignature() (string, error) {
claims := jwt.MapClaims{
"key": s.ApiKey,
"exp": time.Now().Add(time.Minute).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, err := token.SignedString(s.JwtKey)
if err != nil {
return "", err
}
return signedToken, nil
}