forked from webix-hub/chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
50 lines (41 loc) · 1.17 KB
/
jwt.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
package main
import (
"crypto/ed25519"
"github.com/pascaldekloe/jwt"
"fmt"
"time"
)
var JWTPrivateKey ed25519.PrivateKey
var JWTPublicKey ed25519.PublicKey
func init() {
JWTPrivateKey = ed25519.NewKeyFromSeed([]byte("eyJhbGciOiJFUzI1NiJ9OiJFUzI1NiJ9"))
JWTPublicKey = []byte(JWTPrivateKey)[32:]
}
func createUserToken(id int, device int64) ([]byte, error) {
var claims jwt.Claims
claims.Subject = "user"
claims.Expires = jwt.NewNumericTime(time.Now().Add(8 * time.Hour).Round(time.Second))
claims.Set = map[string]interface{}{"id": id, "device": device}
return claims.EdDSASign(JWTPrivateKey)
}
func verifyUserToken(token []byte) (int, int, error) {
claims, err := jwt.EdDSACheck(token, JWTPublicKey)
if err != nil {
return 0, 0, err
}
if !claims.Valid(time.Now()) {
return 0, 0, fmt.Errorf("credential time constraints exceeded")
}
if claims.Subject != "user" {
return 0, 0, fmt.Errorf("wrong claims subject")
}
id, ok := claims.Set["id"].(float64)
if !ok {
return 0, 0, fmt.Errorf("wrong data in the token")
}
device, ok := claims.Set["device"].(float64)
if !ok {
return 0, 0, fmt.Errorf("wrong data in the token")
}
return int(id), int(device), nil
}