-
Notifications
You must be signed in to change notification settings - Fork 12
/
jwt.go
110 lines (92 loc) · 2.77 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package jwtbeego
import (
"crypto/rsa"
"errors"
"io/ioutil"
"log"
"github.com/dgrijalva/jwt-go"
)
// EasyToken is an Struct to encapsulate username and expires as parameter
type EasyToken struct {
// Username is the name of the user
Username string
// Expires is a timestamp with expiration date
Expires int64
}
// https://gist.github.com/cryptix/45c33ecf0ae54828e63b
// location of the files used for signing and verification
const (
privKeyPath = "keys/rsakey.pem" // openssl genrsa -out app.rsa keysize
pubKeyPath = "keys/rsakey.pem.pub" // openssl rsa -in app.rsa -pubout > app.rsa.pub
)
var (
verifyKey *rsa.PublicKey
mySigningKey *rsa.PrivateKey
)
func init() {
verifyBytes, err := ioutil.ReadFile(pubKeyPath)
if err != nil {
log.Fatal(err)
}
verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
if err != nil {
log.Fatal(err)
}
signBytes, err := ioutil.ReadFile(privKeyPath)
if err != nil {
log.Fatal(err)
}
mySigningKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
if err != nil {
log.Fatal(err)
}
}
// GetToken is a function that exposes the method to get a simple token for jwt
func (e EasyToken) GetToken() (string, error) {
// Create the Claims
claims := &jwt.StandardClaims{
ExpiresAt: e.Expires, //time.Unix(c.ExpiresAt, 0)
Issuer: e.Username,
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(mySigningKey)
if err != nil {
log.Fatal(err)
}
return tokenString, err
}
// ValidateToken get token strings and return if is valid or not
func (e EasyToken) ValidateToken(tokenString string) (bool, string, error) {
// Token from another example. This token is expired
//var tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c"
if tokenString == "" {
return false, "", errors.New("token is empty")
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return verifyKey, nil
})
if token == nil {
log.Println(err)
return false, "", errors.New("not work")
}
if token.Valid {
//"You look nice today"
claims, _ := token.Claims.(jwt.MapClaims)
//var user string = claims["username"].(string)
iss := claims["iss"].(string)
return true, iss, nil
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return false, "", errors.New("That's not even a token")
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
// Token is either expired or not active yet
return false, "", errors.New("Timing is everything")
} else {
//"Couldn't handle this token:"
return false, "", err
}
} else {
//"Couldn't handle this token:"
return false, "", err
}
}