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

Function createTestJWT

credentials/jwt/token_file_call_creds_ext_test.go:220–254  ·  view source on GitHub ↗

createTestJWT creates a test JWT token with the specified expiration.

(t *testing.T, expiration time.Time)

Source from the content-addressed store, hash-verified

218
219// createTestJWT creates a test JWT token with the specified expiration.
220func createTestJWT(t *testing.T, expiration time.Time) string {
221 t.Helper()
222
223 claims := map[string]any{}
224 if !expiration.IsZero() {
225 claims["exp"] = expiration.Unix()
226 }
227
228 header := map[string]any{
229 "typ": "JWT",
230 "alg": "HS256",
231 }
232 headerBytes, err := json.Marshal(header)
233 if err != nil {
234 t.Fatalf("Failed to marshal header: %v", err)
235 }
236
237 claimsBytes, err := json.Marshal(claims)
238 if err != nil {
239 t.Fatalf("Failed to marshal claims: %v", err)
240 }
241
242 headerB64 := base64.URLEncoding.EncodeToString(headerBytes)
243 claimsB64 := base64.URLEncoding.EncodeToString(claimsBytes)
244
245 // Remove padding for URL-safe base64.
246 headerB64 = strings.TrimRight(headerB64, "=")
247 claimsB64 = strings.TrimRight(claimsB64, "=")
248
249 // For testing, we use a fake signature.
250 signature := base64.URLEncoding.EncodeToString([]byte("fake_signature"))
251 signature = strings.TrimRight(signature, "=")
252
253 return fmt.Sprintf("%s.%s.%s", headerB64, claimsB64, signature)
254}
255
256// writeTempTokenFile writes the token to a temporary file and returns the path.
257func writeTempTokenFile(t *testing.T, token string) string {

Calls 2

MarshalMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected