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

Added 'groups_claim' to the okta_app_oauth resource #468

Merged
merged 2 commits into from
Jun 2, 2021
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
5 changes: 5 additions & 0 deletions examples/okta_app_oauth/basic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ resource "okta_app_oauth" "test" {
client_id = "something_from_somewhere"
token_endpoint_auth_method = "client_secret_basic"
consent_method = "TRUSTED"
groups_claim {
type = "EXPRESSION"
value = "aa"
name = "bb"
}
}
112 changes: 112 additions & 0 deletions okta/resource_okta_app_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/okta-sdk-golang/v2/okta"
"github.com/okta/okta-sdk-golang/v2/okta/query"
"github.com/okta/terraform-provider-okta/sdk"
)

type (
Expand Down Expand Up @@ -315,6 +316,38 @@ func resourceAppOAuth() *schema.Resource {
Description: "*Early Access Property*. Enable Federation Broker Mode.",
ConflictsWith: []string{"groups", "users"},
},
"groups_claim": {
Type: schema.TypeSet,
MaxItems: 1,
Description: "Groups claim for an OpenID Connect client application",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Groups claim type.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: stringInSlice([]string{"FILTER", "EXPRESSION"}),
},
"filter_type": {
Description: "Groups claim filter. Can only be set if type is FILTER.",
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: stringInSlice([]string{"EQUALS", "STARTS_WITH", "CONTAINS", "REGEX"}),
},
"name": {
Description: "Name of the claim that will be used in the token.",
Type: schema.TypeString,
Required: true,
},
"value": {
Description: "Value of the claim. Can be an Okta Expression Language statement that evaluates at the time the token is minted.",
Type: schema.TypeString,
Required: true,
},
},
},
},
}),
}
}
Expand Down Expand Up @@ -350,9 +383,50 @@ func resourceAppOAuthCreate(ctx context.Context, d *schema.ResourceData, m inter
if err != nil {
return diag.Errorf("failed to upload logo for OAuth application: %v", err)
}
err = setAppOauthGroupsClaim(ctx, d, m)
if err != nil {
return diag.Errorf("failed to update groups claim for an OAuth application: %v", err)
}
return resourceAppOAuthRead(ctx, d, m)
}

func setAppOauthGroupsClaim(ctx context.Context, d *schema.ResourceData, m interface{}) error {
raw, ok := d.GetOk("groups_claim")
if !ok {
return nil
}
groupsClaim := raw.(*schema.Set).List()[0].(map[string]interface{})
gc := &sdk.AppOauthGroupClaim{
IssuerMode: "ORG_URL",
Name: groupsClaim["name"].(string),
Value: groupsClaim["value"].(string),
}
gct := groupsClaim["type"].(string)
if gct == "FILTER" {
gc.ValueType = "GROUPS"
gc.GroupFilterType = groupsClaim["filter_type"].(string)
} else {
gc.ValueType = gct
}
_, err := getSupplementFromMetadata(m).UpdateAppOauthGroupsClaim(ctx, d.Id(), gc)
return err
}

func updateAppOauthGroupsClaim(ctx context.Context, d *schema.ResourceData, m interface{}) error {
raw, ok := d.GetOk("groups_claim")
if !ok {
return nil
}
if len(raw.(*schema.Set).List()) == 0 {
gc := &sdk.AppOauthGroupClaim{
IssuerMode: "ORG_URL",
}
_, err := getSupplementFromMetadata(m).UpdateAppOauthGroupsClaim(ctx, d.Id(), gc)
return err
}
return setAppOauthGroupsClaim(ctx, d, m)
}

func resourceAppOAuthRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
app := okta.NewOpenIdConnectApplication()
err := fetchApp(ctx, d, m, app)
Expand Down Expand Up @@ -452,6 +526,27 @@ func resourceAppOAuthRead(ctx context.Context, d *schema.ResourceData, m interfa
if err != nil {
return diag.Errorf("failed to set OAuth application properties: %v", err)
}
gc, _, err := getSupplementFromMetadata(m).GetAppOauthGroupsClaim(ctx, d.Id())
if err != nil {
return diag.Errorf("failed to get groups claim for OAuth application: %v", err)
}
if gc.Name != "" {
arr := []map[string]interface{}{
{
"name": gc.Name,
"value": gc.Value,
"type": gc.ValueType,
},
}
if gc.ValueType == "GROUPS" {
arr[0]["type"] = "FILTER"
arr[0]["filter_type"] = gc.GroupFilterType
}
err = setNonPrimitives(d, map[string]interface{}{"groups_claim": arr})
if err != nil {
return diag.Errorf("failed to set OAuth application properties: %v", err)
}
}
return nil
}

Expand Down Expand Up @@ -491,6 +586,10 @@ func resourceAppOAuthUpdate(ctx context.Context, d *schema.ResourceData, m inter
return diag.Errorf("failed to upload logo for OAuth application: %v", err)
}
}
err = updateAppOauthGroupsClaim(ctx, d, m)
if err != nil {
return diag.Errorf("failed to update groups claim for an OAuth application: %v", err)
}
return resourceAppOAuthRead(ctx, d, m)
}

