/
jwt.go
45 lines (36 loc) · 1.02 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
package swagger
import (
"fmt"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
)
// SecretKey : secret key for jwt
const SecretKey = "123qwe"
func ValidateToken(w http.ResponseWriter, r *http.Request) (*jwt.Token, bool) {
token, err := request.ParseFromRequest(r, request.AuthorizationHeaderExtractor,
func(token *jwt.Token) (interface{}, error) {
return []byte(SecretKey), nil
})
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "Unauthorized access to this resource")
return token, false
}
if !token.Valid {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "token is invalid")
return token, false
}
return token, true
}
func SignToken(userName string) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * time.Duration(1)).Unix()
claims["iat"] = time.Now().Unix()
claims["name"] = userName
token.Claims = claims
return token.SignedString([]byte(SecretKey))
}