-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
José San Martin
committed
Apr 23, 2024
1 parent
c60665f
commit d707fe7
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace SlashId\Php\Abstraction; | ||
|
||
use SlashId\Php\Exception\MalformedTokenException; | ||
|
||
class TokenAbstraction extends AbstractionBase | ||
{ | ||
public function validateToken(string $token): bool | ||
{ | ||
return $this->sdk->post('/token/validate', ['token' => $token])['valid'] ?? false; | ||
} | ||
|
||
public function getSubFromToken(string $token): string | ||
{ | ||
if (!str_contains($token, '.')) { | ||
throw new MalformedTokenException('The token is malformed.'); | ||
} | ||
|
||
$tokenParts = explode('.', $token); | ||
|
||
if (count($tokenParts) !== 3) { | ||
throw new MalformedTokenException('The token is malformed.'); | ||
} | ||
|
||
[, $userDataTokenPart] = $tokenParts; | ||
$userData = json_decode(base64_decode($userDataTokenPart), true); | ||
return $userData['sub']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace SlashId\Php\Exception; | ||
|
||
/** | ||
* Exception thrown when an invalid token is informed. | ||
*/ | ||
class MalformedTokenException extends \Exception {} |