2016-07-25 20:00:28 +00:00
|
|
|
package server
|
2017-01-09 18:46:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-01-27 19:42:46 +00:00
|
|
|
"net/http"
|
2017-01-09 18:46:16 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2017-01-27 19:42:46 +00:00
|
|
|
"strings"
|
2017-01-09 18:46:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-11 01:51:12 +00:00
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
|
|
|
|
2017-01-09 18:46:16 +00:00
|
|
|
"github.com/coreos/dex/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseAuthorizationRequest(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
clients []storage.Client
|
|
|
|
supportedResponseTypes []string
|
|
|
|
|
2017-01-27 19:42:46 +00:00
|
|
|
usePOST bool
|
|
|
|
|
2017-01-09 18:46:16 +00:00
|
|
|
queryParams map[string]string
|
|
|
|
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "normal request",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "foo",
|
|
|
|
RedirectURIs: []string{"https://example.com/foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "foo",
|
|
|
|
"redirect_uri": "https://example.com/foo",
|
|
|
|
"response_type": "code",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
},
|
2017-01-27 19:42:46 +00:00
|
|
|
{
|
|
|
|
name: "POST request",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "foo",
|
|
|
|
RedirectURIs: []string{"https://example.com/foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "foo",
|
|
|
|
"redirect_uri": "https://example.com/foo",
|
|
|
|
"response_type": "code",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
usePOST: true,
|
|
|
|
},
|
2017-01-09 18:46:16 +00:00
|
|
|
{
|
|
|
|
name: "invalid client id",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "foo",
|
|
|
|
RedirectURIs: []string{"https://example.com/foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "bar",
|
|
|
|
"redirect_uri": "https://example.com/foo",
|
|
|
|
"response_type": "code",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid redirect uri",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "bar",
|
|
|
|
RedirectURIs: []string{"https://example.com/bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "bar",
|
|
|
|
"redirect_uri": "https://example.com/foo",
|
|
|
|
"response_type": "code",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "implicit flow",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "bar",
|
|
|
|
RedirectURIs: []string{"https://example.com/bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code", "id_token", "token"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "bar",
|
|
|
|
"redirect_uri": "https://example.com/bar",
|
|
|
|
"response_type": "code id_token",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsupported response type",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "bar",
|
|
|
|
RedirectURIs: []string{"https://example.com/bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "bar",
|
|
|
|
"redirect_uri": "https://example.com/bar",
|
|
|
|
"response_type": "code id_token",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only token response type",
|
|
|
|
clients: []storage.Client{
|
|
|
|
{
|
|
|
|
ID: "bar",
|
|
|
|
RedirectURIs: []string{"https://example.com/bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportedResponseTypes: []string{"code", "id_token", "token"},
|
|
|
|
queryParams: map[string]string{
|
|
|
|
"client_id": "bar",
|
|
|
|
"redirect_uri": "https://example.com/bar",
|
|
|
|
"response_type": "token",
|
|
|
|
"scope": "openid email profile",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
func() {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
httpServer, server := newTestServer(ctx, t, func(c *Config) {
|
|
|
|
c.SupportedResponseTypes = tc.supportedResponseTypes
|
|
|
|
c.Storage = storage.WithStaticClients(c.Storage, tc.clients)
|
|
|
|
})
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
for k, v := range tc.queryParams {
|
|
|
|
params.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2017-01-27 19:42:46 +00:00
|
|
|
var req *http.Request
|
|
|
|
if tc.usePOST {
|
|
|
|
body := strings.NewReader(params.Encode())
|
|
|
|
req = httptest.NewRequest("POST", httpServer.URL+"/auth", body)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
} else {
|
|
|
|
req = httptest.NewRequest("GET", httpServer.URL+"/auth?"+params.Encode(), nil)
|
|
|
|
}
|
2017-01-09 18:46:16 +00:00
|
|
|
_, err := server.parseAuthorizationRequest(req)
|
|
|
|
if err != nil && !tc.wantErr {
|
|
|
|
t.Errorf("%s: %v", tc.name, err)
|
|
|
|
}
|
|
|
|
if err == nil && tc.wantErr {
|
|
|
|
t.Errorf("%s: expected error", tc.name)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 01:51:12 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
// at_hash value and access_token returned by Google.
|
|
|
|
googleAccessTokenHash = "piwt8oCH-K2D9pXlaS1Y-w"
|
|
|
|
googleAccessToken = "ya29.CjHSA1l5WUn8xZ6HanHFzzdHdbXm-14rxnC7JHch9eFIsZkQEGoWzaYG4o7k5f6BnPLj"
|
|
|
|
googleSigningAlg = jose.RS256
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccessTokenHash(t *testing.T) {
|
|
|
|
atHash, err := accessTokenHash(googleSigningAlg, googleAccessToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if atHash != googleAccessTokenHash {
|
|
|
|
t.Errorf("expected %q got %q", googleAccessTokenHash, atHash)
|
|
|
|
}
|
|
|
|
}
|