MCPcopy
hub / github.com/grpc/grpc-go / extractExpiration

Method extractExpiration

credentials/jwt/file_reader.go:91–118  ·  view source on GitHub ↗

extractExpiration parses the JWT token to extract the expiration time.

(token string)

Source from the content-addressed store, hash-verified

89
90// extractExpiration parses the JWT token to extract the expiration time.
91func (r *jwtFileReader) extractExpiration(token string) (time.Time, error) {
92 claimsRaw, ok := extractClaimsRaw(token)
93 if !ok {
94 return time.Time{}, fmt.Errorf("expected 3 parts in token")
95 }
96 payloadBytes, err := base64.RawURLEncoding.DecodeString(claimsRaw)
97 if err != nil {
98 return time.Time{}, fmt.Errorf("decode error: %v", err)
99 }
100
101 var claims jwtClaims
102 if err := json.Unmarshal(payloadBytes, &claims); err != nil {
103 return time.Time{}, fmt.Errorf("unmarshal error: %v", err)
104 }
105
106 if claims.Exp == 0 {
107 return time.Time{}, fmt.Errorf("no expiration claims")
108 }
109
110 expTime := time.Unix(claims.Exp, 0)
111
112 // Check if token is already expired.
113 if expTime.Before(time.Now()) {
114 return time.Time{}, fmt.Errorf("expired token")
115 }
116
117 return expTime, nil
118}

Callers 1

readTokenMethod · 0.95

Calls 4

extractClaimsRawFunction · 0.85
NowMethod · 0.80
ErrorfMethod · 0.65
UnmarshalMethod · 0.65

Tested by

no test coverage detected