Skip to content

Commit

Permalink
feat: allow direct oAuth token authentication (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Sep 9, 2024
1 parent 488242a commit aa52095
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ resource "ec_core_site" "test2" {
- `host` (String) The hostname (in form of URI) of the Enterprise Console API.
- `instances` (Block List) Named Enterprise Console instances. (see [below for nested schema](#nestedblock--instances))
- `password` (String) The password to authenticate with.
- `token` (String) The oAuth token to authenticate with.
- `token_endpoint` (String) The URI to the token authentication endpoint.
- `username` (String) The user to authenticate with.

Expand All @@ -90,5 +91,6 @@ Optional:

- `client_secret` (String) The oAuth2 client secret to authenticate against.
- `password` (String) The password to authenticate with.
- `token` (String) The oAuth token to authenticate with.
- `token_endpoint` (String) The URI to the token authentication endpoint.
- `username` (String) The user to authenticate with.
20 changes: 20 additions & 0 deletions ec/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("EC_HOST", ""),
Description: "The hostname (in form of URI) of the Enterprise Console API.",
},
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("EC_TOKEN", ""),
Description: "The oAuth token to authenticate with.",
},
"token_endpoint": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -76,6 +82,12 @@ func Provider() *schema.Provider {
Required: true,
Description: "The hostname (in form of URI) of the Enterprise Console API.",
},
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("EC_TOKEN", ""),
Description: "The oAuth token to authenticate with.",
},
"token_endpoint": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -191,6 +203,7 @@ func collectConnData(d *schema.ResourceData) map[string]any {
"client_secret": d.Get("client_secret"),
"username": d.Get("username"),
"password": d.Get("password"),
"token": d.Get("token"),
}
}

Expand Down Expand Up @@ -223,6 +236,13 @@ func createRESTConfig(m map[string]any, tok oauth2.TokenSource) rest.Config {
}

func resolveToken(m map[string]any) (oauth2.TokenSource, error) {
if token, ok := m["token"].(string); ok && token != "" {
return oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
TokenType: "Bearer",
}), nil
}

tokURL, err := resolveTokenURL(m)
if err != nil {
return nil, err
Expand Down

0 comments on commit aa52095

Please sign in to comment.