2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-10-27 20:35:35 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-12-30 18:11:02 -05:00
|
|
|
package jwt
|
2014-10-27 20:35:35 -04:00
|
|
|
|
|
|
|
import (
|
2016-08-24 18:40:36 -04:00
|
|
|
"context"
|
2014-10-27 20:35:35 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var dummyPrivateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIEpAIBAAKCAQEAx4fm7dngEmOULNmAs1IGZ9Apfzh+BkaQ1dzkmbUgpcoghucE
|
|
|
|
DZRnAGd2aPyB6skGMXUytWQvNYav0WTR00wFtX1ohWTfv68HGXJ8QXCpyoSKSSFY
|
|
|
|
fuP9X36wBSkSX9J5DVgiuzD5VBdzUISSmapjKm+DcbRALjz6OUIPEWi1Tjl6p5RK
|
|
|
|
1w41qdbmt7E5/kGhKLDuT7+M83g4VWhgIvaAXtnhklDAggilPPa8ZJ1IFe31lNlr
|
|
|
|
k4DRk38nc6sEutdf3RL7QoH7FBusI7uXV03DC6dwN1kP4GE7bjJhcRb/7jYt7CQ9
|
|
|
|
/E9Exz3c0yAp0yrTg0Fwh+qxfH9dKwN52S7SBwIDAQABAoIBAQCaCs26K07WY5Jt
|
|
|
|
3a2Cw3y2gPrIgTCqX6hJs7O5ByEhXZ8nBwsWANBUe4vrGaajQHdLj5OKfsIDrOvn
|
|
|
|
2NI1MqflqeAbu/kR32q3tq8/Rl+PPiwUsW3E6Pcf1orGMSNCXxeducF2iySySzh3
|
|
|
|
nSIhCG5uwJDWI7a4+9KiieFgK1pt/Iv30q1SQS8IEntTfXYwANQrfKUVMmVF9aIK
|
|
|
|
6/WZE2yd5+q3wVVIJ6jsmTzoDCX6QQkkJICI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3slYLpqrGlcbLgUXBUgzEO684Wk/UV9DFPlHALVqCfXQ9dpJPg==
|
|
|
|
-----END RSA PRIVATE KEY-----`)
|
|
|
|
|
|
|
|
func TestJWTFetch_JSONResponse(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2014-11-06 19:36:41 -05:00
|
|
|
w.Write([]byte(`{
|
|
|
|
"access_token": "90d64460d14870c08c81352a05dedd3465940a7c",
|
|
|
|
"scope": "user",
|
2014-12-16 12:42:17 -05:00
|
|
|
"token_type": "bearer",
|
|
|
|
"expires_in": 3600
|
2014-11-06 19:36:41 -05:00
|
|
|
}`))
|
2014-10-27 20:35:35 -04:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
|
2014-12-30 18:11:02 -05:00
|
|
|
conf := &Config{
|
2014-12-11 02:30:13 -05:00
|
|
|
Email: "aaa@xxx.com",
|
|
|
|
PrivateKey: dummyPrivateKey,
|
|
|
|
TokenURL: ts.URL,
|
|
|
|
}
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.TokenSource(context.Background()).Token()
|
2014-10-27 20:35:35 -04:00
|
|
|
if err != nil {
|
2014-12-11 02:30:13 -05:00
|
|
|
t.Fatal(err)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
if !tok.Valid() {
|
2016-08-26 13:13:32 -04:00
|
|
|
t.Errorf("got invalid token: %v", tok)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := tok.AccessToken, "90d64460d14870c08c81352a05dedd3465940a7c"; got != want {
|
|
|
|
t.Errorf("access token = %q; want %q", got, want)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := tok.TokenType, "bearer"; got != want {
|
|
|
|
t.Errorf("token type = %q; want %q", got, want)
|
2014-12-16 12:42:17 -05:00
|
|
|
}
|
2016-08-26 13:13:32 -04:00
|
|
|
if got := tok.Expiry.IsZero(); got {
|
|
|
|
t.Errorf("token expiry = %v, want none", got)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
scope := tok.Extra("scope")
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := scope, "user"; got != want {
|
|
|
|
t.Errorf("scope = %q; want %q", got, want)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJWTFetch_BadResponse(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"scope": "user", "token_type": "bearer"}`))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
|
2014-12-30 18:11:02 -05:00
|
|
|
conf := &Config{
|
2014-12-11 02:30:13 -05:00
|
|
|
Email: "aaa@xxx.com",
|
|
|
|
PrivateKey: dummyPrivateKey,
|
|
|
|
TokenURL: ts.URL,
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.TokenSource(context.Background()).Token()
|
2014-10-27 20:35:35 -04:00
|
|
|
if err != nil {
|
2014-12-11 02:30:13 -05:00
|
|
|
t.Fatal(err)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
if tok == nil {
|
2016-08-26 13:13:32 -04:00
|
|
|
t.Fatalf("got nil token; want token")
|
2014-12-30 16:25:01 -05:00
|
|
|
}
|
|
|
|
if tok.Valid() {
|
2016-08-26 13:13:32 -04:00
|
|
|
t.Errorf("got invalid token: %v", tok)
|
2014-12-30 16:25:01 -05:00
|
|
|
}
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := tok.AccessToken, ""; got != want {
|
|
|
|
t.Errorf("access token = %q; want %q", got, want)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := tok.TokenType, "bearer"; got != want {
|
|
|
|
t.Errorf("token type = %q; want %q", got, want)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
scope := tok.Extra("scope")
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := scope, "user"; got != want {
|
|
|
|
t.Errorf("token scope = %q; want %q", got, want)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJWTFetch_BadResponseType(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"access_token":123, "scope": "user", "token_type": "bearer"}`))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-30 18:11:02 -05:00
|
|
|
conf := &Config{
|
2014-12-11 02:30:13 -05:00
|
|
|
Email: "aaa@xxx.com",
|
|
|
|
PrivateKey: dummyPrivateKey,
|
|
|
|
TokenURL: ts.URL,
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.TokenSource(context.Background()).Token()
|
2014-12-19 00:20:30 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Error("got a token; expected error")
|
2016-08-26 13:13:32 -04:00
|
|
|
if got, want := tok.AccessToken, ""; got != want {
|
|
|
|
t.Errorf("access token = %q; want %q", got, want)
|
2014-12-19 00:20:30 -05:00
|
|
|
}
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
}
|