Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parameter guards #89

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion custom/lib/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
61 changes: 58 additions & 3 deletions custom/lib/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Passage\Client;

use InvalidArgumentException;
use OpenAPI\Client\Api\TokensApi;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\UsersApi;
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Loading