Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow direct oAuth token authentication #33

Merged
merged 1 commit into from
Sep 9, 2024
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
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
Loading