2015-08-18 05:57:27 +05:30
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2016-02-10 04:36:07 +05:30
|
|
|
"encoding/base64"
|
2015-08-18 05:57:27 +05:30
|
|
|
"net/http"
|
2016-02-10 04:36:07 +05:30
|
|
|
"net/url"
|
2015-08-18 05:57:27 +05:30
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
"github.com/coreos/dex/client"
|
2015-08-18 05:57:27 +05:30
|
|
|
schema "github.com/coreos/dex/schema/workerschema"
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientCreate(t *testing.T) {
|
2016-04-15 04:27:53 +05:30
|
|
|
ci := client.Client{
|
2015-08-18 05:57:27 +05:30
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: "72de74a9",
|
2016-02-10 04:36:07 +05:30
|
|
|
Secret: base64.URLEncoding.EncodeToString([]byte("XXX")),
|
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: []url.URL{
|
|
|
|
{Scheme: "https://", Host: "authn.example.com", Path: "/callback"},
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
}
|
2016-04-15 04:27:53 +05:30
|
|
|
cis := []client.Client{ci}
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
srv, err := mockServer(cis)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error setting up server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oidcClient, err := mockClient(srv, ci)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error setting up OIDC client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := oidcClient.ClientCredsToken([]string{"openid"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed getting client token: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackURL := "http://example.com/oidc/callback"
|
|
|
|
trans := &tokenHandlerTransport{
|
|
|
|
Handler: srv.HTTPHandler(),
|
|
|
|
Token: tok.Encode(),
|
|
|
|
}
|
|
|
|
hc := &http.Client{
|
|
|
|
Transport: trans,
|
|
|
|
}
|
|
|
|
iss := srv.IssuerURL.String()
|
|
|
|
svc, err := schema.NewWithBasePath(hc, iss)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed creating API service client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newClientInput := &schema.Client{
|
|
|
|
RedirectURIs: []string{callbackURL, "http://example.com"},
|
|
|
|
}
|
|
|
|
|
|
|
|
call := svc.Clients.Create(newClientInput)
|
|
|
|
newClient, err := call.Do()
|
|
|
|
if err != nil {
|
2016-02-10 04:36:07 +05:30
|
|
|
t.Fatalf("Call to create client API failed: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if newClient.Id == "" {
|
|
|
|
t.Error("Expected non-empty Client ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
if newClient.Secret == "" {
|
|
|
|
t.Error("Expected non-empty Client Secret")
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
meta, err := srv.ClientRepo.Metadata(nil, newClient.Id)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error looking up client metadata: %v", err)
|
|
|
|
} else if meta == nil {
|
|
|
|
t.Error("Expected new client to exist in repo")
|
|
|
|
}
|
|
|
|
|
2016-01-13 06:46:28 +05:30
|
|
|
gotURLs := make([]string, len(meta.RedirectURIs))
|
|
|
|
for i, u := range meta.RedirectURIs {
|
2015-08-18 05:57:27 +05:30
|
|
|
gotURLs[i] = u.String()
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(newClientInput.RedirectURIs, gotURLs) {
|
|
|
|
t.Errorf("Callback URL mismatch, want=%s, got=%s", newClientInput.RedirectURIs, gotURLs)
|
|
|
|
}
|
|
|
|
}
|