Expand Down Expand Up @@ -638,6 +737,19 @@ func validateAppOAuth(d *schema.ResourceData) error {
if rtr.(string) == "STATIC" && rtl.(int) != 0 {
return errors.New("you can not set 'refresh_token_leeway' when 'refresh_token_rotation' is static")
}
raw, ok := d.GetOk("groups_claim")
if ok {
groupsClaim := raw.(*schema.Set).List()[0].(map[string]interface{})
if groupsClaim["type"].(string) == "EXPRESSION" && groupsClaim["filter_type"].(string) != "" {
return errors.New("'filter_type' in 'groups_claim' can only be set when 'type' is set to 'FILTER'")
}
if groupsClaim["type"].(string) == "FILTER" && groupsClaim["filter_type"].(string) == "" {
return errors.New("'filter_type' in 'groups_claim' is required when 'type' is set to 'FILTER'")
}
if groupsClaim["name"].(string) == "" || groupsClaim["value"].(string) == "" {
return errors.New("'name' 'value' and in 'groups_claim' should not be empty")
}
}
if _, ok := d.GetOk("jwks"); !ok && d.Get("token_endpoint_auth_method").(string) == "private_key_jwt" {
return errors.New("'jwks' is required when 'token_endpoint_auth_method' is 'private_key_jwt'")
}
Expand Down
4 changes: 4 additions & 0 deletions okta/resource_okta_app_oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func TestAccAppOauth_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "client_secret", "something_from_somewhere"),
resource.TestCheckResourceAttr(resourceName, "client_id", "something_from_somewhere"),
resource.TestCheckResourceAttrSet(resourceName, "logo_url"),
resource.TestCheckResourceAttr(resourceName, "groups_claim.0.type", "EXPRESSION"),
resource.TestCheckResourceAttr(resourceName, "groups_claim.0.value", "aa"),
resource.TestCheckResourceAttr(resourceName, "groups_claim.0.name", "bb"),
),
},
{
Expand All @@ -60,6 +63,7 @@ func TestAccAppOauth_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "client_secret"),
resource.TestCheckResourceAttrSet(resourceName, "client_id"),
resource.TestCheckResourceAttrSet(resourceName, "logo_url"),
resource.TestCheckResourceAttr(resourceName, "groups_claim.#", "0"),
),
},
{
Expand Down
40 changes: 40 additions & 0 deletions sdk/app_oauth_group_claim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sdk

import (
"context"
"fmt"
"net/http"

"github.com/okta/okta-sdk-golang/v2/okta"
)

type AppOauthGroupClaim struct {
ValueType string `json:"valueType,omitempty"`
GroupFilterType string `json:"groupFilterType,omitempty"`
Issuer string `json:"issuer,omitempty"`
OrgURL string `json:"orgUrl,omitempty"`
Audience string `json:"audience,omitempty"`
IssuerMode string `json:"issuerMode,omitempty"`
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}

func (m *ApiSupplement) UpdateAppOauthGroupsClaim(ctx context.Context, appID string, gc *AppOauthGroupClaim) (*okta.Response, error) {
url := fmt.Sprintf("/api/v1/internal/apps/%s/settings/oauth/idToken", appID)
req, err := m.RequestExecutor.NewRequest(http.MethodPost, url, gc)
if err != nil {
return nil, err
}
return m.RequestExecutor.Do(ctx, req, nil)
}

func (m *ApiSupplement) GetAppOauthGroupsClaim(ctx context.Context, appID string) (*AppOauthGroupClaim, *okta.Response, error) {
url := fmt.Sprintf("/api/v1/internal/apps/%s/settings/oauth/idToken", appID)
req, err := m.RequestExecutor.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}
gc := &AppOauthGroupClaim{}
resp, err := m.RequestExecutor.Do(ctx, req, gc)
return gc, resp, err
}
6 changes: 6 additions & 0 deletions website/docs/r/app_oauth.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ The following arguments are supported:

- `logo` (Optional) Application logo. The file must be in PNG, JPG, or GIF format, and less than 1 MB in size.

- `groups_claim` - (Optional) Groups claim for an OpenID Connect client application.
- `type` - (Required) Groups claim type. Valid values: `"FILTER"`, `"EXPRESSION"`.
- `filter_type` - (Optional) Groups claim filter. Can only be set if type is `"FILTER"`. Valid values: `"EQUALS"`, `"STARTS_WITH"`, `"CONTAINS"`, `"REGEX"`.
- `name` - (Required) Name of the claim that will be used in the token.
- `value` - (Required) Value of the claim. Can be an Okta Expression Language statement that evaluates at the time the token is minted.

## Attributes Reference

- `id` - ID of the application.
Expand Down