-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_auth.go
41 lines (36 loc) · 957 Bytes
/
custom_auth.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
package authparse
import (
"github.com/golang-jwt/jwt/v5"
"github.com/waro163/auth-parse/utils"
gmw "github.com/waro163/gin-middleware"
)
const (
ACCESS_TOKEN_SUBJECT = "access_token"
REFRESH_TOKEN_SUBJECT = "refresh_token"
)
var (
_ gmw.IAuthenticator = (*CustomJwtAuthenticator)(nil)
)
type CustomJwtAuthenticator struct {
Audience string
Issuer string
Subject string
JwtParse utils.IJwtParser
}
func (auth *CustomJwtAuthenticator) Authenticate(token string) (interface{}, *gmw.Error) {
if token == "" {
return nil, &gmw.Error{
Code: -1,
Message: "missing authorization token",
}
}
claims := MyCustomClaims{}
_, err := auth.JwtParse.ParseJwtToken(token, &claims, jwt.WithAudience(auth.Audience), jwt.WithIssuer(auth.Issuer), jwt.WithIssuedAt(), jwt.WithExpirationRequired(), jwt.WithSubject(auth.Subject))
if err != nil {
return nil, &gmw.Error{
Code: -2,
Message: err.Error(),
}
}
return claims, nil
}