Skip to content

Instantly share code, notes, and snippets.

@pitbulk
Last active March 27, 2017 21:37
Show Gist options
  • Save pitbulk/07220f774034dc734ad50f64c08db744 to your computer and use it in GitHub Desktop.
Save pitbulk/07220f774034dc734ad50f64c08db744 to your computer and use it in GitHub Desktop.
Onelogin - Users API
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters
$query_parameters = array(
// -- Search --
'directory_id' => null,
'email' => null,
'external_id' => null,
'firstname' => null,
'id' => null,
'manager_ad_id' => null,
'role_id' => null,
'samaccountname' => null,
'username' => null,
'userprincipalname' => null,
// -- Pagination --
// 'after_cursor' => '',
// 'before_cursor' => '',
// -- Limit --
'limit' => 50, // Max limit (default value)
// -- Sort --
// 'sort' => '+id',
// -- Fields (return only those fields) --
// 'fields' => 'email, username, status',
// -- Since and Until (UTC string value) --
'since' => null,
'until' => null,
);
$query = http_build_query(array_filter($query_parameters));
$authorization = "bearer:$access_token";
if (!empty($query)) {
$url .= '?' . $query;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
} else if (empty($result_data->data)) {
// No result
$users = array();
} else {
$users = $result_data->data;
$before_cursor = $result_data->pagination->before_cursor;
$after_cursor = $result_data->pagination->after_cursor;
// Now if $after_cursor is not empty, I can pass it as
// a parameter of $query_parameters and execute again
// the curl to retrieve all users.
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
// Let's review the parameters used on the different examples listed at
// https://developers.onelogin.com/api-docs/1/users/get-users
// Return the user whose email value equals [email protected]
/*
$query_parameters = array(
'email' => '[email protected]'
);
*/
// Include wildcards. For example, you can return all users whose email values start with Katinka
/*
$query_parameters = array(
'email' => 'katinka*'
);
*/
// Include wildcards. For example, you can return all users whose email values end with @onelogin.com
/*
$query_parameters = array(
'email' => '*@onelogin.com'
);
*/
// Return a limited number of users:
/*
$query_parameters = array(
'limit' => '10'
);
*/
// Return users sorted by id. Use + to sort in ascending order:
/*
$query_parameters = array(
'sort' => '+id'
);
*/
// Return users displaying only selected fields per user:
/*
$query_parameters = array(
'fields' => 'email, username, status'
);
*/
// Combine use of multiple query parameters using an &:
/*
$query_parameters = array(
'role_id' = => '111111',
'sort' => '+id',
'fields' => 'email, username'
);
*/
// Return users created within a window of time bound by specific created_at values using the since and until parameters,
// Ex, return users created after 2010-11-01T19:44:55.681Z, but before 2011-11-01T19:44:55.681 :
/*
$query_parameters = array(
'since' = => '2010-11-01T19:44:55.681Z',
'until' => '2011-11-01T19:44:55.681Z'
);
*/
<?php
// URL & credentials
$url = "https://api.us.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters
$id = "<id>";
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || ". $result_data->status->message;
throw new Exception($errorMsg);
} else {
$user = $result_data->data;
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters
$id = "<id>";
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/apps";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || ". $result_data->status->message;
throw new Exception($errorMsg);
} else {
$apps = $result_data->data;
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters
$id = "<id>";
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/roles";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || ". $result_data->status->message;
throw new Exception($errorMsg);
} else {
if (empty($result_data->data)) {
$roles = array();
} else {
$roles = $result_data->data[0];
}
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/custom_attributes";
$access_token = "<access_token>";
$authorization = "bearer:$access_token";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || ". $result_data->status->message;
throw new Exception($errorMsg);
} else {
if (empty($result_data->data)) {
$custom_attributes = array();
} else {
$custom_attributes = $result_data->data[0];
}
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/";
$access_token = "<access_token>";
// Parameters (user data)
$user_data = array (
# Required
"firstname" => "<firstname>",
# Required
"lastname" => "<lastname>",
# Required
"email" => "<email>",
# Required
"username" => "<username>",
"company" => "",
"department" => "",
"directory_id" => "",
"distinguished_name" => "",
"external_id" => "",
"group_id" => "",
//"invalid_login_attempts" => 0,
"locale_code" => "",
"manager_ad_id" => "",
"member_of" => "",
//"notes" => "",
"openid_name" => "",
"phone" => "",
"samaccountname" => "",
"title" => "",
"userprincipalname" => ""
);
$authorization = "bearer:$access_token";
$data_string = json_encode($user_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
} else {
$user = $result_data->data[0];
print_r($user);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/login/auth";
$access_token = "<access_token>";
// Parameters
$username_or_email = '<username_or_email>';
$password = '<password>';
$subdomain = '<subdomain>'; // Set to the subdomain of the user that you want to log in.
// For example, if your OneLogin URL is splinkly.onelogin.com,
// enter splinkly as the subdomain value.
$origin_uri = ''; // Required for CORS requests only. Set to the Origin URI
// from which you are allowed to send a request using CORS.
// <protocol>://<hostname>:<port>
$authorization = "bearer:$access_token";
$data = array (
"username_or_email" => $username_or_email,
"password" => $password,
"subdomain" => $subdomain
);
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
if (!empty($origin_uri)) {
$header_opts[] = 'Custom-Allowed-Origin-Header-1:'.$origin_uri;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || " . $result_data->status->message;
throw new Exception($errorMsg);
} else {
$user = $result_data->data[0]->user;
if ($result_data->status->message == "Success") {
// MFA not required
$return_to_url = $result_data->data[0]->return_to_url;
$expires_at = $result_data->data[0]->expires_at;
$session_token = $result_data->data[0]->session_token;
} else {
// MFA required
$state_token = $result_data->data[0]->state_token;
$callback_url = $result_data->data[0]->callback_url;
$devices = $result_data->data[0]->devices;
}
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://admin.<us or eu>.onelogin.com/session_via_api_token";
// Parameters
$session_token = '<session_token>';
$data = array (
"session_token" => $session_token
);
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$headers = get_headers_from_curl_response($result);
if (isset($headers['Set-Cookie'])) {
$cookie = $headers['Set-Cookie'];
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
function get_headers_from_curl_response($response)
{
$headers = array();
$header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
foreach (explode("\r\n", $header_text) as $i => $line) {
if ($i === 0) {
$headers['http_code'] = $line;
} else {
list ($key, $value) = explode(': ', $line);
$headers[$key] = $value;
}
}
return $headers;
}
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/login/verify_factor";
$access_token = "<access_token>";
// Parameters
$device_id = '<device_id>';
$state_token = '<state_token>';
$otp_token = '<otp_token>';
$authorization = "bearer:$access_token";
$data = array (
"device_id" => $device_id,
"state_token" => $state_token
);
if (!empty($otp_token)) {
$data['otp_token'] = $otp_token;
}
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
if (!empty($origin_uri)) {
$header_opts[] = 'Custom-Allowed-Origin-Header-1:'.$origin_uri;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || " . $result_data->status->message;
throw new Exception($errorMsg);
} else {
if (isset($result_data->data)) {
$return_to_url = $result_data->data[0]->return_to_url;
$user = $result_data->data[0]->user;
$status = $result_data->data[0]->status;
$session_token = $result_data->data[0]->session_token;
$expires_at = $result_data->data[0]->expires_at;
} else {
//Authentication pending
}
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters (user data)
$user_data = array (
"firstname" => "<new_firstname>",
"lastname" => "<new_lastname>"
// "email" => "[email protected]",
// "username" => "username_test",
// "company" => "",
// "department" => "",
// "directory_id" => "",
// "distinguished_name" => "",
// "external_id" => "",
// "group_id" => "",
// "invalid_login_attempts" => 0,
// "locale_code" => "",
// "manager_ad_id" => "",
// "member_of" => "",
//"notes" => "",
// "openid_name" => "",
// "phone" => "",
// "samaccountname" => "",
// "title" => "",
// "userprincipalname" => ""
);
// Parameters
$id = "id>"; // Id of the user to be updated
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$data_string = json_encode($user_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
} else {
$user = $result_data->data[0];
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters (user data)
$data = array (
"role_id_array" => array(<role_id1>, <roleid2>) // Integers, ids of roles to be assigned
);
// Parameters
$id = "<id>"; // User id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/add_roles";
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users";
$access_token = "<access_token>";
// Parameters (user data)
$data = array (
"role_id_array" => array(<role_id1>, <roleid2>) // Integers, ids of roles to be removed
);
// Parameters
$id = "<id>"; // User id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/remove_roles";
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/set_password_clear_text";
$access_token = "<access_token>";
// Parameters (user data)
$pw_data = array (
"password" => "<password>",
"password_confirmation" => "<password>"
);
// Parameters
$id = "<id>"; // User Id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$data_string = json_encode($pw_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/set_password_using_salt";
$access_token = "<access_token>";
// Parameters (user data)
$pw_cleartext = "<password>";
$salt = "<salt>"; // Leave empty if you want OneLogin to provide the salt value
$password = hash("sha256", $salt.$pw_cleartext);
$pw_data = array (
"password" => $password,
"password_confirmation" => $password,
"password_algorithm" => "salt+sha256",
"password_salt" => $salt
);
// Parameters
$id = "<id>"; // User Id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$data_string = json_encode($pw_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/";
$access_token = "<access_token>";
// Parameters (user data)
$user_data = array (
"custom_attributes" => array(
"<custom_attribute_shortname_1>" => "<value_1>", // use as key the custom attribute shortname
"<custom_attribute_shortname_2>" => "<value_2>"
)
);
// Parameters
$id = "<id>"; // User Id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/set_custom_attributes";
$data_string = json_encode($user_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/";
$access_token = "<access_token>";
// Parameters
$id = "<id>"; // User Id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/logout";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/users/";
$access_token = "<access_token>";
// Parameters (lock data)
$lock_data = array (
"locked_until" => <minutes> // Set number of minutes to be locked (Integer)
); // 0 to block forever or if there is a
// policy defined, lock the time defined
// on the policy.
// Parameters
$id = "<id>"; // User Id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id."/lock_user";
$data_string = json_encode($lock_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.us.onelogin.com/api/1/users/";
$access_token = "<access_token>";
// Parameters
$id = "<id>"; // User Id
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment