-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
auth_test.go
178 lines (151 loc) · 4.7 KB
/
auth_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
package tdameritrade
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"golang.org/x/oauth2"
)
type testStore struct{}
func (s *testStore) StoreToken(token *oauth2.Token, w http.ResponseWriter, req *http.Request) error {
return nil
}
func (s *testStore) GetToken(req *http.Request) (*oauth2.Token, error) {
return &oauth2.Token{
AccessToken: "ACCESSTOKEN",
RefreshToken: "REFRESHTOKEN",
Expiry: time.Now().AddDate(0, 0, 1),
}, nil
}
func (s *testStore) StoreState(state string, w http.ResponseWriter, req *http.Request) error {
return nil
}
func (s *testStore) GetState(req *http.Request) (string, error) {
return "state", nil
}
type testEmptyStore struct{}
func (s *testEmptyStore) StoreToken(token *oauth2.Token, w http.ResponseWriter, req *http.Request) error {
return nil
}
func (s *testEmptyStore) GetToken(req *http.Request) (*oauth2.Token, error) {
return &oauth2.Token{
AccessToken: "ACCESSTOKEN",
RefreshToken: "REFRESHTOKEN",
Expiry: time.Now().AddDate(0, 0, 1),
}, nil
}
func (s *testEmptyStore) StoreState(state string, w http.ResponseWriter, req *http.Request) error {
return nil
}
func (s *testEmptyStore) GetState(req *http.Request) (string, error) {
return "", nil
}
func TestStartOAuth2ReturnsCorrectURL(t *testing.T) {
authenticator := &Authenticator{
Store: &testEmptyStore{},
OAuth2: oauth2.Config{
ClientID: "CLIENTID",
Endpoint: oauth2.Endpoint{
TokenURL: "https://api.tdameritrade.com/v1/oauth2/token",
AuthURL: "https://auth.tdameritrade.com/auth",
},
RedirectURL: "https://localhost:8080/callback",
},
}
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/callback?code=code&state=state", nil)
u, err := authenticator.StartOAuth2Flow(w, req)
if err != nil {
t.Fatalf(err.Error())
}
expectedURL, _ := url.Parse("https://auth.tdameritrade.com/auth?client_id=CLIENTID&redirect_uri=https%3A%2F%2Flocalhost%3A8080%2Fcallback&response_type=code")
actualURL, _ := url.Parse(u)
// State query param is different each invocation.
// This cannot be changed by design.
modifiedQuery := actualURL.Query()
modifiedQuery.Del("state")
actualURL.RawQuery = modifiedQuery.Encode()
if actualURL.String() != expectedURL.String() {
t.Fatalf("invalid auth code URL. expected: '%v', got: '%v'", expectedURL, actualURL)
}
}
func TestFinishOAuth2RejectsEmptyState(t *testing.T) {
authenticator := &Authenticator{
Store: &testEmptyStore{},
OAuth2: oauth2.Config{
ClientID: "CLIENTID",
Endpoint: oauth2.Endpoint{
TokenURL: "https://api.tdameritrade.com/v1/oauth2/token",
AuthURL: "https://auth.tdameritrade.com/auth",
},
RedirectURL: "https://localhost:8080/callback",
},
}
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/callback?code=code&state=state", nil)
ctx := context.Background()
client, err := authenticator.FinishOAuth2Flow(ctx, w, req)
if err == nil {
t.Fatalf("empty state not rejected.")
}
if err.Error() != "missing state in request from TD Ameritrade" {
t.Fatalf("unexpected error: %v", err)
}
if client != nil {
t.Fatalf("client returned despite empty state.")
}
}
func TestFinishOAuth2RejectsInvalidState(t *testing.T) {
authenticator := &Authenticator{
Store: &testStore{},
OAuth2: oauth2.Config{
ClientID: "CLIENTID",
Endpoint: oauth2.Endpoint{
TokenURL: "https://api.tdameritrade.com/v1/oauth2/token",
AuthURL: "https://auth.tdameritrade.com/auth",
},
RedirectURL: "https://localhost:8080/callback",
},
}
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/callback?code=code&state=invalid", nil)
ctx := context.Background()
client, err := authenticator.FinishOAuth2Flow(ctx, w, req)
if err == nil {
t.Fatalf("invalid state not rejected.")
}
if err.Error() != "invalid state. expected: 'state', got 'invalid'" {
t.Fatalf("unexpected error: %v", err)
}
if client != nil {
t.Fatalf("client returned despite invalid state.")
}
}
func TestFinishOAuth2RejectsEmptyCode(t *testing.T) {
authenticator := &Authenticator{
Store: &testStore{},
OAuth2: oauth2.Config{
ClientID: "CLIENTID",
Endpoint: oauth2.Endpoint{
TokenURL: "https://api.tdameritrade.com/v1/oauth2/token",
AuthURL: "https://auth.tdameritrade.com/auth",
},
RedirectURL: "https://localhost:8080/callback",
},
}
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/callback?code=&state=state", nil)
ctx := context.Background()
client, err := authenticator.FinishOAuth2Flow(ctx, w, req)
if err == nil {
t.Fatalf("empty code not rejected.")
}
if err.Error() != "missing code in request from TD Ameritrade" {
t.Fatalf("unexpected error: %v", err)
}
if client != nil {
t.Fatalf("client returned despite empty code.")
}
}