Skip to content

Commit

Permalink
Adding Session And Refresh Tokens Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore7snehil committed Jan 10, 2025
1 parent 3acf0f4 commit 00788c5
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/API/Management/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ public function deleteMultifactorProvider(
->call();
}

public function deleteRefreshTokens(
string $user,
?RequestOptions $options = null,
): ResponseInterface {
[$user] = Toolkit::filter([$user])->string()->trim();

Toolkit::assert([
[$user, \Auth0\SDK\Exception\ArgumentException::missing('user')],
])->isString();

return $this->getHttpClient()
->method('delete')->addPath(['users', $user, 'refresh-tokens'])
->withOptions($options)
->call();
}

public function deleteSessions(
string $user,
?RequestOptions $options = null,
): ResponseInterface {
[$user] = Toolkit::filter([$user])->string()->trim();

Toolkit::assert([
[$user, \Auth0\SDK\Exception\ArgumentException::missing('user')],
])->isString();

return $this->getHttpClient()
->method('delete')->addPath(['users', $user, 'sessions'])
->withOptions($options)
->call();
}

public function get(
string $id,
?RequestOptions $options = null,
Expand Down Expand Up @@ -351,6 +383,22 @@ public function getPermissions(
->call();
}

public function getRefreshTokens(
string $id,
?RequestOptions $options = null,
): ResponseInterface {
[$id] = Toolkit::filter([$id])->string()->trim();

Toolkit::assert([
[$id, \Auth0\SDK\Exception\ArgumentException::missing('id')],
])->isString();

return $this->getHttpClient()
->method('get')->addPath(['users', $id, 'refresh-tokens'])
->withOptions($options)
->call();
}

public function getRoles(
string $id,
?RequestOptions $options = null,
Expand All @@ -367,6 +415,22 @@ public function getRoles(
->call();
}

public function getSessions(
string $id,
?RequestOptions $options = null,
): ResponseInterface {
[$id] = Toolkit::filter([$id])->string()->trim();

Toolkit::assert([
[$id, \Auth0\SDK\Exception\ArgumentException::missing('id')],
])->isString();

return $this->getHttpClient()
->method('get')->addPath(['users', $id, 'sessions'])
->withOptions($options)
->call();
}

public function invalidateBrowsers(
string $id,
?RequestOptions $options = null,
Expand Down
66 changes: 66 additions & 0 deletions src/Contract/API/Management/UsersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ public function deleteMultifactorProvider(
?RequestOptions $options = null,
): ResponseInterface;

/**
* Delete all refresh tokens for a user.
*
* Required scope: `delete:refresh_tokens`.
*
* @param string $user ID of user to query.
* @param null|RequestOptions $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
*
* @see https://auth0.com/docs/api/management/v2#!/Users/delete_refresh_tokens_for_user
*/
public function deleteRefreshTokens(
string $user,
?RequestOptions $options = null,
): ResponseInterface;

/**
* Delete all sessions for a user.
*
* Required scope: `delete:sessions`.
*
* @param string $user ID of user to query.
* @param null|RequestOptions $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
*
* @see https://auth0.com/docs/api/management/v2#!/Users/delete_sessions_for_user
*/
public function deleteSessions(
string $user,
?RequestOptions $options = null,
): ResponseInterface;

/**
* Get a User.
* Required scopes:
Expand Down Expand Up @@ -330,6 +360,24 @@ public function getPermissions(
?RequestOptions $options = null,
): ResponseInterface;

/**
* Retrieve details for a user's refresh tokens.
* Required scopes:
* - `read:refresh_tokens`.
*
* @param string $id user ID to get refresh tokens for
* @param null|RequestOptions $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
*
* @throws \Auth0\SDK\Exception\ArgumentException when an invalid `id` is provided
* @throws \Auth0\SDK\Exception\NetworkException when the API request fails due to a network error
*
* @see https://auth0.com/docs/api/management/v2#!/Users/get_refresh_tokens_for_user
*/
public function getRefreshTokens(
string $id,
?RequestOptions $options = null,
): ResponseInterface;

/**
* Get all roles assigned to a specific user.
* Required scopes:
Expand All @@ -349,6 +397,24 @@ public function getRoles(
?RequestOptions $options = null,
): ResponseInterface;

/**
* Retrieve details for a user's sessions.
* Required scopes:
* - `read: sessions`.
*
* @param string $id user ID to get session details for
* @param null|RequestOptions $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
*
* @throws \Auth0\SDK\Exception\ArgumentException when an invalid `id` is provided
* @throws \Auth0\SDK\Exception\NetworkException when the API request fails due to a network error
*
* @see https://auth0.com/docs/api/management/v2#!/Users/get_sessions_for_user
*/
public function getSessions(
string $id,
?RequestOptions $options = null,
): ResponseInterface;

/**
* Invalidate all remembered browsers across all authentication factors for a user.
* Required scope: `update:users`.
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/API/Management/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,45 @@
$headers = $this->api->getRequestHeaders();
expect($headers['Content-Type'][0])->toEqual('application/json');
});

test('getRefreshTokens() issues an appropriate request', function(): void {
$mockupId = uniqid();

$this->endpoint->getRefreshTokens($mockupId);

expect($this->api->getRequestMethod())->toEqual('GET');
expect($this->api->getRequestUrl())->toStartWith('https://' . $this->api->mock()->getConfiguration()->getDomain() . '/api/v2/users/' . $mockupId . '/refresh-tokens');
});

test('getSessions() issues an appropriate request', function(): void {
$mockupId = uniqid();

$this->endpoint->getSessions($mockupId);

expect($this->api->getRequestMethod())->toEqual('GET');
expect($this->api->getRequestUrl())->toStartWith('https://' . $this->api->mock()->getConfiguration()->getDomain() . '/api/v2/users/' . $mockupId . '/sessions');
});

test('deleteRefreshTokens() issues an appropriate request', function(): void {
$userId = uniqid();

$this->endpoint->deleteRefreshTokens($userId);

expect($this->api->getRequestMethod())->toEqual('DELETE');
expect($this->api->getRequestUrl())->toEndWith('/api/v2/users/' . $userId . '/refresh-tokens');

$headers = $this->api->getRequestHeaders();
expect($headers['Content-Type'][0])->toEqual('application/json');
});

test('deleteSessions() issues an appropriate request', function(): void {
$userId = uniqid();

$this->endpoint->deleteSessions($userId);

expect($this->api->getRequestMethod())->toEqual('DELETE');
expect($this->api->getRequestUrl())->toEndWith('/api/v2/users/' . $userId . '/sessions');

$headers = $this->api->getRequestHeaders();
expect($headers['Content-Type'][0])->toEqual('application/json');
});

0 comments on commit 00788c5

Please sign in to comment.