git.iko.soy/typeable/oauth2-proxy
on master help sign in
ValidateToken() to check access_token validity
commit25372567ac51e70aa052e0e17a1ebf2242110971 authorMike Bland <michael.bland@gsa.gov> dateSat 9 May 2015, 13:01 (11 years ago) parent7285701
2 files changed+105
oauthproxy.go+21 oauthproxy_test.go+84
oauthproxy.go+21
@@ -262,6 +262,27 @@ func (p *OauthProxy) SetCookie(rw http.ResponseWriter, req *http.Request, val st
262262 http.SetCookie(rw, p.MakeCookie(req, val, p.CookieExpire))
263263}
264264
265+func (p *OauthProxy) ValidateToken(access_token string) bool {
266+ if access_token == "" || p.oauthValidateUrl == nil {
267+ return false
268+ }
269+
270+ req, err := http.NewRequest("GET",
271+ p.oauthValidateUrl.String()+"?access_token="+access_token, nil)
272+ if err != nil {
273+ log.Printf("failed building token validation request: %s", err)
274+ return false
275+ }
276+
277+ httpclient := &http.Client{}
278+ resp, err := httpclient.Do(req)
279+ if err != nil {
280+ log.Printf("token validation request failed: %s", err)
281+ return false
282+ }
283+ return resp.StatusCode == 200
284+}
285+
265286func (p *OauthProxy) ProcessCookie(rw http.ResponseWriter, req *http.Request) (email, user, access_token string, ok bool) {
266287 var value string
267288 var timestamp time.Time
oauthproxy_test.go+84
@@ -306,6 +306,90 @@ func TestSignInPageDirectAccessRedirectsToRoot(t *testing.T) {
306306 }
307307}
308308
309+type ValidateTokenTest struct {
310+ opts *Options
311+ proxy *OauthProxy
312+ backend *httptest.Server
313+ response_code int
314+}
315+
316+func NewValidateTokenTest() *ValidateTokenTest {
317+ var vt_test ValidateTokenTest
318+
319+ vt_test.opts = NewOptions()
320+ vt_test.opts.Upstreams = append(vt_test.opts.Upstreams, "unused")
321+ vt_test.opts.CookieSecret = "foobar"
322+ vt_test.opts.ClientID = "bazquux"
323+ vt_test.opts.ClientSecret = "xyzzyplugh"
324+ vt_test.opts.Validate()
325+
326+ vt_test.backend = httptest.NewServer(
327+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
328+ switch r.URL.Path {
329+ case "/oauth/tokeninfo":
330+ w.WriteHeader(vt_test.response_code)
331+ w.Write([]byte("only code matters; contents disregarded"))
332+ default:
333+ w.WriteHeader(500)
334+ w.Write([]byte("unknown URL"))
335+ }
336+ }))
337+ backend_url, _ := url.Parse(vt_test.backend.URL)
338+ vt_test.opts.provider.Data().ValidateUrl = &url.URL{
339+ Scheme: "http",
340+ Host: backend_url.Host,
341+ Path: "/oauth/tokeninfo",
342+ }
343+ vt_test.response_code = 200
344+
345+ vt_test.proxy = NewOauthProxy(vt_test.opts, func(email string) bool {
346+ return true
347+ })
348+ return &vt_test
349+}
350+
351+func (vt_test *ValidateTokenTest) Close() {
352+ vt_test.backend.Close()
353+}
354+
355+func TestValidateTokenEmptyToken(t *testing.T) {
356+ vt_test := NewValidateTokenTest()
357+ defer vt_test.Close()
358+
359+ assert.Equal(t, false, vt_test.proxy.ValidateToken(""))
360+}
361+
362+func TestValidateTokenEmptyValidateUrl(t *testing.T) {
363+ vt_test := NewValidateTokenTest()
364+ defer vt_test.Close()
365+
366+ vt_test.proxy.oauthValidateUrl = nil
367+ assert.Equal(t, false, vt_test.proxy.ValidateToken("foobar"))
368+}
369+
370+func TestValidateTokenRequestNetworkFailure(t *testing.T) {
371+ vt_test := NewValidateTokenTest()
372+ // Close immediately to simulate a network failure
373+ vt_test.Close()
374+
375+ assert.Equal(t, false, vt_test.proxy.ValidateToken("foobar"))
376+}
377+
378+func TestValidateTokenExpiredToken(t *testing.T) {
379+ vt_test := NewValidateTokenTest()
380+ defer vt_test.Close()
381+
382+ vt_test.response_code = 401
383+ assert.Equal(t, false, vt_test.proxy.ValidateToken("foobar"))
384+}
385+
386+func TestValidateTokenValidToken(t *testing.T) {
387+ vt_test := NewValidateTokenTest()
388+ defer vt_test.Close()
389+
390+ assert.Equal(t, true, vt_test.proxy.ValidateToken("foobar"))
391+}
392+
309393type ProcessCookieTest struct {
310394 opts *Options
311395 proxy *OauthProxy