Skip to content

Commit

Permalink
rename connector;make types private;
Browse files Browse the repository at this point in the history
Signed-off-by: Rui Yang <ruiya@vmware.com>
  • Loading branch information
Rui Yang committed Oct 5, 2022
1 parent 2971d0c commit 177adac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
66 changes: 33 additions & 33 deletions connector/cf/cf.go → connector/cloudfoundry/cloudfoundry.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cf
package cloudfoundry

import (
"context"
Expand All @@ -20,7 +20,7 @@ import (
"github.com/dexidp/dex/pkg/log"
)

type cfConnector struct {
type cloudfoundryConnector struct {
clientID string
clientSecret string
redirectURI string
Expand All @@ -45,56 +45,56 @@ type Config struct {
InsecureSkipVerify bool `json:"insecureSkipVerify"`
}

type CCResponse struct {
type ccResponse struct {
NextURL string `json:"next_url"`
Resources []Resource `json:"resources"`
Resources []resource `json:"resources"`
TotalResults int `json:"total_results"`
}

type Resource struct {
Metadata Metadata `json:"metadata"`
Entity Entity `json:"entity"`
type resource struct {
Metadata metadata `json:"metadata"`
Entity entity `json:"entity"`
}

type Metadata struct {
type metadata struct {
GUID string `json:"guid"`
}

type Entity struct {
type entity struct {
Name string `json:"name"`
OrganizationGUID string `json:"organization_guid"`
}

type Space struct {
type space struct {
Name string
GUID string
OrgGUID string
Role string
}

type Org struct {
type org struct {
Name string
GUID string
}

func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
var err error

cfConn := &cfConnector{
cloudfoundryConn := &cloudfoundryConnector{
clientID: c.ClientID,
clientSecret: c.ClientSecret,
apiURL: c.APIURL,
redirectURI: c.RedirectURI,
logger: logger,
}

cfConn.httpClient, err = newHTTPClient(c.RootCAs, c.InsecureSkipVerify)
cloudfoundryConn.httpClient, err = newHTTPClient(c.RootCAs, c.InsecureSkipVerify)
if err != nil {
return nil, err
}

apiURL := strings.TrimRight(c.APIURL, "/")
apiResp, err := cfConn.httpClient.Get(fmt.Sprintf("%s/v2/info", apiURL))
apiResp, err := cloudfoundryConn.httpClient.Get(fmt.Sprintf("%s/v2/info", apiURL))
if err != nil {
logger.Errorf("failed-to-send-request-to-cloud-controller-api", err)
return nil, err
Expand All @@ -112,7 +112,7 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error)
json.NewDecoder(apiResp.Body).Decode(&apiResult)

uaaURL := strings.TrimRight(apiResult["authorization_endpoint"].(string), "/")
uaaResp, err := cfConn.httpClient.Get(fmt.Sprintf("%s/.well-known/openid-configuration", uaaURL))
uaaResp, err := cloudfoundryConn.httpClient.Get(fmt.Sprintf("%s/.well-known/openid-configuration", uaaURL))
if err != nil {
logger.Errorf("failed-to-send-request-to-uaa-api", err)
return nil, err
Expand All @@ -134,11 +134,11 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error)
return nil, err
}

cfConn.tokenURL, _ = uaaResult["token_endpoint"].(string)
cfConn.authorizationURL, _ = uaaResult["authorization_endpoint"].(string)
cfConn.userInfoURL, _ = uaaResult["userinfo_endpoint"].(string)
cloudfoundryConn.tokenURL, _ = uaaResult["token_endpoint"].(string)
cloudfoundryConn.authorizationURL, _ = uaaResult["authorization_endpoint"].(string)
cloudfoundryConn.userInfoURL, _ = uaaResult["userinfo_endpoint"].(string)

return cfConn, err
return cloudfoundryConn, err
}

func newHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, error) {
Expand Down Expand Up @@ -175,7 +175,7 @@ func newHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err
}, nil
}

func (c *cfConnector) LoginURL(scopes connector.Scopes, callbackURL, state string) (string, error) {
func (c *cloudfoundryConnector) LoginURL(scopes connector.Scopes, callbackURL, state string) (string, error) {
if c.redirectURI != callbackURL {
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
}
Expand All @@ -191,15 +191,15 @@ func (c *cfConnector) LoginURL(scopes connector.Scopes, callbackURL, state strin
return oauth2Config.AuthCodeURL(state), nil
}

func fetchRoleSpaces(baseURL, path, role string, client *http.Client) ([]Space, error) {
func fetchRoleSpaces(baseURL, path, role string, client *http.Client) ([]space, error) {
resources, err := fetchResources(baseURL, path, client)
if err != nil {
return nil, fmt.Errorf("failed to fetch resources: %v", err)
}

spaces := make([]Space, len(resources))
spaces := make([]space, len(resources))
for i, resource := range resources {
spaces[i] = Space{
spaces[i] = space{
Name: resource.Entity.Name,
GUID: resource.Metadata.GUID,
OrgGUID: resource.Entity.OrganizationGUID,
Expand All @@ -210,15 +210,15 @@ func fetchRoleSpaces(baseURL, path, role string, client *http.Client) ([]Space,
return spaces, nil
}

func fetchOrgs(baseURL, path string, client *http.Client) ([]Org, error) {
func fetchOrgs(baseURL, path string, client *http.Client) ([]org, error) {
resources, err := fetchResources(baseURL, path, client)
if err != nil {
return nil, fmt.Errorf("failed to fetch resources: %v", err)
}

orgs := make([]Org, len(resources))
orgs := make([]org, len(resources))
for i, resource := range resources {
orgs[i] = Org{
orgs[i] = org{
Name: resource.Entity.Name,
GUID: resource.Metadata.GUID,
}
Expand All @@ -227,9 +227,9 @@ func fetchOrgs(baseURL, path string, client *http.Client) ([]Org, error) {
return orgs, nil
}

func fetchResources(baseURL, path string, client *http.Client) ([]Resource, error) {
func fetchResources(baseURL, path string, client *http.Client) ([]resource, error) {
var (
resources []Resource
resources []resource
url string
)

Expand All @@ -246,7 +246,7 @@ func fetchResources(baseURL, path string, client *http.Client) ([]Resource, erro
return nil, fmt.Errorf("unsuccessful status code %d", resp.StatusCode)
}

response := CCResponse{}
response := ccResponse{}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to parse spaces: %v", err)
Expand All @@ -263,16 +263,16 @@ func fetchResources(baseURL, path string, client *http.Client) ([]Resource, erro
return resources, nil
}

func getGroupsClaims(orgs []Org, spaces []Space) []string {
func getGroupsClaims(orgs []org, spaces []space) []string {
var (
orgMap = map[string]string{}
orgSpaces = map[string][]Space{}
orgSpaces = map[string][]space{}
groupsClaims = map[string]bool{}
)

for _, org := range orgs {
orgMap[org.GUID] = org.Name
orgSpaces[org.Name] = []Space{}
orgSpaces[org.Name] = []space{}
groupsClaims[org.GUID] = true
groupsClaims[org.Name] = true
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func getGroupsClaims(orgs []Org, spaces []Space) []string {
return groups
}

func (c *cfConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {
func (c *cloudfoundryConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {
q := r.URL.Query()
if errType := q.Get("error"); errType != "" {
return identity, errors.New(q.Get("error_description"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cf
package cloudfoundry

import (
"encoding/json"
Expand Down Expand Up @@ -30,7 +30,7 @@ func TestHandleCallback(t *testing.T) {
testServer := testSetup()
defer testServer.Close()

cfConn := &cfConnector{
cloudfoundryConn := &cloudfoundryConnector{
tokenURL: fmt.Sprintf("%s/token", testServer.URL),
authorizationURL: fmt.Sprintf("%s/authorize", testServer.URL),
userInfoURL: fmt.Sprintf("%s/userinfo", testServer.URL),
Expand All @@ -45,7 +45,7 @@ func TestHandleCallback(t *testing.T) {
expectEqual(t, err, nil)

t.Run("CallbackWithGroupsScope", func(t *testing.T) {
identity, err := cfConn.HandleCallback(connector.Scopes{Groups: true}, req)
identity, err := cloudfoundryConn.HandleCallback(connector.Scopes{Groups: true}, req)
expectEqual(t, err, nil)

expectEqual(t, len(identity.Groups), 24)
Expand Down Expand Up @@ -76,15 +76,15 @@ func TestHandleCallback(t *testing.T) {
})

t.Run("CallbackWithoutGroupsScope", func(t *testing.T) {
identity, err := cfConn.HandleCallback(connector.Scopes{}, req)
identity, err := cloudfoundryConn.HandleCallback(connector.Scopes{}, req)

expectEqual(t, err, nil)
expectEqual(t, identity.UserID, "12345")
expectEqual(t, identity.Username, "test-user")
})

t.Run("CallbackWithOfflineAccessScope", func(t *testing.T) {
identity, err := cfConn.HandleCallback(connector.Scopes{OfflineAccess: true}, req)
identity, err := cloudfoundryConn.HandleCallback(connector.Scopes{OfflineAccess: true}, req)

expectEqual(t, err, nil)
expectNotEqual(t, len(identity.ConnectorData), 0)
Expand Down Expand Up @@ -220,7 +220,7 @@ func testSetup() *httptest.Server {
return httptest.NewServer(mux)
}

func newConnector(t *testing.T, serverURL string) *cfConnector {
func newConnector(t *testing.T, serverURL string) *cloudfoundryConnector {
callBackURL := fmt.Sprintf("%s/callback", serverURL)

testConfig := Config{
Expand All @@ -238,12 +238,12 @@ func newConnector(t *testing.T, serverURL string) *cfConnector {
t.Fatal(err)
}

cfConn, ok := conn.(*cfConnector)
cloudfoundryConn, ok := conn.(*cloudfoundryConnector)
if !ok {
t.Fatal(errors.New("it is not a cf conn"))
t.Fatal(errors.New("it is not a cloudfoundry conn"))
}

return cfConn
return cloudfoundryConn
}

func expectEqual(t *testing.T, a interface{}, b interface{}) {
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/dexidp/dex/connector/atlassiancrowd"
"github.com/dexidp/dex/connector/authproxy"
"github.com/dexidp/dex/connector/bitbucketcloud"
"github.com/dexidp/dex/connector/cf"
"github.com/dexidp/dex/connector/cloudfoundry"
"github.com/dexidp/dex/connector/gitea"
"github.com/dexidp/dex/connector/github"
"github.com/dexidp/dex/connector/gitlab"
Expand Down Expand Up @@ -555,7 +555,7 @@ var ConnectorsConfig = map[string]func() ConnectorConfig{
"bitbucket-cloud": func() ConnectorConfig { return new(bitbucketcloud.Config) },
"openshift": func() ConnectorConfig { return new(openshift.Config) },
"atlassian-crowd": func() ConnectorConfig { return new(atlassiancrowd.Config) },
"cf": func() ConnectorConfig { return new(cf.Config) },
"cloudfoundry": func() ConnectorConfig { return new(cloudfoundry.Config) },
// Keep around for backwards compatibility.
"samlExperimental": func() ConnectorConfig { return new(saml.Config) },
}
Expand Down

0 comments on commit 177adac

Please sign in to comment.