-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_framework.go
37 lines (34 loc) · 947 Bytes
/
web_framework.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
package jwt
import (
"errors"
"strings"
)
// GetJWTFromHeader get JWT from header which provided by any of web framework, but with rules JWT "token"
func GetJWTFromHeader(header string) (token string, err error) {
if header != "" {
splitAuthorization := strings.Split(header, " ")
if len(splitAuthorization) != 0 && len(splitAuthorization) == 2 {
if splitAuthorization[0] != "JWT" {
err = errors.New("unknown authorization type")
} else {
token = splitAuthorization[1]
}
} else {
err = errors.New("invalid authorization header")
}
} else {
err = errors.New("no authorization provided")
}
return token, err
}
// VerifyAndBinding is to verify and binding any given token
func VerifyAndBinding(model interface{}, t string) (bool, error) {
isValid, err := VerifyAndBindingJWT(&model, t)
if err != nil {
if !IsUseAuthorization() {
err = nil
isValid = !IsUseAuthorization()
}
}
return isValid, err
}