This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
forked from evanj/googlesignin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_test.go
160 lines (140 loc) · 4.86 KB
/
public_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
// Tests of the public googlesignin API to avoid import cycles with signintest.
package googlesignin_test
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/evanj/googlesignin"
"github.com/evanj/googlesignin/signintest"
)
const insecureTestDomain = "example.com"
const testEmail = "user@" + insecureTestDomain
type fixture struct {
a *googlesignin.Authenticator
requestAuth *signintest.RequestAuthenticator
}
func newFixture() *fixture {
f := &fixture{
googlesignin.New(signintest.ClientID),
nil,
}
f.a.HostedDomain = insecureTestDomain
f.requestAuth = signintest.InsecureTestAuthenticator(f.a)
return f
}
func TestGetEmail(t *testing.T) {
f := newFixture()
r := httptest.NewRequest(http.MethodGet, "/hello", nil)
email, err := f.a.GetEmail(r)
if !(email == "" && strings.Contains(err.Error(), "no ID token cookie")) {
t.Error("GetEmail without cookie expected no ID token", email, err)
}
// Set a token: hack the key cache to avoid using google's keys
r = f.requestAuth.InsecureMakeAuthenticated(r, testEmail)
email, err = f.a.GetEmail(r)
if !(email == "[email protected]" && err == nil) {
t.Error("expected success", email, err)
}
}
func TestAuthenticatedHandler(t *testing.T) {
f := newFixture()
var calledRequest *http.Request
handler := func(w http.ResponseWriter, r *http.Request) {
calledRequest = r
}
authenticatedHandler := f.a.RequireSignIn(http.HandlerFunc(handler))
// request failed: not permitted
recorder := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/hello?param=1", nil)
authenticatedHandler.ServeHTTP(recorder, r)
if recorder.Code != http.StatusForbidden {
t.Error("expected Forbidden:", recorder.Code)
}
if calledRequest != nil {
t.Error("handler should not have been called:", calledRequest)
}
// enable redirects: GET should now be redirected; POST should fail
f.a.RedirectIfNotSignedIn = true
recorder = httptest.NewRecorder()
authenticatedHandler.ServeHTTP(recorder, r)
location := recorder.Header().Get("Location")
if calledRequest != nil || !(recorder.Code == http.StatusSeeOther && location == "/__start_signin") {
t.Error("expected redirect:", recorder.Code, location)
}
postR := httptest.NewRequest(http.MethodPost, "/hello", nil)
recorder = httptest.NewRecorder()
authenticatedHandler.ServeHTTP(recorder, postR)
if calledRequest != nil || recorder.Code != http.StatusForbidden {
t.Error("expected forbidden:", recorder.Code)
}
// make hello public: should be permitted
f.a.MakePublic("/hello")
recorder = httptest.NewRecorder()
authenticatedHandler.ServeHTTP(recorder, r)
if calledRequest == nil || recorder.Code != http.StatusOK {
t.Error("expected succesful request:", recorder.Code)
}
calledRequest = nil
// the sign in path must be public
r.URL.Scheme = "https"
r.URL.Path = "/__start_signin"
recorder = httptest.NewRecorder()
authenticatedHandler.ServeHTTP(recorder, r)
if calledRequest != nil || recorder.Code != http.StatusOK {
t.Error("expected succesful request for sign in:", recorder.Code)
}
body, _ := ioutil.ReadAll(recorder.Result().Body)
if !bytes.Contains(body, []byte("https://accounts.google.com/gsi/client")) {
t.Error("sign in page should load JS library", string(body))
}
r.URL.Path = "/other"
r = f.requestAuth.InsecureMakeAuthenticated(r, "[email protected]")
recorder = httptest.NewRecorder()
authenticatedHandler.ServeHTTP(recorder, r)
if calledRequest == nil || recorder.Code != http.StatusOK {
t.Error("expected succesful request:", recorder.Code)
}
email := f.a.MustGetEmail(calledRequest)
if email != testEmail {
t.Errorf("MustGetEmail expected %#v got %#v", testEmail, email)
}
}
func TestHTTPSSignIn(t *testing.T) {
f := newFixture()
handler := func(w http.ResponseWriter, r *http.Request) {}
authenticatedHandler := f.a.RequireSignIn(http.HandlerFunc(handler))
// HTTPS request: works
recorder := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/__start_signin", nil)
r.URL.Scheme = "https"
authenticatedHandler.ServeHTTP(recorder, r)
if recorder.Code != http.StatusOK {
t.Error("expected OK:", recorder.Code)
}
// HTTP request: fails
recorder = httptest.NewRecorder()
r.URL.Scheme = "http"
authenticatedHandler.ServeHTTP(recorder, r)
if recorder.Code != http.StatusInternalServerError {
t.Error("expected InternalServerError:", recorder.Code)
}
// HTTP with X-Forwarded-Proto: success
recorder = httptest.NewRecorder()
r.URL.Scheme = "http"
r.Header.Set("X-Forwarded-Proto", "https")
authenticatedHandler.ServeHTTP(recorder, r)
if recorder.Code != http.StatusOK {
t.Error("expected OK:", recorder.Code)
}
r.Header.Del("X-Forwarded-Proto")
// HTTP request with insecure set: works
f.a.PermitInsecureCookies()
recorder = httptest.NewRecorder()
authenticatedHandler.ServeHTTP(recorder, r)
if recorder.Code != http.StatusOK {
t.Error("expected OK:", recorder.Code)
}
}