Skip to content

Commit

Permalink
feat: add parameter guards (#89)
Browse files Browse the repository at this point in the history
ctran88 authored Dec 10, 2024

Verified

This commit was signed with the committer’s verified signature.
targos Michaël Zasso
1 parent 308894c commit 9317085
Showing 2 changed files with 63 additions and 4 deletions.
6 changes: 5 additions & 1 deletion custom/lib/Auth.php
Original file line number Diff line number Diff line change
@@ -47,11 +47,15 @@ public function __construct(private string $appId, private Configuration $config
* @param string $jwt The authentication token to be validated
*
* @return string User ID of the Passage user
* @throws InvalidArgumentException JWT format is invalid
* @throws InvalidArgumentException Invalid parameter value or JWT format is invalid
* @throws UnexpectedValueException Could not retrieve sub claim from token
*/
public function validateJwt(string $jwt): string
{
if (!$jwt) {
throw new InvalidArgumentException('JWT is required');
}

$jwtSegments = explode('.', $jwt);
if (count($jwtSegments) !== 3) {
throw new InvalidArgumentException('Invalid JWT format');
61 changes: 58 additions & 3 deletions custom/lib/User.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace Passage\Client;

use InvalidArgumentException;
use OpenAPI\Client\Api\TokensApi;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\UsersApi;
@@ -34,10 +35,15 @@ public function __construct(private string $appId, Configuration $config)
*
* @param string $userId The Passage user ID
* @return PassageUser Passage User object
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function get(string $userId): PassageUser
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
return $this->usersApi->getUser($this->appId, $userId)->getUser();
} catch (ApiException $e) {
@@ -50,10 +56,15 @@ public function get(string $userId): PassageUser
*
* @param string $identifier The Passage user email or phone number
* @return PassageUser Passage User object
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function getByIdentifier(string $identifier): PassageUser
{
if (!$identifier) {
throw new InvalidArgumentException('identifier is required');
}

try {
$users = $this->usersApi->listPaginatedUsers(
$this->appId,
@@ -84,10 +95,15 @@ public function getByIdentifier(string $identifier): PassageUser
*
* @param string $userId The Passage user ID
* @return PassageUser Passage User object
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function activate(string $userId): PassageUser
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
return $this->usersApi->activateUser($this->appId, $userId)->getUser();
} catch (ApiException $e) {
@@ -100,10 +116,15 @@ public function activate(string $userId): PassageUser
*
* @param string $userId The Passage user ID
* @return PassageUser Passage User object
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function deactivate(string $userId): PassageUser
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
return $this->usersApi->deactivateUser($this->appId, $userId)->getUser();
} catch (ApiException $e) {
@@ -115,14 +136,19 @@ public function deactivate(string $userId): PassageUser
* Update a user.
*
* @param string $userId The Passage user ID
* @param UpdateUserArgs $args The updated user information
* @param UpdateUserArgs $options The updated user information
* @return PassageUser Passage User Object
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function update(string $userId, UpdateUserArgs $args): PassageUser
public function update(string $userId, UpdateUserArgs $options): PassageUser
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
return $this->usersApi->updateUser($this->appId, $userId, $args)->getUser();
return $this->usersApi->updateUser($this->appId, $userId, $options)->getUser();
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
@@ -133,10 +159,15 @@ public function update(string $userId, UpdateUserArgs $args): PassageUser
*
* @param CreateUserArgs $args Arguments for creating a user
* @return PassageUser Passage User object
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function create(CreateUserArgs $args): PassageUser
{
if (!$args->getEmail() && !$args->getPhone()) {
throw new InvalidArgumentException('At least one of args->email or args->phone is required.');
}

try {
return $this->usersApi->createUser($this->appId, $args)->getUser();
} catch (ApiException $e) {
@@ -149,10 +180,15 @@ public function create(CreateUserArgs $args): PassageUser
*
* @param string $userId The Passage user ID used to delete the corresponding user.
* @return void
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function delete(string $userId): void
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
$this->usersApi->deleteUser($this->appId, $userId);
} catch (ApiException $e) {
@@ -165,10 +201,15 @@ public function delete(string $userId): void
*
* @param string $userId The Passage user ID
* @return WebAuthnDevices[] List of devices
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function listDevices(string $userId): array
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
return $this->userDevicesApi->listUserDevices($this->appId, $userId)->getDevices();
} catch (ApiException $e) {
@@ -182,10 +223,19 @@ public function listDevices(string $userId): array
* @param string $userId The Passage user ID
* @param string $deviceId The Passage user's device ID
* @return void
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function revokeDevice(string $userId, string $deviceId): void
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

if (!$deviceId) {
throw new InvalidArgumentException('deviceId is required');
}

try {
$this->userDevicesApi->deleteUserDevices($this->appId, $userId, $deviceId);
} catch (ApiException $e) {
@@ -198,10 +248,15 @@ public function revokeDevice(string $userId, string $deviceId): void
*
* @param string $userId The Passage user ID
* @return void
* @throws InvalidArgumentException Invalid parameter value
* @throws PassageError
*/
public function revokeRefreshTokens(string $userId): void
{
if (!$userId) {
throw new InvalidArgumentException('userId is required');
}

try {
$this->tokensApi->revokeUserRefreshTokens($this->appId, $userId);
} catch (ApiException $e) {

0 comments on commit 9317085

Please sign in to comment.