@@ -6,11 +6,16 @@ package oidc
6
6
7
7
import (
8
8
"context"
9
+ "fmt"
10
+ "log"
9
11
"net/http"
10
12
"net/http/httptest"
13
+ "net/url"
11
14
"testing"
12
15
13
16
"github.com/coreos/go-oidc/v3/oidc"
17
+ "github.com/go-chi/chi/v5"
18
+ "github.com/go-chi/chi/v5/middleware"
14
19
"github.com/stretchr/testify/require"
15
20
"golang.org/x/oauth2"
16
21
)
@@ -53,9 +58,9 @@ func TestGetStartParams(t *testing.T) {
53
58
}
54
59
55
60
func TestGetClientConfigFromRequest (t * testing.T ) {
56
- const (
57
- issuerG = "https://accounts.google.com"
58
- )
61
+ issuer , err := setupFakeIdP ( t )
62
+ require . NoError ( t , err )
63
+
59
64
testCases := []struct {
60
65
Location string
61
66
ExpectedError bool
@@ -67,7 +72,7 @@ func TestGetClientConfigFromRequest(t *testing.T) {
67
72
ExpectedId : "" ,
68
73
},
69
74
{
70
- Location : "/start?issuer=https%3A%2F%2Faccounts.google.com" ,
75
+ Location : "/start?issuer=" + url . QueryEscape ( issuer ) ,
71
76
ExpectedError : false ,
72
77
ExpectedId : "google-1" ,
73
78
},
@@ -79,9 +84,9 @@ func TestGetClientConfigFromRequest(t *testing.T) {
79
84
}
80
85
81
86
service := NewOIDCService ()
82
- err : = service .AddClientConfig (& OIDCClientConfig {
87
+ err = service .AddClientConfig (& OIDCClientConfig {
83
88
ID : "google-1" ,
84
- Issuer : issuerG ,
89
+ Issuer : issuer ,
85
90
OIDCConfig : & oidc.Config {},
86
91
OAuth2Config : & oauth2.Config {},
87
92
})
@@ -103,34 +108,119 @@ func TestGetClientConfigFromRequest(t *testing.T) {
103
108
}
104
109
}
105
110
106
- func TestAuthenticate (t * testing.T ) {
107
- t .Skip () //
108
- const (
109
- issuerG = "https://accounts.google.com"
110
- )
111
+ func TestAuthenticate_nonce_check (t * testing.T ) {
112
+ issuer , err := setupFakeIdP (t )
113
+ require .NoError (t , err )
114
+
111
115
service := NewOIDCService ()
112
- err : = service .AddClientConfig (& OIDCClientConfig {
116
+ err = service .AddClientConfig (& OIDCClientConfig {
113
117
ID : "google-1" ,
114
- Issuer : issuerG ,
118
+ Issuer : issuer ,
115
119
OIDCConfig : & oidc.Config {
116
- SkipClientIDCheck : true ,
120
+ SkipClientIDCheck : true ,
121
+ SkipIssuerCheck : true ,
122
+ SkipExpiryCheck : true ,
123
+ InsecureSkipSignatureCheck : true ,
117
124
},
118
125
OAuth2Config : & oauth2.Config {},
119
126
})
120
127
require .NoError (t , err , "failed to initialize test" )
121
128
122
129
token := oauth2.Token {}
130
+ rawIDToken := `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkN1bXF1YXQgRG9wdGlnIiwibm9uY2UiOiIxMTEiLCJpYXQiOjE1MTYyMzkwMjJ9.NfbRZns-Sefhw6MT4ULWMj_7bX0vScklaZA2ObCYkStYlo2SvNu5Be79-5Lwcy4GY95vY_dFvLIKrZjfqv_duURSKLUbtH8VxskhcrW4sPAK2R5lzz62a6d_OnVydjNJRZf754TQZILAzMm81tEDNAJSDQjaTFl7t8Bp0iYapNyyH9ZoBrGAPaZkXHYoq4lNH88gCZj5JMRIbrZbsvhOuR3CAzbAMplOmKIWxhFVnHdm6aq6HRjz0ra6Y7yh0R9jEF3vWl2w5D3aN4XESPNBbyB3CHKQ5TG0WkbgdUpv1wwzbPfz4aFHOt--qLy7ZK0TOrS-A7YLFFsJKtoPGRjAPA`
123
131
extra := map [string ]interface {}{
124
- "id_token" : "foo123" ,
132
+ "id_token" : rawIDToken ,
125
133
}
126
134
127
- ctx := context .Background ()
128
- nonceCookieValue := "foobar123"
129
- result , err := service .Authenticate (ctx , & OAuth2Result {
135
+ nonceCookieValue := "111"
136
+ oauth2Result := & OAuth2Result {
130
137
OAuth2Token : token .WithExtra (extra ),
131
- }, issuerG , nonceCookieValue )
138
+ }
139
+ result , err := service .Authenticate (context .Background (), oauth2Result , issuer , nonceCookieValue )
132
140
133
141
require .NoError (t , err , "failed to authenticate" )
134
142
require .NotNil (t , result )
143
+ }
144
+
145
+ func setupFakeIdP (t * testing.T ) (string , error ) {
146
+ router := chi .NewRouter ()
147
+ ts := httptest .NewServer (router )
148
+ url := ts .URL
149
+
150
+ router .Use (middleware .Logger )
151
+ router .Get ("/oauth2/v3/certs" , func (w http.ResponseWriter , r * http.Request ) {
152
+ _ , err := w .Write ([]byte (`{
153
+ "keys": [
154
+ ]
155
+ }` ))
156
+ if err != nil {
157
+ log .Fatal (err )
158
+ }
159
+ })
160
+ router .Get ("/.well-known/openid-configuration" , func (w http.ResponseWriter , r * http.Request ) {
161
+ _ , err := w .Write ([]byte (fmt .Sprintf (`{
162
+ "issuer": "%[1]s",
163
+ "authorization_endpoint": "%[1]s/o/oauth2/v2/auth",
164
+ "device_authorization_endpoint": "%[1]s/device/code",
165
+ "token_endpoint": "%[1]s/token",
166
+ "userinfo_endpoint": "%[1]s/v1/userinfo",
167
+ "revocation_endpoint": "%[1]s/revoke",
168
+ "jwks_uri": "%[1]s/oauth2/v3/certs",
169
+ "response_types_supported": [
170
+ "code",
171
+ "token",
172
+ "id_token",
173
+ "code token",
174
+ "code id_token",
175
+ "token id_token",
176
+ "code token id_token",
177
+ "none"
178
+ ],
179
+ "subject_types_supported": [
180
+ "public"
181
+ ],
182
+ "id_token_signing_alg_values_supported": [
183
+ "RS256"
184
+ ],
185
+ "scopes_supported": [
186
+ "openid",
187
+ "email",
188
+ "profile"
189
+ ],
190
+ "token_endpoint_auth_methods_supported": [
191
+ "client_secret_post",
192
+ "client_secret_basic"
193
+ ],
194
+ "claims_supported": [
195
+ "aud",
196
+ "email",
197
+ "email_verified",
198
+ "exp",
199
+ "family_name",
200
+ "given_name",
201
+ "iat",
202
+ "iss",
203
+ "locale",
204
+ "name",
205
+ "picture",
206
+ "sub"
207
+ ],
208
+ "code_challenge_methods_supported": [
209
+ "plain",
210
+ "S256"
211
+ ],
212
+ "grant_types_supported": [
213
+ "authorization_code",
214
+ "refresh_token",
215
+ "urn:ietf:params:oauth:grant-type:device_code",
216
+ "urn:ietf:params:oauth:grant-type:jwt-bearer"
217
+ ]
218
+ }` , url )))
219
+ if err != nil {
220
+ log .Fatal (err )
221
+ }
222
+ })
135
223
224
+ t .Cleanup (ts .Close )
225
+ return url , nil
136
226
}
0 commit comments