|
5 | 5 | package oidc
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "strings" |
9 | 8 | "testing"
|
10 | 9 | "time"
|
11 | 10 |
|
12 | 11 | "github.com/golang-jwt/jwt/v5"
|
13 | 12 | "github.com/stretchr/testify/require"
|
14 | 13 | )
|
15 | 14 |
|
16 |
| -func Test_Encode(t *testing.T) { |
17 |
| - stateJWT := NewStateJWT([]byte("ANY KEY")) |
18 |
| - encodedState, err := stateJWT.Encode(StateClaims{ |
19 |
| - ClientConfigID: "test-id", |
20 |
| - ReturnToURL: "test-url", |
21 |
| - }) |
22 |
| - require.NoError(t, err) |
23 |
| - // check for header: { "alg": "HS256", "typ": "JWT" } |
24 |
| - require.Contains(t, encodedState, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.", "") |
25 |
| -} |
26 |
| - |
27 |
| -func Test_Decode(t *testing.T) { |
28 |
| - |
29 |
| - testCases := []struct { |
30 |
| - Label string |
31 |
| - Key4Encode string |
32 |
| - expiresIn time.Duration |
33 |
| - Key4Decode string |
34 |
| - ExpectedError string |
35 |
| - }{ |
36 |
| - { |
37 |
| - Label: "happy path", |
38 |
| - Key4Encode: "ANY KEY", |
39 |
| - expiresIn: 5 * time.Minute, |
40 |
| - Key4Decode: "ANY KEY", |
41 |
| - ExpectedError: "", |
42 |
| - }, |
43 |
| - { |
44 |
| - Label: "expired state token", |
45 |
| - Key4Encode: "ANY KEY", |
46 |
| - expiresIn: 0 * time.Second, |
47 |
| - Key4Decode: "ANY KEY", |
48 |
| - ExpectedError: "token is expired", |
| 15 | +func TestNewStateJWT(t *testing.T) { |
| 16 | + var ( |
| 17 | + clientConfigID = "test-id" |
| 18 | + returnURL = "test-url" |
| 19 | + issuedAt = time.Now() |
| 20 | + expiry = issuedAt.Add(5 * time.Minute) |
| 21 | + ) |
| 22 | + token := NewStateJWT(clientConfigID, returnURL, issuedAt, expiry) |
| 23 | + require.Equal(t, &StateClaims{ |
| 24 | + ClientConfigID: clientConfigID, |
| 25 | + ReturnToURL: returnURL, |
| 26 | + RegisteredClaims: jwt.RegisteredClaims{ |
| 27 | + ExpiresAt: jwt.NewNumericDate(expiry), |
| 28 | + IssuedAt: jwt.NewNumericDate(issuedAt), |
49 | 29 | },
|
50 |
| - { |
51 |
| - Label: "signature is invalid", |
52 |
| - Key4Encode: "OTHER KEY", |
53 |
| - expiresIn: 5 * time.Minute, |
54 |
| - Key4Decode: "ANY KEY", |
55 |
| - ExpectedError: jwt.ErrSignatureInvalid.Error(), |
56 |
| - }, |
57 |
| - } |
58 |
| - |
59 |
| - for _, tc := range testCases { |
60 |
| - t.Run(tc.Label, func(t *testing.T) { |
61 |
| - encoder := newTestStateJWT([]byte(tc.Key4Encode), tc.expiresIn) |
62 |
| - decoder := NewStateJWT([]byte(tc.Key4Decode)) |
63 |
| - encodedState, err := encoder.Encode(StateClaims{ |
64 |
| - ClientConfigID: "test-id", |
65 |
| - ReturnToURL: "test-url", |
66 |
| - }) |
67 |
| - if err != nil && tc.ExpectedError == "" { |
68 |
| - require.FailNowf(t, "Unexpected error on `Encode`.", "Error: %", err) |
69 |
| - } |
70 |
| - _, err = decoder.Decode(encodedState) |
71 |
| - if err != nil && tc.ExpectedError == "" { |
72 |
| - require.FailNowf(t, "Unexpected error on `Decode`.", "Error: %", err) |
73 |
| - } |
74 |
| - if err != nil && !strings.Contains(err.Error(), tc.ExpectedError) { |
75 |
| - require.FailNowf(t, "Unmatched error.", "Got error: %", err.Error()) |
76 |
| - } |
77 |
| - }) |
78 |
| - } |
79 |
| - |
| 30 | + }, token.Claims) |
80 | 31 | }
|
0 commit comments