-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
38 lines (29 loc) · 911 Bytes
/
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
package utils
import (
"net/http"
"strings"
"time"
"github.com/Gavazn/Gavazn/config"
"github.com/dgrijalva/jwt-go"
)
const expireDuration = time.Hour * 72
// CreateToken creating token
func CreateToken(id string) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["jti"] = id
claims["exp"] = time.Now().Add(expireDuration).Unix()
return token.SignedString([]byte(config.Get("SECRET_KEY")))
}
// GetToken get user token
func GetToken(req *http.Request) string {
cleared := strings.Replace(req.Header.Get("Authorization"), " ", "", -1)
return strings.Replace(cleared, "Bearer", "", -1)
}
// ParseToken parse token from request
func ParseToken(req *http.Request) (*jwt.Token, error) {
tokenStr := GetToken(req)
return jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
return []byte(config.Get("SECRET_KEY")), nil
})
}