Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
Adds support for basic auth application
Browse files Browse the repository at this point in the history
  • Loading branch information
vijetm committed Nov 12, 2019
1 parent bfb2bc4 commit 46aa3a6
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/okta_app_basic_auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# okta_app_bookmark

Represents an Okta Basic Auth App. [See Okta documentation for more details](https://developer.okta.com/docs/reference/api/apps/#add-basic-authentication-application).

* Example of an app with a group association [can be found here](./basic.tf)
* Example of an app with a user association [can be found here](./basic_updated.tf)
10 changes: 10 additions & 0 deletions examples/okta_app_basic_auth/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "okta_group" "group" {
name = "testAcc_replace_with_uuid"
}

resource "okta_app_basic_auth" "test" {
label = "testAcc_replace_with_uuid"
url = "https://example.com/login.html"
auth_url = "https://example.com/auth.html"
groups = ["${okta_group.group.id}"]
}
22 changes: 22 additions & 0 deletions examples/okta_app_basic_auth/basic_updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "okta_user" "user" {
admin_roles = ["APP_ADMIN", "USER_ADMIN"]
first_name = "TestAcc"
last_name = "blah"
login = "[email protected]"
email = "[email protected]"
}

resource "okta_group" "group" {
name = "testAcc_replace_with_uuid"
}

resource "okta_app_basic_auth" "test" {
label = "testAcc_replace_with_uuid"
url = "https://example.com/login.html"
auth_url = "https://example.com/auth.html"

users {
id = "${okta_user.user.id}"
username = "${okta_user.user.email}"
}
}
3 changes: 3 additions & 0 deletions okta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
appAutoLogin = "okta_app_auto_login"
appBookmark = "okta_app_bookmark"
appBasicAuth = "okta_app_basic_auth"
appGroupAssignment = "okta_app_group_assignment"
appUser = "okta_app_user"
appOAuth = "okta_app_oauth"
Expand Down Expand Up @@ -116,6 +117,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
appAutoLogin: resourceAppAutoLogin(),
appBookmark: resourceAppBookmark(),
appBasicAuth: resourceAppBasicAuth(),
appGroupAssignment: resourceAppGroupAssignment(),
appUser: resourceAppUser(),
appOAuth: resourceAppOAuth(),
Expand Down Expand Up @@ -160,6 +162,7 @@ func Provider() terraform.ResourceProvider {
"okta_saml_idp_signing_key": deprecateIncorrectNaming(resourceIdpSigningKey(), idpSamlKey),
"okta_social_idp": deprecateIncorrectNaming(resourceIdpSocial(), idpSocial),
"okta_bookmark_app": deprecateIncorrectNaming(resourceAppBookmark(), appBookmark),
"okta_basic_auth_app": deprecateIncorrectNaming(resourceAppBasicAuth(), appBasicAuth),
"okta_saml_app": deprecateIncorrectNaming(resourceAppSaml(), appSaml),
"okta_oauth_app": deprecateIncorrectNaming(resourceAppOAuth(), appOAuth),
"okta_oauth_app_redirect_uri": deprecateIncorrectNaming(resourceAppOAuthRedirectUri(), appOAuthRedirectUri),
Expand Down
131 changes: 131 additions & 0 deletions okta/resource_okta_app_basic_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package okta

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/okta/okta-sdk-golang/okta"
"github.com/okta/okta-sdk-golang/okta/query"
)

func resourceAppBasicAuth() *schema.Resource {
return &schema.Resource{
CustomizeDiff: func(d *schema.ResourceDiff, v interface{}) error {
return nil
},
Create: resourceAppBasicAuthCreate,
Read: resourceAppBasicAuthRead,
Update: resourceAppBasicAuthUpdate,
Delete: resourceAppBasicAuthDelete,
Exists: resourceAppExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: buildAppSchemaWithVisibility(map[string]*schema.Schema{
"auth_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Login button field",
},
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Login password field",
},
}),
}
}

