-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathoidc_test.go
154 lines (129 loc) · 3.97 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
package beyond
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
oidc "github.com/coreos/go-oidc"
"github.com/drewolson/testflight"
"github.com/gorilla/securecookie"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)
var (
oidcServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/.well-known/openid-configuration":
err := json.NewEncoder(w).Encode(oidcWK)
if err != nil {
http.Error(w, err.Error(), 500)
}
return
case "/token":
fmt.Fprint(w, `{"typ":"JWT","alg":"HS256"}`)
return
case "/next":
fmt.Fprint(w, "NEXT")
return
default:
WithField("path", r.URL.Path).Error("Invalid OIDC Request")
return
}
}))
oidcWK = struct {
Issuer string
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
}{
"/issuer",
"/authorize",
"/token",
}
)
type oidcMock struct{}
func (o *oidcMock) AuthCodeURL(state string, opt ...oauth2.AuthCodeOption) string {
return oidcServer.URL + "/AuthCodeURL"
}
func (o *oidcMock) Exchange(ctx context.Context, code string) (*oauth2.Token, error) {
token := &oauth2.Token{}
token.AccessToken = "AccessToken"
token.Expiry = time.Now().Add(time.Hour)
token = token.WithExtra(map[string]interface{}{"id_token": "IDToken"})
return token, nil
}
func (o *oidcMock) Verify(ctx context.Context, raw string) (*oidc.IDToken, error) {
if raw == "err" {
return nil, http.ErrHijacked
}
token := &oidc.IDToken{}
getOIDCClaims = func(claims *oidcClaims, tokenID *oidc.IDToken) error {
claims.Email = "[email protected]"
return nil
}
if raw == "claimsErr" {
getOIDCClaims = func(claims *oidcClaims, tokenID *oidc.IDToken) error {
return fmt.Errorf("test error")
}
}
return token, nil
}
func init() {
// *oidcIssuer = oidcServer.URL
oidcWK.Issuer = oidcServer.URL + oidcWK.Issuer
oidcWK.TokenEndpoint = oidcServer.URL + oidcWK.TokenEndpoint
oidcWK.AuthorizationEndpoint = oidcServer.URL + oidcWK.AuthorizationEndpoint
}
func TestOIDCSetup(t *testing.T) {
assert.Contains(t, oidcSetup("ftp://localhost").Error(), "unsupported protocol scheme")
}
func TestOIDCSuccess(t *testing.T) {
mock := &oidcMock{}
oidcConfig = mock
oidcVerifier = mock
testflight.WithServer(testMux, func(r *testflight.Requester) {
request, err := http.NewRequest("GET", "/oidc?state=barbaz&next=localhost/next", nil)
assert.NoError(t, err)
vals := map[string]interface{}{"state": "barbaz", "next": oidcServer.URL + "/next"}
cookieValue, err := securecookie.EncodeMulti(*cookieName, &vals, store.Codecs...)
assert.NoError(t, err)
request.AddCookie(&http.Cookie{Name: *cookieName, Value: cookieValue})
request.Host = *host
response := r.Do(request)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "NEXT", response.Body)
b := strings.NewReader("POSTED")
request, err = http.NewRequest("POST", oidcServer.URL+"/next", b)
assert.NoError(t, err)
request.AddCookie(&http.Cookie{Name: *cookieName, Value: cookieValue})
request.Host = *host
resp, err := http.DefaultClient.Do(request)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
respBody, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "NEXT", string(respBody))
})
}
func TestOIDCVerifyToken(t *testing.T) {
token := &oauth2.Token{}
s, err := oidcVerifyToken(context.TODO(), token)
assert.Empty(t, s)
assert.Equal(t, "missing ID token", err.Error())
}
func TestOIDCVerifyTokenID(t *testing.T) {
email, err := oidcVerifyTokenID(context.TODO(), "err")
assert.Equal(t, "", email)
assert.Equal(t, http.ErrHijacked, err)
testErr := fmt.Errorf("test error")
email, err = oidcVerifyTokenID(context.TODO(), "claimsErr")
assert.Equal(t, "", email)
assert.Equal(t, testErr, err)
email, err = oidcVerifyTokenID(context.TODO(), "rawID")
assert.Equal(t, "[email protected]", email)
assert.NoError(t, err)
}