-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
46 lines (37 loc) · 1002 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
39
40
41
42
43
44
45
46
package jwt
import (
"errors"
"time"
jwt_lib "github.com/dgrijalva/jwt-go"
"github.com/shing1211/go-lib/common"
"gopkg.in/mgo.v2/bson"
)
// SdtClaims defines the custom claims
type SdtClaims struct {
Name string `json:"name"`
Role string `json:"role"`
jwt_lib.StandardClaims
}
type Utils struct {
}
// GenerateJWT generates token from the given information
func (u *Utils) GenerateJWT(name string, role string) (string, error) {
claims := SdtClaims{
name,
role,
jwt_lib.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * 1).Unix(),
Issuer: common.Config.Issuer,
},
}
token := jwt_lib.NewWithClaims(jwt_lib.SigningMethodHS256, claims)
tokenString, err := token.SignedString([]byte(common.Config.JwtSecretPassword))
return tokenString, err
}
// ValidateObjectID checks the given ID if it's an object id or not
func (u *Utils) ValidateObjectID(id string) error {
if bson.IsObjectIdHex(id) != true {
return errors.New(common.ErrNotObjectIDHex)
}
return nil
}