-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from xima-media/authentik-resolver
Authentik support
- Loading branch information
Showing
7 changed files
with
168 additions
and
18 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,12 @@ | ||
<?php | ||
|
||
namespace Xima\XimaOauth2Extended\ResourceProvider; | ||
|
||
use League\OAuth2\Client\Provider\GenericProvider; | ||
|
||
class AuthentikResourceProvider extends GenericProvider | ||
{ | ||
use SubResourceOwnerIdTrait; | ||
|
||
use TokenBasedResourceOwnerDetailsTrait; | ||
} |
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
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,14 @@ | ||
<?php | ||
|
||
namespace Xima\XimaOauth2Extended\ResourceProvider; | ||
|
||
use League\OAuth2\Client\Provider\GenericResourceOwner; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
|
||
trait SubResourceOwnerIdTrait | ||
{ | ||
protected function createResourceOwner(array $response, AccessToken $token): GenericResourceOwner | ||
{ | ||
return new GenericResourceOwner($response, 'sub'); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Classes/ResourceProvider/TokenBasedResourceOwnerDetailsTrait.php
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,29 @@ | ||
<?php | ||
|
||
namespace Xima\XimaOauth2Extended\ResourceProvider; | ||
|
||
use League\OAuth2\Client\Token\AccessToken; | ||
|
||
trait TokenBasedResourceOwnerDetailsTrait | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
protected function getRequiredOptions(): array | ||
{ | ||
return [ | ||
'urlAuthorize', | ||
'urlAccessToken', | ||
]; | ||
} | ||
|
||
/** | ||
* @param AccessToken $token | ||
* @return array | ||
*/ | ||
protected function fetchResourceOwnerDetails(AccessToken $token): array | ||
{ | ||
$tokenValues = $token->getValues(); | ||
return (array)json_decode(base64_decode(str_replace('_', '/', str_replace('-', '+', explode('.', $tokenValues['id_token'])[1])))); | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace Xima\XimaOauth2Extended\ResourceResolver; | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class AuthentikResourceResolver extends GenericResourceResolver implements ProfileImageResolverInterface | ||
{ | ||
public function updateBackendUser(array &$beUser): void | ||
{ | ||
$remoteUser = $this->getRemoteUser()->toArray(); | ||
|
||
if (!$beUser['username'] && $remoteUser['email']) { | ||
$beUser['username'] = strtolower($remoteUser['email']); | ||
} | ||
|
||
if ($remoteUser['email']) { | ||
$beUser['email'] = strtolower($remoteUser['email']); | ||
} | ||
|
||
$beUser['disable'] = 0; | ||
|
||
if (!$beUser['realName']) { | ||
$beUser['realName'] = $remoteUser['name']; | ||
} | ||
} | ||
|
||
public function resolveProfileImage(): ?string | ||
{ | ||
$remoteUser = $this->getRemoteUser()->toArray(); | ||
|
||
if (!isset($remoteUser['avatar']) || !$remoteUser['avatar']) { | ||
return null; | ||
} | ||
|
||
$base64Parts = GeneralUtility::trimExplode(',', $remoteUser['avatar']); | ||
|
||
return base64_decode($base64Parts[1]) ?: null; | ||
} | ||
|
||
public function updateFrontendUser(array &$feUser): void | ||
{ | ||
$remoteUser = $this->getRemoteUser()->toArray(); | ||
|
||
if (!$feUser['username'] && $remoteUser['email']) { | ||
$feUser['username'] = strtolower($remoteUser['email']); | ||
} | ||
|
||
if ($remoteUser['email']) { | ||
$feUser['email'] = strtolower($remoteUser['email']); | ||
} | ||
|
||
$feUser['disable'] = 0; | ||
|
||
if (!$feUser['name']) { | ||
$feUser['name'] = $remoteUser['name']; | ||
} | ||
} | ||
} |
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
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