-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
14 changed files
with
422 additions
and
2 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
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
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
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20240603230734 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add Authentik SSO'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE "user" ADD oauth_authentik_id VARCHAR(255) DEFAULT NULL'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE "user" DROP oauth_authentik_id'); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Security; | ||
|
||
use App\Controller\AbstractController; | ||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class AuthentikController extends AbstractController | ||
{ | ||
public function connect(ClientRegistry $clientRegistry): Response | ||
{ | ||
return $clientRegistry | ||
->getClient('authentik') | ||
->redirect([ | ||
'openid', | ||
'email', | ||
'profile', | ||
]); | ||
} | ||
|
||
public function verify(Request $request, ClientRegistry $client) | ||
{ | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Provider; | ||
|
||
use League\OAuth2\Client\Provider\AbstractProvider; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Authentik extends AbstractProvider | ||
{ | ||
use BearerAuthorizationTrait; | ||
|
||
protected $baseUrl; | ||
|
||
public function __construct(array $options = [], array $collaborators = []) | ||
{ | ||
$this->baseUrl = $options['base_url'] ?? ''; | ||
|
||
parent::__construct($options, $collaborators); | ||
} | ||
|
||
protected function getBaseUrl() | ||
{ | ||
return rtrim($this->baseUrl, '/').'/'; | ||
} | ||
|
||
protected function getAuthorizationHeaders($token = null) | ||
{ | ||
return ['Authorization' => 'Bearer '.$token]; | ||
} | ||
|
||
public function getBaseAuthorizationUrl() | ||
{ | ||
return $this->getBaseUrl().'application/o/authorize/'; | ||
} | ||
|
||
public function getBaseAccessTokenUrl(array $params) | ||
{ | ||
return $this->getBaseUrl().'application/o/token/'; | ||
} | ||
|
||
public function getResourceOwnerDetailsUrl(AccessToken $token) | ||
{ | ||
return $this->getBaseUrl().'application/o/userinfo/'; | ||
} | ||
|
||
protected function getDefaultScopes() | ||
{ | ||
return ['openid', 'profile', 'email']; | ||
} | ||
|
||
protected function checkResponse(ResponseInterface $response, $data) | ||
{ | ||
if (!empty($data['error'])) { | ||
$error = htmlentities($data['error'], ENT_QUOTES, 'UTF-8'); | ||
$message = htmlentities($data['error_description'], ENT_QUOTES, 'UTF-8'); | ||
throw new IdentityProviderException($message, $response->getStatusCode(), $response); | ||
} | ||
} | ||
|
||
protected function createResourceOwner(array $response, AccessToken $token) | ||
{ | ||
return new AuthentikResourceOwner($response); | ||
} | ||
|
||
protected function getScopeSeparator() | ||
{ | ||
return ' '; | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Provider; | ||
|
||
use League\OAuth2\Client\Provider\ResourceOwnerInterface; | ||
|
||
class AuthentikResourceOwner implements ResourceOwnerInterface | ||
{ | ||
protected $response; | ||
|
||
public function __construct(array $response) | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->getResponseValue('sub'); | ||
} | ||
|
||
public function getEmail() | ||
{ | ||
return $this->getResponseValue('email'); | ||
} | ||
|
||
public function getFamilyName() | ||
{ | ||
return $this->getResponseValue('family_name'); | ||
} | ||
|
||
public function getGivenName() | ||
{ | ||
return $this->getResponseValue('given_name'); | ||
} | ||
|
||
public function getPreferredUsername() | ||
{ | ||
return $this->getResponseValue('preferred_username'); | ||
} | ||
|
||
public function getPictureUrl() | ||
{ | ||
return $this->getResponseValue('picture'); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return $this->response; | ||
} | ||
|
||
protected function getResponseValue($key) | ||
{ | ||
$keys = explode('.', $key); | ||
$value = $this->response; | ||
|
||
foreach ($keys as $k) { | ||
if (isset($value[$k])) { | ||
$value = $value[$k]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Oops, something went wrong.