1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package sessions
import (
"net/http"
"testing"
"github.com/oauth2-proxy/mockoidc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xlgmokha/x/pkg/test"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web"
)
func TestService(t *testing.T) {
srv := oidc.NewTestServer(t)
defer srv.Close()
clientID := srv.MockOIDC.Config().ClientID
clientSecret := srv.MockOIDC.Config().ClientSecret
cfg, err := oidc.New(
t.Context(),
srv.Issuer(),
clientID,
clientSecret,
"/session/callback",
)
require.NoError(t, err)
svc := NewService(cfg, http.DefaultClient)
t.Run("Exchange", func(t *testing.T) {
t.Run("when the csrf token is missing", func(t *testing.T) {
r := test.Request("GET", "/session/callback")
tokens, err := svc.Exchange(r)
require.Error(t, err)
assert.Nil(t, tokens)
})
t.Run("when the csrf token is invalid", func(t *testing.T) {
user := mockoidc.DefaultUser()
code := srv.CreateAuthorizationCodeFor(user)
nonce := pls.GenerateRandomHex(32)
r := test.Request(
"GET",
"/session/callback?code="+code+"&state=invalid",
test.WithCookie(web.NewCookie("oauth_state", nonce)),
)
tokens, err := svc.Exchange(r)
require.Error(t, err)
assert.Nil(t, tokens)
})
t.Run("with an invalid authorization code grant", func(t *testing.T) {
nonce := pls.GenerateRandomHex(32)
r := test.Request(
"GET", "/session/callback?code=invalid",
test.WithCookie(web.NewCookie("oauth_state", nonce)),
)
tokens, err := svc.Exchange(r)
require.Error(t, err)
assert.Nil(t, tokens)
})
t.Run("with a valid grant", func(t *testing.T) {
user := mockoidc.DefaultUser()
code := srv.CreateAuthorizationCodeFor(user)
nonce := pls.GenerateRandomHex(32)
r := test.Request(
"GET",
"/session/callback?code="+code+"&state="+nonce,
test.WithCookie(web.NewCookie("oauth_state", nonce)),
)
tokens, err := svc.Exchange(r)
require.NoError(t, err)
assert.NotNil(t, tokens)
assert.NotEmpty(t, tokens.AccessToken)
assert.NotEmpty(t, tokens.Expiry)
assert.NotEmpty(t, tokens.TokenType)
assert.NotEmpty(t, tokens.RefreshToken)
assert.NotEmpty(t, tokens.IDToken)
})
})
}
|