MCPcopy
hub / github.com/spacecloud-io/space-cloud / VerifyToken

Method VerifyToken

runner/utils/auth/admin.go:10–42  ·  view source on GitHub ↗

VerifyToken checks if the token is valid and returns the token claims

(token string)

Source from the content-addressed store, hash-verified

8
9// VerifyToken checks if the token is valid and returns the token claims
10func (m *Module) VerifyToken(token string) (map[string]interface{}, error) {
11 m.lock.RLock()
12 defer m.lock.RUnlock()
13 if m.config.IsDev {
14 return nil, nil
15 }
16 // Parse the JWT token
17 tokenObj, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
18 // Don't forget to validate the alg is what you expect it to be
19 if token.Method.Alg() != jwt.SigningMethodHS256.Alg() {
20 return nil, errors.New("invalid signing method")
21 }
22
23 // Return the key
24 return []byte(m.config.Secret), nil
25 })
26 if err != nil {
27 return nil, err
28 }
29
30 // Get the claims
31 if claims, ok := tokenObj.Claims.(jwt.MapClaims); ok && tokenObj.Valid {
32 tokenClaims := make(map[string]interface{}, len(claims))
33 for key, val := range claims {
34 tokenClaims[key] = val
35 }
36
37 return tokenClaims, nil
38 }
39
40 return nil, errors.New("token could not be verified")
41
42}

Callers 15

HandleSetServiceRoleMethod · 0.80
handleCreateProjectMethod · 0.80
handleDeleteProjectMethod · 0.80
handleApplyServiceMethod · 0.80
HandleDeleteServiceMethod · 0.80
HandleGetServicesMethod · 0.80
handleGetClusterTypeMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected