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

Function createTestJWT

credentials/jwt/token_file_call_creds_test.go:511–545  ·  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

509
510// createTestJWT creates a test JWT token with the specified expiration.
511func createTestJWT(t *testing.T, expiration time.Time) string {
512 t.Helper()
513
514 claims := map[string]any{}
515 if !expiration.IsZero() {
516 claims["exp"] = expiration.Unix()
517 }
518
519 header := map[string]any{
520 "typ": "JWT",
521 "alg": "HS256",
522 }
523 headerBytes, err := json.Marshal(header)
524 if err != nil {
525 t.Fatalf("Failed to marshal header: %v", err)
526 }
527
528 claimsBytes, err := json.Marshal(claims)
529 if err != nil {
530 t.Fatalf("Failed to marshal claims: %v", err)
531 }
532
533 headerB64 := base64.URLEncoding.EncodeToString(headerBytes)
534 claimsB64 := base64.URLEncoding.EncodeToString(claimsBytes)
535
536 // Remove padding for URL-safe base64
537 headerB64 = strings.TrimRight(headerB64, "=")
538 claimsB64 = strings.TrimRight(claimsB64, "=")
539
540 // For testing, we'll use a fake signature
541 signature := base64.URLEncoding.EncodeToString([]byte("fake_signature"))
542 signature = strings.TrimRight(signature, "=")
543
544 return fmt.Sprintf("%s.%s.%s", headerB64, claimsB64, signature)
545}
546
547func writeTempFile(t *testing.T, name, content string) string {
548 t.Helper()

Calls 2

MarshalMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected