-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
58 lines (48 loc) · 1.35 KB
/
auth.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
package letsrest
import (
"github.com/dgrijalva/jwt-go"
"github.com/rs/xid"
"time"
)
// LetsRestClaims claims in terminology of jwt just a data that serialized in jwt token
type LetsRestClaims struct {
UserID string `json:"user_id"`
jwt.StandardClaims
}
type Auth struct {
UserID string `json:"user_id"`
AuthToken string `json:"auth_token"`
}
func createUser() *User {
return &User{
ID: xid.New().String(),
RequestLimit: 1000,
}
}
func createAuthToken(user *User) *Auth {
expDate := time.Now().Add(time.Hour * 24 * 355 * 10) // 10 years
token := jwt.NewWithClaims(jwt.SigningMethodHS256, LetsRestClaims{
UserID: user.ID,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expDate.Unix(),
IssuedAt: time.Now().Unix(),
Issuer: "LetsRest",
},
})
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString([]byte(secretForJwt))
Must(err, "token.SignedString([]byte(secretForJwt))")
return &Auth{UserID: user.ID, AuthToken: tokenString}
}
func userFromAuthToken(authToken string) (*User, error) {
token, err := jwt.ParseWithClaims(authToken, &LetsRestClaims{}, func(token *jwt.Token) (interface{}, error) {
return secretForJwt, nil
})
if err != nil {
return nil, err
}
claims, _ := token.Claims.(*LetsRestClaims)
//if !ok || !token.Valid {
//}
return &User{ID: claims.UserID}, nil
}