Edits history of script submission #22593 for ' Assign User to Application (okta)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_app_id = string
    
    // Dropdown of the org's applications so users pick an app instead of typing its opaque ID.
    export async function app_id(auth: RT.Okta) {
      const url = new URL(`${auth.org_url}/api/v1/apps`)
      url.searchParams.append("limit", "200")
      const response = await fetch(url, {
        headers: {
          Authorization: `SSWS ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const apps = (await response.json()) as {
        id: string
        label: string
        name: string
      }[]
      return apps.map((a) => ({
        value: a.id,
        label: a.label ? `${a.label} (${a.id})` : a.id,
      }))
    }
    
    /**
     * Assign User to Application
     * Assign a user to an application. Only `user_id` is required; optionally pass app-specific `credentials` (e.g. { userName }) and a `profile` of app-level attributes.
     */
    export async function main(
      auth: RT.Okta,
      app_id: DynSelect_app_id,
      user_id: string,
      credentials: { [key: string]: any } | undefined,
      profile: { [key: string]: any } | undefined
    ) {
      const url = new URL(`${auth.org_url}/api/v1/apps/${app_id}/users`)
    
      const body: { [key: string]: any } = { id: user_id, scope: "USER" }
      if (credentials !== undefined) body.credentials = credentials
      if (profile !== undefined) body.profile = profile
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `SSWS ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago