|
1 | 1 | // |
2 | | -// Copyright 2023 The Chainloop Authors. |
| 2 | +// Copyright 2023-2026 The Chainloop Authors. |
3 | 3 | // |
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | // you may not use this file except in compliance with the License. |
@@ -94,3 +94,30 @@ func TestGenerateJWT(t *testing.T) { |
94 | 94 | assert.Contains(t, claims.Audience, Audience) |
95 | 95 | assert.WithinDuration(t, time.Now(), claims.ExpiresAt.Time, 10*time.Second) |
96 | 96 | } |
| 97 | + |
| 98 | +func TestGenerateJWTWithCustomAudience(t *testing.T) { |
| 99 | + const hmacSecret = "my-secret" |
| 100 | + const customAudience = "mcp-user-auth.chainloop" |
| 101 | + |
| 102 | + b, err := NewBuilder( |
| 103 | + WithIssuer("my-issuer"), |
| 104 | + WithKeySecret(hmacSecret), |
| 105 | + WithExpiration(10*time.Second), |
| 106 | + WithAudience(customAudience), |
| 107 | + ) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + token, err := b.GenerateJWT("user-id") |
| 111 | + require.NoError(t, err) |
| 112 | + assert.NotEmpty(t, token) |
| 113 | + |
| 114 | + claims := &CustomClaims{} |
| 115 | + tokenInfo, err := jwt.ParseWithClaims(token, claims, func(_ *jwt.Token) (interface{}, error) { |
| 116 | + return []byte(hmacSecret), nil |
| 117 | + }, jwt.WithValidMethods([]string{SigningMethod.Alg()})) |
| 118 | + |
| 119 | + require.NoError(t, err) |
| 120 | + assert.True(t, tokenInfo.Valid) |
| 121 | + assert.Equal(t, "user-id", claims.UserID) |
| 122 | + assert.Equal(t, jwt.ClaimStrings{customAudience}, claims.Audience) |
| 123 | +} |
0 commit comments