Skip to content

Commit

Permalink
Merge pull request #537 from dmstr-forks/master
Browse files Browse the repository at this point in the history
Added Keycloak as a new Auth Client
  • Loading branch information
maxxer authored Feb 9, 2024
2 parents 7b8e192 + a6e2492 commit bb917cd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## dev

- Enh: Keycloak auth client (e.luhr)
- Fix: Social Network Auth (eluhr)

## 1.6.2 Jan 4th, 2024
Expand Down
1 change: 1 addition & 0 deletions docs/guides/social-network-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following is the list of clients supported by the module:
- **Facebook** - `Da\User\AuthClient\Facebook`
- **Github** - `Da\User\AuthClient\Github`
- **Google** - `Da\User\AuthClient\Google`
- **Keycloak** - `Da\User\AuthClient\Keycloak`
- **LinkedIn** - `Da\User\AuthClient\LinkedIn`
- **Twitter** - `Da\User\AuthClient\Twitter`
- **VKontakte** - `Da\User\AuthClient\VKontakte`
Expand Down
55 changes: 55 additions & 0 deletions src/User/AuthClient/Keycloak.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Da\User\AuthClient;

use Da\User\Contracts\AuthClientInterface;
use yii\authclient\OpenIdConnect;

/**
* Example application configuration:
*
* ```php
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'keycloak' => [
* 'class' => 'yii\authclient\clients\Keycloak',
* 'clientId' => 'keycloak_client_id',
* 'clientSecret' => 'keycloak_client_secret',
* 'issuerUrl' => 'http://keycloak/realms/your-realm',
* ],
* ],
* ]
* // ...
* ]
* ```
*/
class Keycloak extends OpenIdConnect implements AuthClientInterface
{
/**
* {@inheritdoc}
*/
public function getEmail()
{
// claim from email scope
return $this->getUserAttributes()['email'] ?? null;
}

/**
* {@inheritdoc}
*/
public function getUserName()
{
// claim from profile scope
return $this->getUserAttributes()['preferred_username'] ?? $this->getEmail();
}

/**
* {@inheritdoc}
*/
public function getUserId()
{
return $this->getUserAttributes()['sub'] ?? null;
}
}

0 comments on commit bb917cd

Please sign in to comment.