Skip to content

Commit 9eaf760

Browse files
committed
Update checkScope function
1 parent 4d98cf7 commit 9eaf760

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

01-Authorization-RS256/main.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,24 @@ type CustomClaims struct {
124124
}
125125

126126
func checkScope(scope string, tokenString string) bool {
127-
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, nil)
127+
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func (token *jwt.Token) (interface{}, error) {
128+
cert, err := getPemCert(token)
129+
if err != nil {
130+
return nil, err
131+
}
132+
result, _ := jwt.ParseRSAPublicKeyFromPEM([]byte(cert))
133+
return result, nil
134+
})
128135

129-
claims, _ := token.Claims.(*CustomClaims)
136+
claims, ok := token.Claims.(*CustomClaims)
130137

131138
hasScope := false
132-
result := strings.Split(claims.Scope, " ")
133-
for i := range result {
134-
if result[i] == scope {
135-
hasScope = true
139+
if ok && token.Valid {
140+
result := strings.Split(claims.Scope, " ")
141+
for i := range result {
142+
if result[i] == scope {
143+
hasScope = true
144+
}
136145
}
137146
}
138147

0 commit comments

Comments
 (0)