Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
gaokevin1 committed Oct 28, 2024
1 parent 5567585 commit 1806bdf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
40 changes: 16 additions & 24 deletions src/SDK/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ class API
private $projectId;
private $managementKey;

const SESSION_TOKEN_NAME = 'sessionToken';
const REFRESH_SESSION_TOKEN_NAME = 'refreshSessionToken';
const COOKIE_DATA_NAME = 'cookieData';

const SESSION_COOKIE_NAME = "DS";
const REFRESH_SESSION_COOKIE_NAME = "DSR";
const REDIRECT_LOCATION_COOKIE_NAME = "Location";

/**
* Constructor for API class.
*
Expand Down Expand Up @@ -244,21 +236,21 @@ private function generateAuthInfo(array $responseBody, ?string $refreshToken, bo
$stJwt = $responseBody['sessionJwt'] ?? '';

if ($stJwt) {
$jwtResponse[self::SESSION_TOKEN_NAME] = $stJwt;
$jwtResponse[EndpointsV1::SESSION_TOKEN_NAME] = $stJwt;
}

$rtJwt = $responseBody['refreshJwt'] ?? '';

if ($refreshToken) {
$jwtResponse[self::REFRESH_SESSION_TOKEN_NAME] = $refreshToken;
$jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME] = $refreshToken;
} elseif ($rtJwt) {
$jwtResponse[self::REFRESH_SESSION_TOKEN_NAME] = $rtJwt;
$jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME] = $rtJwt;
}

$jwtResponse = $this->adjustProperties($jwtResponse, $userJwt);

if ($userJwt) {
$jwtResponse[self::COOKIE_DATA_NAME] = [
$jwtResponse[EndpointsV1::COOKIE_DATA_NAME] = [
'exp' => $responseBody['cookieExpiration'] ?? 0,
'maxAge' => $responseBody['cookieMaxAge'] ?? 0,
'domain' => $responseBody['cookieDomain'] ?? '',
Expand All @@ -271,29 +263,29 @@ private function generateAuthInfo(array $responseBody, ?string $refreshToken, bo

private function adjustProperties(array $jwtResponse, bool $userJwt): array
{
if (isset($jwtResponse[self::SESSION_TOKEN_NAME])) {
$jwtResponse['permissions'] = $jwtResponse[self::SESSION_TOKEN_NAME]['permissions'] ?? [];
$jwtResponse['roles'] = $jwtResponse[self::SESSION_TOKEN_NAME]['roles'] ?? [];
$jwtResponse['tenants'] = $jwtResponse[self::SESSION_TOKEN_NAME]['tenants'] ?? [];
} elseif (isset($jwtResponse[self::REFRESH_SESSION_TOKEN_NAME])) {
$jwtResponse['permissions'] = $jwtResponse[self::REFRESH_SESSION_TOKEN_NAME]['permissions'] ?? [];
$jwtResponse['roles'] = $jwtResponse[self::REFRESH_SESSION_TOKEN_NAME]['roles'] ?? [];
$jwtResponse['tenants'] = $jwtResponse[self::REFRESH_SESSION_TOKEN_NAME]['tenants'] ?? [];
if (isset($jwtResponse[EndpointsV1::SESSION_TOKEN_NAME])) {
$jwtResponse['permissions'] = $jwtResponse[EndpointsV1::SESSION_TOKEN_NAME]['permissions'] ?? [];
$jwtResponse['roles'] = $jwtResponse[EndpointsV1::SESSION_TOKEN_NAME]['roles'] ?? [];
$jwtResponse['tenants'] = $jwtResponse[EndpointsV1::SESSION_TOKEN_NAME]['tenants'] ?? [];
} elseif (isset($jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME])) {
$jwtResponse['permissions'] = $jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME]['permissions'] ?? [];
$jwtResponse['roles'] = $jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME]['roles'] ?? [];
$jwtResponse['tenants'] = $jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME]['tenants'] ?? [];
} else {
$jwtResponse['permissions'] = $jwtResponse['permissions'] ?? [];
$jwtResponse['roles'] = $jwtResponse['roles'] ?? [];
$jwtResponse['tenants'] = $jwtResponse['tenants'] ?? [];
}

$issuer = $jwtResponse[self::SESSION_TOKEN_NAME]['iss'] ??
$jwtResponse[self::REFRESH_SESSION_TOKEN_NAME]['iss'] ??
$issuer = $jwtResponse[EndpointsV1::SESSION_TOKEN_NAME]['iss'] ??
$jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME]['iss'] ??
$jwtResponse['iss'] ?? '';

$issuerParts = explode("/", $issuer);
$jwtResponse['projectId'] = end($issuerParts);

$sub = $jwtResponse[self::SESSION_TOKEN_NAME]['sub'] ??
$jwtResponse[self::REFRESH_SESSION_TOKEN_NAME]['sub'] ??
$sub = $jwtResponse[EndpointsV1::SESSION_TOKEN_NAME]['sub'] ??
$jwtResponse[EndpointsV1::REFRESH_TOKEN_NAME]['sub'] ??
$jwtResponse['sub'] ?? '';

if ($userJwt) {
Expand Down
16 changes: 8 additions & 8 deletions src/SDK/DescopeSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(array $config)
*/
public function verify($sessionToken = null)
{
$sessionToken = $sessionToken ?? $_COOKIE[SESSION_COOKIE] ?? null;
$sessionToken = $sessionToken ?? $_COOKIE[EndpointsV1::SESSION_COOKIE_NAME_NAME] ?? null;

if (!$sessionToken) {
throw new \InvalidArgumentException('Session token is required.');
Expand All @@ -77,7 +77,7 @@ public function verify($sessionToken = null)
*/
public function refreshSession($refreshToken = null)
{
$refreshToken = $refreshToken ?? $_COOKIE[REFRESH_COOKIE] ?? null;
$refreshToken = $refreshToken ?? $_COOKIE[EndpointsV1::REFRESH_COOKIE_NAME] ?? null;

if (!$refreshToken) {
throw new \InvalidArgumentException('Refresh token is required.');
Expand All @@ -97,8 +97,8 @@ public function refreshSession($refreshToken = null)
*/
public function verifyAndRefreshSession($sessionToken = null, $refreshToken = null)
{
$sessionToken = $sessionToken ?? $_COOKIE[SESSION_COOKIE] ?? null;
$refreshToken = $refreshToken ?? $_COOKIE[REFRESH_COOKIE] ?? null;
$sessionToken = $sessionToken ?? $_COOKIE[EndpointsV1::SESSION_COOKIE_NAME] ?? null;
$refreshToken = $refreshToken ?? $_COOKIE[EndpointsV1::REFRESH_COOKIE_NAME] ?? null;

if (!$sessionToken || !$refreshToken) {
throw new \InvalidArgumentException('Session token and refresh token are required.');
Expand All @@ -117,7 +117,7 @@ public function verifyAndRefreshSession($sessionToken = null, $refreshToken = nu
*/
public function getClaims($token = null)
{
$token = $token ?? $_COOKIE[SESSION_COOKIE] ?? null;
$token = $token ?? $_COOKIE[EndpointsV1::SESSION_COOKIE_NAME] ?? null;

if (!$token) {
throw new \InvalidArgumentException('Token is required.');
Expand All @@ -136,7 +136,7 @@ public function getClaims($token = null)
*/
public function getUserDetails(string $refreshToken = null)
{
$refreshToken = $refreshToken ?? $_COOKIE[REFRESH_COOKIE] ?? null;
$refreshToken = $refreshToken ?? $_COOKIE[EndpointsV1::REFRESH_COOKIE_NAME] ?? null;

if (!$refreshToken) {
throw new \InvalidArgumentException('Refresh token is required.');
Expand All @@ -158,7 +158,7 @@ public function getUserDetails(string $refreshToken = null)
*/
public function logout(string $refreshToken = null): void
{
$refreshToken = $refreshToken ?? $_COOKIE[REFRESH_COOKIE] ?? null;
$refreshToken = $refreshToken ?? $_COOKIE[EndpointsV1::REFRESH_COOKIE_NAME] ?? null;

if (!$refreshToken) {
throw new \InvalidArgumentException('Refresh token is required.');
Expand All @@ -181,7 +181,7 @@ public function logout(string $refreshToken = null): void
*/
public function logoutAll(string $refreshToken = null): void
{
$refreshToken = $refreshToken ?? $_COOKIE[REFRESH_COOKIE] ?? null;
$refreshToken = $refreshToken ?? $_COOKIE[EndpointsV1::REFRESH_COOKIE_NAME] ?? null;

if (!$refreshToken) {
throw new \InvalidArgumentException('Refresh token is required.');
Expand Down
10 changes: 10 additions & 0 deletions src/SDK/EndpointsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
const SESSION_COOKIE = "DS";
const REFRESH_COOKIE = "DSR";

const SESSION_TOKEN = "sessionToken";
const REFRESH_TOKEN = "refreshSessionToken";

const REDIRECT_LOCATION_NAME = "Location";

const PHONE_REGEX = '/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?){0,}((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/';

class EndpointsV1
Expand All @@ -21,6 +26,11 @@ class EndpointsV1
public static $SESSION_COOKIE_NAME = SESSION_COOKIE;
public static $REFRESH_COOKIE_NAME = REFRESH_COOKIE;

public static $SESSION_TOKEN_NAME = SESSION_TOKEN;
public static $REFRESH_TOKEN_NAME = REFRESH_TOKEN;

public static $REDIRECT_LOCATION_COOKIE_NAME = REDIRECT_LOCATION_NAME;

public static $REFRESH_TOKEN_PATH;
public static $SELECT_TENANT_PATH;
public static $LOGOUT_PATH;
Expand Down

0 comments on commit 1806bdf

Please sign in to comment.