Skip to content

Commit

Permalink
Use single associative array parameter instead of positional params
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenclouston committed Nov 29, 2024
1 parent 6e21f59 commit 55141bb
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions lib/Authsignal/Authsignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,44 +85,47 @@ public static function track(array $params)

/**
* Get an action
* @param string $userId The userId of the user you are tracking the action for
* @param string $action The action code that you are tracking
* @param string $idempotencyKey The action code that you are tracking
* @param array $params An associative array of parameters:
* - string 'userId': The userId of the user you are tracking the action for
* - string 'action': The action code that you are tracking
* - string 'idempotencyKey': The idempotency key for the action
* @return Array The authsignal response
*/
public static function getAction(string $userId, string $action, string $idempotencyKey)
public static function getAction(array $params)
{
$request = new AuthsignalClient();
$userId = urlencode($userId);
$action = urlencode($action);
$userId = urlencode($params['userId']);
$action = urlencode($params['action']);
$idempotencyKey = urlencode($params['idempotencyKey']);
list($response, $request) = $request->send("/users/{$userId}/actions/{$action}/{$idempotencyKey}", array(), 'get');

return $response;
}

/**
* Get a user
* @param string $userId The userId of the user you are tracking the action for
* @param string $redirectUrl The redirectUrl if using the redirect flow (optional)
* @param array $params An associative array of parameters:
* - string 'userId': The userId of the user you are tracking the action for
* - string|null 'redirectUrl': The redirectUrl if using the redirect flow (optional)
* @return Array The authsignal response
*/
public static function getUser(string $userId, string $redirectUrl = null)
public static function getUser(array $params)
{
$request = new AuthsignalClient();
$userId = urlencode($userId);
$userId = urlencode($params['userId']);
$redirectUrl = isset($params['redirectUrl']) ? urlencode($params['redirectUrl']) : null;

$redirectUrl = empty($redirectUrl) ? null : urlencode($redirectUrl);

$path = empty($redirectUrl) ? "/users/{$userId}" : "/users/{$userId}?redirectUrl={$redirectUrl}";
list($response, $request) = $request->send($path, null, 'get');

return $response;
}

public static function updateUser(string $userId, array $data)
public static function updateUser(array $params)
{
$request = new AuthsignalClient();
$userId = urlencode($userId);
$userId = urlencode($params['userId']);
$data = $params['data'];
$path = "/users/{$userId}";
list($response, $request) = $request->send($path, $data, 'post');
return $response;
Expand All @@ -131,50 +134,54 @@ public static function updateUser(string $userId, array $data)

/**
* Enroll Authenticators
* @param string $userId The userId of the user you are tracking the action for
* @param Array $authenticator The authenticator object
* @param array $params An associative array of parameters:
* - string 'userId': The userId of the user you are tracking the action for
* - array 'authenticator': The authenticator object
* @return Array The authsignal response
*/
public static function enrollVerifiedAuthenticator(string $userId, Array $authenticator)
public static function enrollVerifiedAuthenticator(array $params)
{
$request = new AuthsignalClient();
$userId = urlencode($userId);
$userId = urlencode($params['userId']);
$authenticator = $params['authenticator'];
list($response, $request) = $request->send("/users/{$userId}/authenticators", $authenticator, 'post');

return $response;
}

/**
* Delete a user
* @param string $userId The userId of the user you want to delete
* @param array $params An associative array of parameters:
* - string 'userId': The userId of the user you want to delete
* @return Array The authsignal response
*/
public static function deleteUser(string $userId)
public static function deleteUser(array $params)
{
$request = new AuthsignalClient();
$userId = urlencode($userId);
$userId = urlencode($params['userId']);
$path = "/users/{$userId}";
list($response, $request) = $request->send($path, null, 'delete');
return $response;
}

/**
* Delete a user authenticator
* @param string $userId The userId of the user
* @param string $userAuthenticatorId The userAuthenticatorId of the authenticator
* @param array $params An associative array of parameters:
* - string 'userId': The userId of the user
* - string 'userAuthenticatorId': The userAuthenticatorId of the authenticator
* @return Array The authsignal response
*/
public static function deleteAuthenticator(string $userId, string $userAuthenticatorId) {
if (empty($userId)) {
*/
public static function deleteAuthenticator(array $params) {
if (empty($params['userId'])) {
throw new InvalidArgumentException('user_id cannot be empty');
}

if (empty($userAuthenticatorId)) {
if (empty($params['userAuthenticatorId'])) {
throw new InvalidArgumentException('user_authenticator_id cannot be empty');
}

$userId = urlencode($userId);
$userAuthenticatorId = urlencode($userAuthenticatorId);
$userId = urlencode($params['userId']);
$userAuthenticatorId = urlencode($params['userAuthenticatorId']);
$path = "/users/{$userId}/authenticators/{$userAuthenticatorId}";

$request = new AuthsignalClient();
Expand All @@ -189,20 +196,20 @@ public static function deleteAuthenticator(string $userId, string $userAuthentic

/**
* Validate Challenge
* Validates the token returned on a challenge response, this is a critical security measure
* also performs a back-end call to validate the state
* @param string|null $userId The userId of the user you are tracking the action for
* @param string $token The JWT token string returned on a challenge response
* @param array $params An associative array of parameters:
* - string 'token': The JWT token string returned on a challenge response
* - string|null 'userId': The userId of the user you are tracking the action for (optional)
* - string|null 'action': The action code that you are tracking (optional)
* @return Array The authsignal response
*/
public static function validateChallenge(string $token, ?string $userId = null, ?string $action = null)
public static function validateChallenge(array $params)
{
$request = new AuthsignalClient();

$payload = [
'userId' => $userId,
'action' => $action,
'token' => $token
'userId' => $params['userId'] ?? null,
'action' => $params['action'] ?? null,
'token' => $params['token']
];

list($response, $request) = $request->send("/validate", $payload, 'post');
Expand All @@ -216,19 +223,19 @@ public static function validateChallenge(string $token, ?string $userId = null,

/**
* Update Action
* Updates the state of an action for a user
* @param string $userId The userId of the user to update the action for
* @param string $action The action code to update
* @param string $idempotencyKey The idempotency key for the action
* @param array $attributes Additional attributes for the action
* @param array $params An associative array of parameters:
* - string 'userId': The userId of the user to update the action for
* - string 'action': The action code to update
* - string 'idempotencyKey': The idempotency key for the action
* - array 'attributes': Additional attributes for the action
* @return array The Authsignal response
*/
public static function updateAction(string $userId, string $action, string $idempotencyKey, array $attributes)
public static function updateAction(array $params)
{
$request = new AuthsignalClient();
$path = "/users/" . urlencode($userId) . "/actions/" . urlencode($action) . "/" . urlencode($idempotencyKey);
$path = "/users/" . urlencode($params['userId']) . "/actions/" . urlencode($params['action']) . "/" . urlencode($params['idempotencyKey']);

list($response, $request) = $request->send($path, $attributes, 'patch');
list($response, $request) = $request->send($path, $params['attributes'], 'patch');
return $response;
}

Expand Down

0 comments on commit 55141bb

Please sign in to comment.