func resourceAppBasicAuthCreate(d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
app := buildAppBasicAuth(d, m)
activate := d.Get("status").(string) == "ACTIVE"
params := &query.Params{Activate: &activate}
_, _, err := client.Application.CreateApplication(app, params)

if err != nil {
return err
}

d.SetId(app.Id)

err = handleAppGroupsAndUsers(app.Id, d, m)

if err != nil {
return err
}

return resourceAppBasicAuthRead(d, m)
}

func resourceAppBasicAuthRead(d *schema.ResourceData, m interface{}) error {
app := okta.NewBasicAuthApplication()
err := fetchApp(d, m, app)

if app == nil {
d.SetId("")
return nil
}

if err != nil {
return err
}

d.Set("url", app.Settings.App.Url)
d.Set("auth_url", app.Settings.App.AuthURL)
appRead(d, app.Name, app.Status, app.SignOnMode, app.Label, app.Accessibility, app.Visibility)

return syncGroupsAndUsers(app.Id, d, m)
}

func resourceAppBasicAuthUpdate(d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
app := buildAppBasicAuth(d, m)
_, _, err := client.Application.UpdateApplication(d.Id(), app)

if err != nil {
return err
}

desiredStatus := d.Get("status").(string)
err = setAppStatus(d, client, app.Status, desiredStatus)

if err != nil {
return err
}

err = handleAppGroupsAndUsers(app.Id, d, m)

if err != nil {
return err
}

return resourceAppBasicAuthRead(d, m)
}

func resourceAppBasicAuthDelete(d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
_, err := client.Application.DeactivateApplication(d.Id())
if err != nil {
return err
}

_, err = client.Application.DeleteApplication(d.Id())

return err
}

func buildAppBasicAuth(d *schema.ResourceData, m interface{}) *okta.BasicAuthApplication {
// Abstracts away name and SignOnMode which are constant for this app type.
app := okta.NewBasicAuthApplication()
app.Label = d.Get("label").(string)

app.Settings = &okta.BasicApplicationSettings{
App: &okta.BasicApplicationSettingsApplication{
AuthURL: d.Get("auth_url").(string),
Url: d.Get("url").(string),
},
}
app.Visibility = buildVisibility(d)

return app
}
48 changes: 48 additions & 0 deletions okta/resource_okta_app_basic_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package okta

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/okta/okta-sdk-golang/okta"
)

func TestAccAppBasicAuthApplication_crud(t *testing.T) {
ri := acctest.RandInt()
mgr := newFixtureManager(appBasicAuth)
config := mgr.GetFixtures("basic.tf", ri, t)
updatedConfig := mgr.GetFixtures("basic_updated.tf", ri, t)
resourceName := fmt.Sprintf("%s.test", appBasicAuth)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: createCheckResourceDestroy(appBasicAuth, createDoesAppExist(okta.NewBasicAuthApplication())),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
ensureResourceExists(resourceName, createDoesAppExist(okta.NewBasicAuthApplication())),
resource.TestCheckResourceAttr(resourceName, "label", buildResourceName(ri)),
resource.TestCheckResourceAttr(resourceName, "status", "ACTIVE"),
resource.TestCheckResourceAttr(resourceName, "url", "https://example.com/login.html"),
resource.TestCheckResourceAttr(resourceName, "auth_url", "https://example.com/auth.html"),
resource.TestCheckResourceAttr(resourceName, "groups.#", "1"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
ensureResourceExists(resourceName, createDoesAppExist(okta.NewBasicAuthApplication())),
resource.TestCheckResourceAttr(resourceName, "label", buildResourceName(ri)),
resource.TestCheckResourceAttr(resourceName, "status", "ACTIVE"),
resource.TestCheckResourceAttr(resourceName, "url", "https://example.com/login.html"),
resource.TestCheckResourceAttr(resourceName, "auth_url", "https://example.com/auth.html"),
resource.TestCheckResourceAttr(resourceName, "users.#", "1"),
),
},
},
})
}

0 comments on commit 46aa3a6

Please sign in to comment.