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

Added support for oauth2 config #267

Merged
merged 6 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions auth0/resource_auth0_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ var connectionSchema = map[string]*schema.Schema{
Optional: true,
Description: "",
},
"scripts": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "",
},
"configuration": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Expand Down
80 changes: 80 additions & 0 deletions auth0/resource_auth0_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,86 @@ resource "auth0_connection" "oidc" {
}
`

func TestAccConnectionOAuth2(t *testing.T) {

rand := random.String(6)

resource.Test(t, resource.TestCase{
Providers: map[string]terraform.ResourceProvider{
"auth0": Provider(),
},
Steps: []resource.TestStep{
{
Config: random.Template(testAccConnectionOAuth2Config, rand),
Check: resource.ComposeTestCheckFunc(
random.TestCheckResourceAttr("auth0_connection.oauth2", "name", "Acceptance-Test-OAuth2-{{.random}}", rand),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "strategy", "oauth2"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.client_id", "123456"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.client_secret", "123456"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.token_endpoint", "https://api.login.yahoo.com/oauth2/get_token"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.authorization_endpoint", "https://api.login.yahoo.com/oauth2/request_auth"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.#", "3"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.2517049750", "openid"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.4080487570", "profile"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.881205744", "email"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scripts.fetchUserProfile", "function( { return callback(null) }"),
),
},
{
Config: random.Template(testAccConnectionOAuth2ConfigUpdate, rand),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.client_id", "1234567"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.client_secret", "1234567"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.token_endpoint", "https://api.paypal.com/v1/oauth2/token"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.authorization_endpoint", "https://www.paypal.com/signin/authorize"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.#", "2"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.2517049750", "openid"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scopes.881205744", "email"),
resource.TestCheckResourceAttr("auth0_connection.oauth2", "options.0.scripts.fetchUserProfile", "function( { return callback(null) }"),
),
},
},
})
}

const testAccConnectionOAuth2Config = `

resource "auth0_connection" "oauth2" {
name = "Acceptance-Test-OAuth2-{{.random}}"
strategy = "oauth2"
is_domain_connection = false
options {
client_id = "123456"
client_secret = "123456"
token_endpoint = "https://api.login.yahoo.com/oauth2/get_token"
authorization_endpoint = "https://api.login.yahoo.com/oauth2/request_auth"
scopes = [ "openid", "email", "profile" ]
scripts = {
fetchUserProfile= "function( { return callback(null) }"
}
}
}
`

const testAccConnectionOAuth2ConfigUpdate = `

resource "auth0_connection" "oauth2" {
name = "Acceptance-Test-OAuth2-{{.random}}"
strategy = "oauth2"
is_domain_connection = false
options {
client_id = "1234567"
client_secret = "1234567"
token_endpoint = "https://api.paypal.com/v1/oauth2/token"
authorization_endpoint = "https://www.paypal.com/signin/authorize"
scopes = [ "openid", "email" ]
scripts = {
fetchUserProfile= "function( { return callback(null) }"
}
}
}
`

func TestAccConnectionWithEnbledClients(t *testing.T) {

rand := random.String(6)
Expand Down
29 changes: 29 additions & 0 deletions auth0/structure_auth0_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func flattenConnectionOptions(d ResourceData, options interface{}) []interface{}
m = flattenConnectionOptionsAuth0(d, o)
case *management.ConnectionOptionsGoogleOAuth2:
m = flattenConnectionOptionsGoogleOAuth2(o)
case *management.ConnectionOptionsOAuth2:
m = flattenConnectionOptionsOAuth2(o)
case *management.ConnectionOptionsFacebook:
m = flattenConnectionOptionsFacebook(o)
case *management.ConnectionOptionsApple:
Expand Down Expand Up @@ -81,6 +83,17 @@ func flattenConnectionOptionsGoogleOAuth2(o *management.ConnectionOptionsGoogleO
}
}

func flattenConnectionOptionsOAuth2(o *management.ConnectionOptionsOAuth2) interface{} {
return map[string]interface{}{
"client_id": o.GetClientID(),
"client_secret": o.GetClientSecret(),
"scopes": o.Scopes(),
"token_endpoint": o.GetTokenEndpoint(),
"authorization_endpoint": o.GetAuthorizationEndpoint(),
"scripts": o.Scripts,
}
}

func flattenConnectionOptionsFacebook(o *management.ConnectionOptionsFacebook) interface{} {
return map[string]interface{}{
"client_id": o.GetClientID(),
Expand Down Expand Up @@ -241,6 +254,8 @@ func expandConnection(d ResourceData) *management.Connection {
c.Options = expandConnectionOptionsAuth0(d)
case management.ConnectionStrategyGoogleOAuth2:
c.Options = expandConnectionOptionsGoogleOAuth2(d)
case management.ConnectionStrategyOAuth2:
c.Options = expandConnectionOptionsOAuth2(d)
case management.ConnectionStrategyFacebook:
c.Options = expandConnectionOptionsFacebook(d)
case management.ConnectionStrategyApple:
Expand Down Expand Up @@ -340,6 +355,20 @@ func expandConnectionOptionsGoogleOAuth2(d ResourceData) *management.ConnectionO

return o
}
func expandConnectionOptionsOAuth2(d ResourceData) *management.ConnectionOptionsOAuth2 {

o := &management.ConnectionOptionsOAuth2{
ClientID: String(d, "client_id"),
ClientSecret: String(d, "client_secret"),
AuthorizationEndpoint: String(d, "authorization_endpoint"),
TokenEndpoint: String(d, "token_endpoint"),
}
o.Scripts = Map(d, "scripts")

expandConnectionOptionsScopes(d, o)

return o
}

func expandConnectionOptionsFacebook(d ResourceData) *management.ConnectionOptionsFacebook {

Expand Down
32 changes: 32 additions & 0 deletions docs/resources/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,38 @@ With the `oidc` connection strategy, `options` supports the following arguments:
* `userinfo_endpoint` - (Optional)
* `authorization_endpoint` - (Optional)

### OAuth2

With the `oauth2` connection strategy, `options` supports the following arguments:

* `client_id` - (Optional) OIDC provider client ID.
* `client_secret` - (Optional) OIDC provider client secret.
* `scopes` - (Optional) Scopes required by the connection. The value must be a list, for example `["openid", "profile", "email"]`.
* `token_endpoint` - (Optional)
* `authorization_endpoint` - (Optional)

**Example**:

```hcl
resource "auth0_connection" "oauth2" {
name = "OAuth2-Connection"
strategy = "oauth2"
options {
client_id = "<client-id>"
client_secret = "<client-secret>"
token_endpoint = "https://auth.example.com/oauth2/token"
authorization_endpoint = "https://auth.example.com/oauth2/authorize"
scripts = {
fetchUserProfile = <<EOF
function function(accessToken, ctx, cb) {
return callback(new Error("Whoops!"))
}
EOF
}
}
}
```

### Azure AD

With the `waad` connection strategy, `options` supports the following arguments:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go 1.13
require (
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/terraform-plugin-sdk v1.15.0
gopkg.in/auth0.v4 v4.5.0
gopkg.in/auth0.v4 v4.6.0
)