This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
forked from arrikto/oidc-authservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oidc_test.go
189 lines (171 loc) · 4.57 KB
/
oidc_test.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"context"
"html/template"
"net/http"
"testing"
"time"
"github.com/coreos/go-oidc"
"github.com/gorilla/mux"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
func startFakeOIDCProvider(addr string) {
discoveryDoc := `
{
"issuer": "{{.Address}}",
"authorization_endpoint": "{{.Address}}/auth",
"userinfo_endpoint": "{{.Address}}/userinfo",
"revocation_endpoint": "{{.Address}}/revoke",
"jwks_uri": "{{.Address}}/jwks",
"response_types_supported": [
"code",
"token",
"id_token",
"code token",
"code id_token",
"token id_token",
"code token id_token",
"none"
],
"subject_types_supported": [
"public"
],
"id_token_signing_alg_values_supported": [
"RS256"
],
"scopes_supported": [
"openid",
"email",
"profile"
],
"token_endpoint_auth_methods_supported": [
"client_secret_post",
"client_secret_basic"
],
"claims_supported": [
"aud",
"email",
"email_verified",
"exp",
"family_name",
"given_name",
"iat",
"iss",
"locale",
"name",
"picture",
"sub"
],
"code_challenge_methods_supported": [
"plain",
"S256"
],
"grant_types_supported": [
"authorization_code",
"refresh_token",
"urn:ietf:params:oauth:grant-type:device_code",
"urn:ietf:params:oauth:grant-type:jwt-bearer"
]
}
`
tmpl, err := template.New("oidc_discovery_doc").Parse(discoveryDoc)
if err != nil {
log.Fatalf("Error parsing discovery doc template: %v", err)
}
discoveryHandler := func(w http.ResponseWriter, r *http.Request) {
err := tmpl.Execute(w, struct{ Address string }{Address: addr})
if err != nil {
log.Errorf("Error executing oidc discovery doc template: %v", err)
}
}
userinfoHandler := func(w http.ResponseWriter, r *http.Request) {
log.Info("Userinfo handler, returning 401...")
w.WriteHeader(http.StatusUnauthorized)
}
router := mux.NewRouter()
router.HandleFunc("/.well-known/openid-configuration", discoveryHandler)
router.HandleFunc("/userinfo", userinfoHandler)
log.Infof("Starting fake OIDC Provider at address: %v", addr)
if err := http.ListenAndServe("localhost:9999", router); err != nil {
log.Fatalf("Error in fake OIDC Provider server: %v", err)
}
}
func TestGetUserInfo_ContextCancelled(t *testing.T) {
// Start fake OIDC provider
oidcProviderAddr := "http://localhost:9999"
go startFakeOIDCProvider(oidcProviderAddr)
time.Sleep(5 * time.Second)
provider, err := oidc.NewProvider(context.Background(), oidcProviderAddr)
if err != nil {
t.Fatalf("Error creating OIDC Provider: %v", err)
}
// Make a UserInfo request
_, err = GetUserInfo(context.Background(), provider,
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "test"}))
// Check that we find a wrapped requestError
var reqErr *requestError
if !errors.As(err, &reqErr) {
log.Fatalf("Returned error is not a requestError. Got: %+v", reqErr)
}
if reqErr.Response.StatusCode != http.StatusUnauthorized {
t.Fatalf("Got wrong status code. Got '%v', expected '%v'.",
reqErr.Response.StatusCode, http.StatusUnauthorized)
}
}
func TestParseUserInfo(t *testing.T){
testCases := []struct{
testName string
userInfoResponse string
success bool
}{
{
testName: "EmailVerified: true (boolean)",
userInfoResponse: `{"sub": "", "profile": "", "email": "", "email_verified": true}`,
success: true,
},
{
testName: "EmailVerified: false (boolean)",
userInfoResponse: `{"sub": "","profile": "","email": "","email_verified": false}`,
success: true,
},
{
testName: "EmailVerified: true (string)",
userInfoResponse: `{"sub": "","profile": "","email": "","email_verified": "true"}`,
success: true,
},
{
testName: "EmailVerified: false (string)",
userInfoResponse: `{"sub": "","profile": "","email": "","email_verified": "false"}`,
success: true,
},
{
testName: "EmailVerified: nil",
userInfoResponse: `{"sub": "","profile": "","email": ""}`,
success: true,
},
{
testName: "EmailVerified: not boolean, not string, not nil",
userInfoResponse: `{"sub": "","profile": "","email": "","email_verified": 42}`,
success: false,
},
{
testName: "EmailVerified: irrelevant string",
userInfoResponse: `{"sub": "","profile": "","email": "","email_verified": "fals"}`,
success: false,
},
}
for _, c := range testCases {
t.Run(c.testName, func(t *testing.T) {
_, err:= ParseUserInfo([]byte(c.userInfoResponse))
success := true
if err != nil {
success = false
}
if success != c.success {
t.Errorf("ParseUserInfo result for %v is not the expected one.", c)
}
})
}
}