createTestJWT creates a test JWT token with the specified expiration.
(t *testing.T, expiration time.Time)
| 218 | |
| 219 | // createTestJWT creates a test JWT token with the specified expiration. |
| 220 | func 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. |
| 257 | func writeTempTokenFile(t *testing.T, token string) string { |
no test coverage detected