2016-07-26 01:30:28 +05:30
|
|
|
package server
|
2017-01-10 00:16:16 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-20 22:45:59 +05:30
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2017-01-28 01:12:46 +05:30
|
|
|
"net/http"
|
2017-01-10 00:16:16 +05:30
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2017-01-28 01:12:46 +05:30
|
|
|
"strings"
|
2017-01-10 00:16:16 +05:30
|
|
|
"testing"
|
|
|
|
|
2017-01-11 07:21:12 +05:30
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
|
|
|
|
2018-09-03 12:14:44 +05:30
|
|
|
"github.com/dexidp/dex/storage"
|
2019-06-20 22:45:59 +05:30
|
|
|
"github.com/dexidp/dex/storage/memory"
|
2017-01-10 00:16:16 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseAuthorizationRequest(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
clients []storage.Client
|
|
|
|
supportedResponseTypes []string
|
|
|
|
|
2017-01-28 01:12:46 +05:30
|
|
|
usePOST bool
|
|
|
|
|
2017-01-10 00:16:16 +05:30
|
|
|
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-28 01:12:46 +05:30
|
|
|
{
|
|
|
|
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-10 00:16:16 +05:30
|
|
|
{
|
|
|
|
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-28 01:12:46 +05:30
|
|
|
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-10 00:16:16 +05:30
|
|
|
_, 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 07:21:12 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 05:39:20 +05:30
|
|
|
|
|
|
|
func TestValidRedirectURI(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
client storage.Client
|
|
|
|
redirectURI string
|
|
|
|
wantValid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
RedirectURIs: []string{"http://foo.com/bar"},
|
|
|
|
},
|
|
|
|
redirectURI: "http://foo.com/bar",
|
|
|
|
wantValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
RedirectURIs: []string{"http://foo.com/bar"},
|
|
|
|
},
|
|
|
|
redirectURI: "http://foo.com/bar/baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
redirectURI: "urn:ietf:wg:oauth:2.0:oob",
|
|
|
|
wantValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
redirectURI: "http://localhost:8080/",
|
|
|
|
wantValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
redirectURI: "http://localhost:991/bar",
|
|
|
|
wantValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
redirectURI: "http://localhost",
|
|
|
|
wantValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
client: storage.Client{
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
redirectURI: "http://localhost.localhost:8080/",
|
|
|
|
wantValid: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
got := validateRedirectURI(test.client, test.redirectURI)
|
|
|
|
if got != test.wantValid {
|
|
|
|
t.Errorf("client=%#v, redirectURI=%q, wanted valid=%t, got=%t",
|
|
|
|
test.client, test.redirectURI, test.wantValid, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 22:45:59 +05:30
|
|
|
|
|
|
|
func TestStorageKeySet(t *testing.T) {
|
|
|
|
s := memory.New(logger)
|
|
|
|
if err := s.UpdateKeys(func(keys storage.Keys) (storage.Keys, error) {
|
|
|
|
keys.SigningKey = &jose.JSONWebKey{
|
|
|
|
Key: testKey,
|
|
|
|
KeyID: "testkey",
|
|
|
|
Algorithm: "RS256",
|
|
|
|
Use: "sig",
|
|
|
|
}
|
|
|
|
keys.SigningKeyPub = &jose.JSONWebKey{
|
|
|
|
Key: testKey.Public(),
|
|
|
|
KeyID: "testkey",
|
|
|
|
Algorithm: "RS256",
|
|
|
|
Use: "sig",
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tokenGenerator func() (jwt string, err error)
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid token",
|
|
|
|
tokenGenerator: func() (string, error) {
|
|
|
|
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: testKey}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
jws, err := signer.Sign([]byte("payload"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return jws.CompactSerialize()
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "token signed by different key",
|
|
|
|
tokenGenerator: func() (string, error) {
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: key}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
jws, err := signer.Sign([]byte("payload"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return jws.CompactSerialize()
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
jwt, err := tc.tokenGenerator()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keySet := &storageKeySet{s}
|
|
|
|
|
|
|
|
_, err = keySet.VerifySignature(context.Background(), jwt)
|
|
|
|
if (err != nil && !tc.wantErr) || (err == nil && tc.wantErr) {
|
|
|
|
t.Fatalf("wantErr = %v, but got err = %v", tc.wantErr, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|