1. Go to this page and download the library: Download boldlygrow/okta-api-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
boldlygrow / okta-api-client example snippets
use BoldlyGrow\Okta\ApiClient;
// Get a list of records
// https://developer.okta.com/docs/reference/api/groups/#list-groups
$groups = ApiClient::get('groups');
// Search for records with a specific name
// This example uses positional arguments
// https://developer.okta.com/docs/reference/core-okta-api/#filter
// https://developer.okta.com/docs/reference/api/groups/#list-groups-with-search
$groups = ApiClient::get('groups', [
'search' => 'profile.name eq "Hack the Planet Engineers"'
]);
// Search for users with a specific
// This example uses positional arguments
// https://developer.okta.com/docs/reference/api/users/#list-users-with-search
$users = ApiClient::get('users', [
'search' => 'profile.firstName eq "Dade"'
]);
// Get a specific record
// https://developer.okta.com/docs/reference/api/groups/#get-group
$group = ApiClient::get('groups/00g1ab2c3D4E5F6G7h8i');
// {
// +"id": "0og1ab2c3D4E5F6G7h8i",
// +"created": "2023-01-01T00:00:00.000Z",
// +"lastUpdated": "2023-02-01T00:00:00.000Z",
// +"lastMembershipUpdated": "2023-03-15T00:00:00.000Z",
// +"type": "OKTA_GROUP",
// +"profile": {
// +"name": "Hack the Planet Engineers",
// +"description": "This group contains engineers that have proven they are elite enough to hack the Gibson.",
// },
// }
$group_name = $group->data->profile->name;
// Hack the Planet Engineers
// Create a group
// https://developer.okta.com/docs/reference/api/groups/#add-group
// This example uses named arguments
$group = ApiClient::post(
uri: 'groups',
data: [
'profile' => [
'name' => 'Hack the Planet Engineers',
'description' => 'This group contains engineers that have proven they are elite enough to hack the Gibson.'
]
]
);
// Update a group
// https://developer.okta.com/docs/reference/api/groups/#update-group
// This example uses named arguments
$group_id = '00g1ab2c3D4E5F6G7h8i';
$group = ApiClient::put(
uri: 'groups/' . $group_id,
data: [
'profile' => [
'description' => 'This group contains engineers that have liberated the garbage files.'
]
]
);
// Delete a group
// https://developer.okta.com/docs/reference/api/groups/#remove-group
$group_id = '00g1ab2c3D4E5F6G7h8i';
ApiClient::delete('groups/' . $group_id);
OKTA_API_URL="https://mycompany.okta.com"
OKTA_API_CLIENT_ID="0oaExampleClientId"
OKTA_API_KEY_ID="the-registered-kid"
# Provide the signing key one of two ways (see Private Key Storage):
# a path to a PEM file (local development or a mounted secret) ...
OKTA_API_PRIVATE_KEY_PATH="/var/secrets/okta/private-key.pem"
# ... or an inline PEM string (often resolved from a secrets manager in code).
# OKTA_API_PRIVATE_KEY=
use BoldlyGrow\Okta\PublicKeyJwk;
$jwk = PublicKeyJwk::fromPemFile('okta-public-key.pem');
// or
$jwk = PublicKeyJwk::fromPem($publicKeyPemString);
use BoldlyGrow\Okta\ApiClient;
// Requests only okta.users.read for this call
$users = ApiClient::get(uri: 'users', scope: 'okta.users.read')->data;
// Requests only okta.groups.read for this call
$groups = ApiClient::get(uri: 'groups', scope: 'okta.groups.read')->data;
use BoldlyGrow\Okta\ApiClient;
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
use Google\Cloud\SecretManager\V1\AccessSecretVersionRequest;
// Fetch the PEM in your own code. Swap this for Vault, AWS Secrets Manager,
// a mounted file, an HTTP call, etc. Cache it as appropriate for your app.
$client = new SecretManagerServiceClient();
$name = $client->secretVersionName('my-gcp-project', 'okta-mycompany-private-key', 'latest');
$privateKey = $client->accessSecretVersion(
(new AccessSecretVersionRequest())->setName($name)
)->getPayload()->getData();
$connection = [
'url' => 'https://mycompany.okta.com',
'client_id' => '0oaExampleClientId',
'key_id' => 'the-registered-kid',
'private_key' => $privateKey,
];
$users = ApiClient::get(uri: 'users', scope: 'okta.users.read', connection: $connection)->data;
// Get a list of records
// https://developer.okta.com/docs/reference/api/groups/#list-groups
$records = ApiClient::get('groups');
// Use variable for endpoint
$endpoint = 'groups';
$records = ApiClient::get($endpoint);
// Get a specific record
// https://developer.okta.com/docs/reference/api/groups/#get-group
$group_id = '0og1ab2c3D4E5F6G7h8i';
$record = ApiClient::get('groups/' . $group_id);
// Get a specific record using a variable
// This assumes that you have a database column named `api_group_id` that
// contains the string with the Okta ID `0og1ab2c3D4E5F6G7h8i`.
$okta_group = \App\Models\OktaGroup::where('id', $id)->firstOrFail();
$record = ApiClient::get('groups/' . $okta_group->api_group_id);
// Named Arguments
$records = ApiClient::get(
uri: 'groups',
data: ['search' => 'profile.name eq "Hack the Planet Engineers"']
);
// Positional Arguments
$records = ApiClient::get('groups', [
'search' => 'profile.name eq "Hack the Planet Engineers"'
]);
// This will parse the array and render the query string
// https://mycompany.okta.com/api/v1/groups?search=profile.name+eq+%22Hack%20the&%20Planet%20Engineers%22
$records = ApiClient::get(
uri: 'users',
data: ['search' => 'status eq "DEPROVISIONED"']
);
// This will parse the array and render the query string
// https://mycompany.okta.com/api/v1/groups?search=status+eq+%22DEPROVISIONED%22
$records = ApiClient::get(
uri: 'users',
data: ['search' => 'profile.department eq "Engineering"']
);
// This will parse the array and render the query string
// https://mycompany.okta.com/api/v1/groups?search=profile.department+eq+%22Engineering%22
// Create a group
// https://developer.okta.com/docs/reference/api/groups/#add-group
$record = ApiClient::post(
uri: 'groups',
data: [
'profile' => [
'name' => 'Hack the Planet Engineers',
'description' => 'This group contains engineers that have proven they are elite enough to hack the Gibson.'
]
]
);
// Update a group
// https://developer.okta.com/docs/reference/api/groups/#update-group
$group_id = '00g1ab2c3D4E5F6G7h8i';
$record = ApiClient::put(
uri: 'groups/' . $group_id,
data: [
'profile' => [
'description' => 'This group contains engineers that have liberated the garbage files.'
]
]
);
// Update a group
// https://developer.okta.com/docs/reference/api/groups/#update-group
$group_id = '00g1ab2c3D4E5F6G7h8i';
$record = ApiClient::put(
uri: 'groups/' . $group_id,
data: [
'profile' => [
'name' => 'Hack the Planet Engineers',
'description' => 'This group contains engineers that have revealed to the world their elite skills.'
]
]
);
// Delete a group
// https://developer.okta.com/docs/reference/api/groups/#remove-group
$group_id = '00g1ab2c3D4E5F6G7h8i';
$record = ApiClient::delete('groups/' . $group_id);
use BoldlyGrow\Okta\ApiClient;
use BoldlyGrow\Okta\Exceptions\NotFoundException;
class OktaGroupService
{
private $connection;
public function __construct(array $connection = [])
{
// If connection is null, use the environment variables
$this->connection = !empty($connection) ? $connection : config('okta-api-client');
}
public function listGroups($query = [])
{
$groups = ApiClient::get(
connection: $this->connection,
uri: 'groups',
data: $query
);
return $groups->data;
}
public function getGroup($id, $query = [])
{
try {
$group = ApiClient::get(
connection: $this->connection,
uri: 'groups/' . $id,
data: $query
);
} catch (NotFoundException $e) {
// Custom logic to handle a record not found. For example, you could
// redirect to a page and flash an alert message.
}
return $group->data;
}
public function storeGroup($request_data)
{
$group = ApiClient::post(
connection: $this->connection,
uri: 'groups',
data: $request_data
);
// To return an object with the newly created group
return $group->data;
// To return the ID of the newly created group
// return $group->data->id;
// To return the status code of the form request
// return $group->status->code;
// To return a bool with the status of the form request
// return $group->status->successful;
// To throw an exception if the request fails
// throw_if(!$group->status->successful, new \Exception($group->error->message, $group->status->code));
// To return the entire API response with the data, headers, and status
// return $group;
}
public function updateGroup($id, $request_data)
{
try {
$group = ApiClient::put(
connection: $this->connection,
uri: 'groups/' . $id,
data: $request_data
);
} catch (NotFoundException $e) {
// Custom logic to handle a record not found. For example, you could
// redirect to a page and flash an alert message.
}
// To return an object with the updated group
return $group->data;
// To return a bool with the status of the form request
// return $group->status->successful;
}
public function deleteGroup($id)
{
try {
$group = ApiClient::delete(
connection: $this->connection,
uri: 'groups/' . $id
);
} catch (NotFoundException $e) {
// Custom logic to handle a record not found. For example, you could
// redirect to a page and flash an alert message.
}
return $group->status->successful;
}
}
$groups = ApiClient::get('groups')->data;
foreach($groups as $group) {
dd($group->profile->name);
// Hack the Planet Engineers
}
use Illuminate\Support\Facades\Cache;
use BoldlyGrow\Okta\ApiClient;
$groups = Cache::remember('okta_groups', now()->addHours(12), function () {
return ApiClient::get('groups')->data;
});
foreach($groups as $group) {
dd($group->profile->name);
// Hack the Planet Engineers
}
$group_id = '00g1ab2c3D4E5F6G7h8i';
$groups = Cache::remember('okta_group_' . $group_id, now()->addHours(12), function () use ($group_id) {
return ApiClient::get('groups/' . $group_id)->data;
});
GET 404 https://example.okta.com/api/v1/users/00u1ab2c3D4E5F6G7h8i (Reason) E0000007 Not found: Resource not found: 00u1ab2c3D4E5F6G7h8i (User)
// {"errorCode":"E0000001","errorSummary":"Api validation failed: profile","errorCauses":[{"errorSummary":"firstName: The field cannot be left blank"},{"errorSummary":"email: Does not match
// {"error":"invalid_client","error_description":"The client secret is invalid."}
POST 400 https://example.okta.com/oauth2/v1/token (Reason) invalid_client The client secret is invalid.
use BoldlyGrow\Okta\Exceptions\NotFoundException;
try {
$group = ApiClient::get('groups/00g1ab2c3D4E5F6G7h8i');
} catch (NotFoundException $e) {
// Group is not found. You can create a log entry, throw an exception, or handle it another way.
Log::error('Okta group could not be found', ['okta_group_id' => $group_id]);
}
OKTA_API_EXCEPTIONS=false
$users = ApiClient::get('users');
$user_collection = collect($users->data)->where('profile.department', 'Security')->toArray();
// This will return an array of users that belong to the Security department based on their profile attribute