forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
55 lines (50 loc) · 1.56 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
51
52
53
54
55
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package security
import (
"crypto/ecdsa"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
)
// BuildJWT builds a JSON Web Token for the given subject and ttl, and signs it with the given private key
func BuildJWT(subject string, ttl time.Duration, privateKey []byte) (token string, err error) {
claims := jwt.StandardClaims{
Issuer: subject,
Subject: subject,
IssuedAt: time.Now().Add(-20 * time.Second).Unix(),
NotBefore: time.Now().Add(-20 * time.Second).Unix(),
}
if ttl > 0 {
claims.ExpiresAt = time.Now().Add(ttl).Unix()
}
tokenBuilder := jwt.NewWithClaims(jwt.SigningMethodES256, claims)
var key *ecdsa.PrivateKey
key, err = jwt.ParseECPrivateKeyFromPEM(privateKey)
if err != nil {
return
}
token, err = tokenBuilder.SignedString(key)
if err != nil {
return
}
return
}
// ValidateJWT validates a JSON Web Token with the given public key
func ValidateJWT(token string, publicKey []byte) (*jwt.StandardClaims, error) {
claims := &jwt.StandardClaims{}
_, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("Unexpected JWT signing method: %v", token.Header["alg"])
}
key, err := jwt.ParseECPublicKeyFromPEM(publicKey)
if err != nil {
return nil, err
}
return key, nil
})
if err != nil {
return nil, fmt.Errorf("Unable to verify JWT: %s", err.Error())
}
return claims, nil
}