-
Notifications
You must be signed in to change notification settings - Fork 4
refactor: Code cleanup Pt4 #40
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
base: code-cleanup-pt2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| parameters: | ||
| level: 7 | ||
| level: 9 | ||
| paths: | ||
| - ./src | ||
| - ./test |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,7 +32,8 @@ trait SSODataTrait | |||||||||
| */ | ||||||||||
| public function getBranchId(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_BRANCH_ID); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_BRANCH_ID); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -42,7 +43,8 @@ public function getBranchId(): ?string | |||||||||
| */ | ||||||||||
| public function getBranchSlug(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_BRANCH_SLUG); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_BRANCH_SLUG); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -52,7 +54,8 @@ public function getBranchSlug(): ?string | |||||||||
| */ | ||||||||||
| public function getSessionId(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_SESSION_ID); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_SESSION_ID); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -64,7 +67,8 @@ public function getSessionId(): ?string | |||||||||
| */ | ||||||||||
| public function getInstanceId(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_INSTANCE_ID); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_INSTANCE_ID); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -74,7 +78,8 @@ public function getInstanceId(): ?string | |||||||||
| */ | ||||||||||
| public function getInstanceName(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_INSTANCE_NAME); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_INSTANCE_NAME); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -84,7 +89,8 @@ public function getInstanceName(): ?string | |||||||||
| */ | ||||||||||
| public function getUserId(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_ID); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_ID); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -97,7 +103,8 @@ public function getUserId(): ?string | |||||||||
| */ | ||||||||||
| public function getUserExternalId(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_EXTERNAL_ID); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_EXTERNAL_ID); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -107,7 +114,8 @@ public function getUserExternalId(): ?string | |||||||||
| */ | ||||||||||
| public function getUserUsername(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_USERNAME); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_USERNAME); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -117,7 +125,8 @@ public function getUserUsername(): ?string | |||||||||
| */ | ||||||||||
| public function getUserPrimaryEmailAddress(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_PRIMARY_EMAIL_ADDRESS); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_PRIMARY_EMAIL_ADDRESS); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -127,7 +136,8 @@ public function getUserPrimaryEmailAddress(): ?string | |||||||||
| */ | ||||||||||
| public function getFullName(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_FULL_NAME); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_FULL_NAME); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -137,7 +147,8 @@ public function getFullName(): ?string | |||||||||
| */ | ||||||||||
| public function getFirstName(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_FIRST_NAME); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_FIRST_NAME); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -147,20 +158,22 @@ public function getFirstName(): ?string | |||||||||
| */ | ||||||||||
| public function getLastName(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_LAST_NAME); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_LAST_NAME); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Get the type of the token. | ||||||||||
| * | ||||||||||
| * The type of the accessing entity can be either a “user” or a “token”. | ||||||||||
| * The type of the accessing entity can be either a "user" or a "token". | ||||||||||
| * | ||||||||||
| * @return null|string | ||||||||||
| */ | ||||||||||
| public function getType(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_ENTITY_TYPE); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_ENTITY_TYPE); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -172,7 +185,8 @@ public function getType(): ?string | |||||||||
| */ | ||||||||||
| public function getThemeTextColor(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_THEME_TEXT_COLOR); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_THEME_TEXT_COLOR); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -184,7 +198,8 @@ public function getThemeTextColor(): ?string | |||||||||
| */ | ||||||||||
| public function getThemeBackgroundColor(): ?string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_THEME_BACKGROUND_COLOR); | ||||||||||
| $value = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_THEME_BACKGROUND_COLOR); | ||||||||||
| return is_string($value) ? $value : null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -194,7 +209,8 @@ public function getThemeBackgroundColor(): ?string | |||||||||
| */ | ||||||||||
| public function getLocale(): string | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_LOCALE); | ||||||||||
| $val = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_LOCALE); | ||||||||||
| return is_string($val) ? $val : ''; | ||||||||||
|
Comment on lines
+212
to
+213
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
@@ -204,6 +220,7 @@ public function getLocale(): string | |||||||||
| */ | ||||||||||
| public function getTags(): ?array | ||||||||||
| { | ||||||||||
| return $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_TAGS); | ||||||||||
| $val = $this->getClaimSafe(SSODataClaimsInterface::CLAIM_USER_TAGS); | ||||||||||
| return is_array($val) ? $val : null; | ||||||||||
|
Comment on lines
+223
to
+224
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,8 @@ public function getAudience(): ?string | |
| */ | ||
| public function getExpireAtTime(): ?DateTimeImmutable | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_EXPIRE_AT); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_EXPIRE_AT); | ||
| return $value instanceof DateTimeImmutable ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -69,7 +70,8 @@ public function getExpireAtTime(): ?DateTimeImmutable | |
| */ | ||
| public function getNotBeforeTime(): ?DateTimeImmutable | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_NOT_BEFORE); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_NOT_BEFORE); | ||
| return $value instanceof DateTimeImmutable ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -79,7 +81,8 @@ public function getNotBeforeTime(): ?DateTimeImmutable | |
| */ | ||
| public function getIssuedAtTime(): ?DateTimeImmutable | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_ISSUED_AT); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_ISSUED_AT); | ||
| return $value instanceof DateTimeImmutable ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -89,7 +92,8 @@ public function getIssuedAtTime(): ?DateTimeImmutable | |
| */ | ||
| public function getIssuer(): ?string | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_ISSUER); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_ISSUER); | ||
| return is_string($value) ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -99,7 +103,8 @@ public function getIssuer(): ?string | |
| */ | ||
| public function getId(): ?string | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_JWT_ID); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_JWT_ID); | ||
| return is_string($value) ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -109,21 +114,23 @@ public function getId(): ?string | |
| */ | ||
| public function getSubject(): ?string | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_SUBJECT); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_SUBJECT); | ||
| return is_string($value) ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
| * Get the role of the accessing user. | ||
| * | ||
| * If this is set to “editor”, the requesting user may manage the contents | ||
| * If this is set to "editor", the requesting user may manage the contents | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * of the plugin instance, i.e. she has administration rights. | ||
| * The type of the accessing entity can be either a “user” or a “editor”. | ||
| * The type of the accessing entity can be either a "user" or a "editor". | ||
| * | ||
| * @return null|string | ||
| */ | ||
| public function getRole(): ?string | ||
| { | ||
| return $this->getClaimSafe(SharedClaimsInterface::CLAIM_USER_ROLE); | ||
| $value = $this->getClaimSafe(SharedClaimsInterface::CLAIM_USER_ROLE); | ||
| return is_string($value) ? $value : null; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,22 +64,52 @@ public static function createSignedTokenFromData(string $privateKey, array $toke | |
| private static function buildToken(Configuration $config, array $tokenData): Token | ||
| { | ||
| $builder = $config->builder(); | ||
| // Validate and coerce required registered claims to the expected types | ||
| $audience = $tokenData[SSOData\SharedClaimsInterface::CLAIM_AUDIENCE] ?? ''; | ||
| if (!is_string($audience) || $audience === '') { | ||
| throw new \InvalidArgumentException('aud claim must be a non-empty string for token generation'); | ||
| } | ||
|
Comment on lines
+67
to
+71
|
||
|
|
||
| $issuedAt = $tokenData[SSOData\SharedClaimsInterface::CLAIM_ISSUED_AT] ?? null; | ||
| if (!($issuedAt instanceof \DateTimeImmutable)) { | ||
| throw new \InvalidArgumentException('iat claim must be a DateTimeImmutable for token generation'); | ||
| } | ||
|
|
||
| $notBefore = $tokenData[SSOData\SharedClaimsInterface::CLAIM_NOT_BEFORE] ?? null; | ||
| if (!($notBefore instanceof \DateTimeImmutable)) { | ||
| throw new \InvalidArgumentException('nbf claim must be a DateTimeImmutable for token generation'); | ||
| } | ||
|
|
||
| $expiresAt = $tokenData[SSOData\SharedClaimsInterface::CLAIM_EXPIRE_AT] ?? null; | ||
| if (!($expiresAt instanceof \DateTimeImmutable)) { | ||
| throw new \InvalidArgumentException('exp claim must be a DateTimeImmutable for token generation'); | ||
| } | ||
|
|
||
| $token = $builder | ||
| ->permittedFor($tokenData[SSOData\SharedClaimsInterface::CLAIM_AUDIENCE]) | ||
| ->issuedAt($tokenData[SSOData\SharedClaimsInterface::CLAIM_ISSUED_AT]) | ||
| ->canOnlyBeUsedAfter($tokenData[SSOData\SharedClaimsInterface::CLAIM_NOT_BEFORE]) | ||
| ->expiresAt($tokenData[SSOData\SharedClaimsInterface::CLAIM_EXPIRE_AT]); | ||
| ->permittedFor($audience) | ||
| ->issuedAt($issuedAt) | ||
| ->canOnlyBeUsedAfter($notBefore) | ||
| ->expiresAt($expiresAt); | ||
|
|
||
| if (isset($tokenData[SSOData\SharedClaimsInterface::CLAIM_ISSUER])) { | ||
| $token = $token->issuedBy($tokenData[SSOData\SharedClaimsInterface::CLAIM_ISSUER]); | ||
| $issuer = $tokenData[SSOData\SharedClaimsInterface::CLAIM_ISSUER]; | ||
| if (is_string($issuer) && $issuer !== '') { | ||
| $token = $token->issuedBy($issuer); | ||
| } | ||
| } | ||
|
|
||
| if (isset($tokenData[SSOData\SSODataClaimsInterface::CLAIM_USER_ID])) { | ||
| $token = $token->relatedTo($tokenData[SSOData\SSODataClaimsInterface::CLAIM_USER_ID]); | ||
| $subject = $tokenData[SSOData\SSODataClaimsInterface::CLAIM_USER_ID]; | ||
| if (is_string($subject) && $subject !== '') { | ||
| $token = $token->relatedTo($subject); | ||
| } | ||
| } | ||
|
|
||
| if (isset($tokenData[SSOData\SharedClaimsInterface::CLAIM_JWT_ID])) { | ||
| $token = $token->identifiedBy($tokenData[SSOData\SharedClaimsInterface::CLAIM_JWT_ID]); | ||
| $jwtId = $tokenData[SSOData\SharedClaimsInterface::CLAIM_JWT_ID]; | ||
| if (is_string($jwtId) && $jwtId !== '') { | ||
| $token = $token->identifiedBy($jwtId); | ||
| } | ||
| } | ||
|
|
||
| // Remove all set keys as they throw an exception when used with withClaim | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing
deleteInstance()to returnnevercan cause a runtime fatal error if a consumer overridesexitRemoteCall()(it’sprotected) with an implementation that does not terminate execution. In that case,deleteInstance()would reach the end and violate thenevercontract. To avoid breaking extensibility/tests, consider keeping the return typevoidand using a PHPDoc@return neverfor static analysis, or alternatively also declareexitRemoteCall(): neverto enforce the contract consistently.