-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
44 lines (35 loc) · 986 Bytes
/
admin.go
File metadata and controls
44 lines (35 loc) · 986 Bytes
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
package admin
import (
"context"
"sync"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/spaceuptech/helpers"
)
// Module stores admin module information
type Module struct {
lock sync.RWMutex
secret string
}
// New initializes admin module
func New(secret string) *Module {
return &Module{secret: secret}
}
// CreateToken create jwt token
func (m *Module) CreateToken(ctx context.Context, tokenClaims map[string]interface{}) (string, error) {
m.lock.RLock()
defer m.lock.RUnlock()
claims := jwt.MapClaims{}
for k, v := range tokenClaims {
claims[k] = v
}
// Add expiry of one week
claims["exp"] = time.Now().Add(10 * time.Minute).Unix()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token.Header["kid"] = "sc-admin-kid"
tokenString, err := token.SignedString([]byte(m.secret))
if err != nil {
return "", helpers.Logger.LogError(helpers.GetRequestID(ctx), "Cannot sign token with the given secret", err, nil)
}
return tokenString, nil
}