-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2m_auth.go
49 lines (44 loc) · 1.05 KB
/
m2m_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
42
43
44
45
46
47
48
49
package authparse
import (
"github.com/waro163/auth-parse/utils"
"github.com/golang-jwt/jwt/v5"
gmw "github.com/waro163/gin-middleware"
)
var (
_ gmw.IAuthenticator = (*M2mJwtAuthenticator)(nil)
)
type M2mJwtAuthenticator struct {
Audience string
Issuer string
Scope string
JwtParse utils.IJwtParser
}
func (auth *M2mJwtAuthenticator) Authenticate(token string) (interface{}, *gmw.Error) {
if token == "" {
return nil, &gmw.Error{
Code: -1,
Message: "missing authorization token",
}
}
parseToken, err := auth.JwtParse.ParseJwtToken(token, &M2mClaims{}, jwt.WithIssuer(auth.Issuer), jwt.WithIssuedAt(), jwt.WithAudience(auth.Audience), jwt.WithExpirationRequired())
if err != nil {
return nil, &gmw.Error{
Code: -2,
Message: err.Error(),
}
}
claims, ok := parseToken.Claims.(*M2mClaims)
if !ok {
return nil, &gmw.Error{
Code: -3,
Message: "invalid claims",
}
}
if auth.Scope != "" && claims.Scp != auth.Scope {
return nil, &gmw.Error{
Code: -4,
Message: "invalid scp",
}
}
return claims, nil
}