-
Notifications
You must be signed in to change notification settings - Fork 3
/
jwtmaker.go
65 lines (54 loc) · 1.36 KB
/
jwtmaker.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* ------------ License ------------ */
// BSD 3-Clause License
//
// Copyright (c) 2021, Seongmin Kim
// All rights reserved.
/* --------------------------------- */
package jwtmaker
import (
"crypto/sha512"
"encoding/hex"
"github.com/dgrijalva/jwt-go"
uuid "github.com/satori/go.uuid"
"log"
"net/url"
)
type Keys struct {
Secret []byte `json:"secret"`
Access string `json:"access"`
}
type KeySet []Keys
func MakeJwtWithoutPayload(keys Keys) string {
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["access_key"] = keys.Access
u := uuid.NewV4()
claims["nonce"] = u.String()
token.Claims = claims
signedToken, err := token.SignedString(keys.Secret)
if err != nil {
log.Println("SignedString Error")
log.Fatal(err)
return ""
}
return signedToken
}
func MakeJwtWithPayload(keys Keys, payload url.Values) string {
claims := make(jwt.MapClaims)
claims["access_key"] = keys.Access
claims["nonce"] = uuid.NewV4().String()
qh := sha512.New()
qh.Reset()
qh.Write([]byte(payload.Encode()))
claims["query_hash"] = hex.EncodeToString(qh.Sum(nil))
claims["query_hash_alg"] = "SHA512"
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, err := token.SignedString(keys.Secret)
//log.Println(signedToken)
if err != nil {
log.Println("SignedString Error")
log.Fatal(err)
return ""
}
return signedToken
}