-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
44 lines (38 loc) · 1.03 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
package auth
import (
"errors"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
)
var ErrInvalidJwt = errors.New("invalid jwt")
func parseJwt(tokenString string, secret []byte, c *Claims) error {
_, err := jwt.ParseWithClaims(tokenString, c, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, ErrInvalidJwt
}
return secret, nil
})
if err != nil {
return fmt.Errorf("could not parse token: %w", err)
}
now := time.Now()
deadline := time.Unix(c.ExpiresAt, 0).In(time.UTC)
if deadline.Before(now) {
return ErrUnauthorized
}
return nil
}
//buildJwt builds a JWT token with custom claims
func buildJwt(id, username, fullName string, secret []byte, validity time.Duration, scope Scope) (string, error) {
ttl := time.Now().Add(validity)
c := newClaims()
defer returnClaims(c)
c.Id = id
c.Username = username
c.Name = fullName
c.Scope = scope
c.ExpiresAt = ttl.Unix()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, *c)
return token.SignedString(secret)
}