Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #141 from mcalster/master
Browse files Browse the repository at this point in the history
Add support for Oauth2 Connection Strategy
  • Loading branch information
alexkappa authored Sep 23, 2020
2 parents ff08785 + 5155087 commit 93ddd3a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
38 changes: 38 additions & 0 deletions management/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
ConnectionStrategyEmail = "email"
ConnectionStrategySMS = "sms"
ConnectionStrategyOIDC = "oidc"
ConnectionStrategyOAuth2 = "oauth2"
ConnectionStrategyAD = "ad"
ConnectionStrategyAzureAD = "waad"
ConnectionStrategySAML = "samlp"
Expand Down Expand Up @@ -126,6 +127,8 @@ func (c *Connection) UnmarshalJSON(b []byte) error {
v = &ConnectionOptionsSMS{}
case ConnectionStrategyOIDC:
v = &ConnectionOptionsOIDC{}
case ConnectionStrategyOAuth2:
v = &ConnectionOptionsOAuth2{}
case ConnectionStrategyAD:
v = &ConnectionOptionsAD{}
case ConnectionStrategyAzureAD:
Expand Down Expand Up @@ -526,6 +529,41 @@ func (c *ConnectionOptionsOIDC) SetScopes(enable bool, scopes ...string) {
c.Scope = &scope
}

type ConnectionOptionsOAuth2 struct {
ClientID *string `json:"client_id,omitempty"`
ClientSecret *string `json:"client_secret,omitempty"`
AuthorizationEndpoint *string `json:"authorization_endpoint"`
TokenEndpoint *string `json:"token_endpoint"`
Scope *string `json:"scope,omitempty"`

// Scripts for the connection
// Allowed keys are: "fetchUserProfile"
Scripts map[string]interface{} `json:"scripts,omitempty"`
}

func (c *ConnectionOptionsOAuth2) Scopes() []string {
return strings.Fields(c.GetScope())
}

func (c *ConnectionOptionsOAuth2) SetScopes(enable bool, scopes ...string) {
scopeMap := make(map[string]bool)
for _, scope := range c.Scopes() {
scopeMap[scope] = true
}
for _, scope := range scopes {
scopeMap[scope] = enable
}
scopeSlice := make([]string, 0, len(scopeMap))
for scope, enabled := range scopeMap {
if enabled {
scopeSlice = append(scopeSlice, scope)
}
}
sort.Strings(scopeSlice)
scope := strings.Join(scopeSlice, " ")
c.Scope = &scope
}

type ConnectionOptionsAD struct {
TenantDomain *string `json:"tenant_domain,omitempty"`
DomainAliases []interface{} `json:"domain_aliases,omitempty"`
Expand Down
18 changes: 18 additions & 0 deletions management/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func TestConnection(t *testing.T) {
_, ok = c.Options.(*ConnectionOptionsSMS)
case ConnectionStrategyOIDC:
_, ok = c.Options.(*ConnectionOptionsOIDC)
case ConnectionStrategyOAuth2:
_, ok = c.Options.(*ConnectionOptionsOAuth2)
case ConnectionStrategyAD:
_, ok = c.Options.(*ConnectionOptionsAD)
case ConnectionStrategyAzureAD:
Expand Down Expand Up @@ -177,6 +179,22 @@ func TestConnectionOptions(t *testing.T) {
expect.Expect(t, o.Scopes(), []string{"bar", "foo"})
})

t.Run("OAuth2", func(t *testing.T) {
o := &ConnectionOptionsOAuth2{
Scripts: map[string]interface{}{"fetchUserProfile": "function( { return callback(null) }"},
}
expect.Expect(t, len(o.Scopes()), 0)

o.SetScopes(true, "foo", "bar", "baz")
expect.Expect(t, len(o.Scopes()), 3)
expect.Expect(t, o.Scopes(), []string{"bar", "baz", "foo"})

o.SetScopes(false, "baz")
expect.Expect(t, len(o.Scopes()), 2)
expect.Expect(t, o.Scopes(), []string{"bar", "foo"})

})

t.Run("Email", func(t *testing.T) {

e := &Connection{
Expand Down
45 changes: 45 additions & 0 deletions management/management.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93ddd3a

Please sign in to comment.