Skip to content

Commit

Permalink
Add authelia provider (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
YajTPG authored Feb 2, 2025
1 parent e03d25a commit 8e6aead
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
1 change: 1 addition & 0 deletions monorepo-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ parameters:
src/Asana: 'git@github.com:SocialiteProviders/Asana.git'
src/Atlassian: 'git@github.com:SocialiteProviders/Atlassian.git'
src/Auth0: 'git@github.com:SocialiteProviders/Auth0.git'
src/Authelia: 'git@github.com:SocialiteProviders/Authelia.git'
src/Authentik: 'git@github.com:SocialiteProviders/Authentik.git'
src/AutodeskAPS: 'git@github.com:SocialiteProviders/AutodeskAPS.git'
src/Aweber: 'git@github.com:SocialiteProviders/Aweber.git'
Expand Down
13 changes: 13 additions & 0 deletions src/Authelia/AutheliaExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SocialiteProviders\Authelia;

use SocialiteProviders\Manager\SocialiteWasCalled;

class AutheliaExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite('authelia', Provider::class);
}
}
73 changes: 73 additions & 0 deletions src/Authelia/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace SocialiteProviders\Authelia;

use GuzzleHttp\RequestOptions;
use InvalidArgumentException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
public const IDENTIFIER = 'AUTHELIA';

protected $scopes = ['openid', 'profile', 'email', 'groups'];

protected $scopeSeparator = ' ';

public static function additionalConfigKeys(): array
{
return ['base_url'];
}

protected function getBaseUrl()
{
$baseUrl = $this->getConfig('base_url');

if (empty($baseUrl)) {
throw new InvalidArgumentException('Missing base_url');
}

return rtrim($baseUrl, '/');
}

protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase($this->getBaseUrl() . '/api/oidc/authorization', $state);
}

protected function getTokenUrl(): string
{
return $this->getBaseUrl() . '/api/oidc/token';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->getBaseUrl() . '/api/oidc/userinfo', [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer ' . $token,
],
]);

return json_decode((string) $response->getBody(), true);
}

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'email' => $user['email'] ?? null,
'email_verified' => $user['email_verified'] ?? null,
'alt_emails' => $user['alt_emails'] ?? null,
'name' => $user['name'] ?? null,
'preferred_username' => $user['preferred_username'],
'groups' => $user['groups'] ?? null,
'id' => $user['sub'],
]);
}
}
78 changes: 78 additions & 0 deletions src/Authelia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Authelia

```bash
composer require socialiteproviders/authelia
```

## Installation & Basic Usage

Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below.

### Prepare OAuth provider & client in Authelia

Create a new OAuth provider and client within Authelia, according to the Authelia Documentation \
Client: (https://www.authelia.com/configuration/identity-providers/openid-connect/clients/) \
Provider: (https://www.authelia.com/configuration/identity-providers/openid-connect/provider/)


### Add configuration to `config/services.php`

```php
'authelia' => [
'base_url' => env('AUTHELIA_BASE_URL'),
'client_id' => env('AUTHELIA_CLIENT_ID'),
'client_secret' => env('AUTHELIA_CLIENT_SECRET'),
'redirect' => env('AUTHELIA_REDIRECT_URI')
],
```

### Add provider event listener

#### Laravel 11+

In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method.

* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.

```php
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('authelia', \SocialiteProviders\Authelia\Provider::class);
});
```
<details>
<summary>
Laravel 10 or below
</summary>
Configure the package's listener to listen for `SocialiteWasCalled` events.

Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions.

```php
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\Authelia\AutheliaExtendSocialite::class.'@handle',
],
];
```
</details>

### Usage

You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed):

```php
return Socialite::driver('authelia')->redirect();
```

### Returned User Fields

`Note`: For types and scope definitions refer https://www.authelia.com/integration/openid-connect/introduction/#scope-definitions \

- email
- email_verified
- alt_emails
- name
- preferred_username
- groups
- id
33 changes: 33 additions & 0 deletions src/Authelia/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "socialiteproviders/authelia",
"description": "Authelia OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"keywords": [
"authelia",
"laravel",
"oauth",
"provider",
"socialite"
],
"authors": [
{
"name": "yajtpg",
"email": "yajtpg@gmail.com"
}
],
"support": {
"issues": "https://github.com/socialiteproviders/providers/issues",
"source": "https://github.com/socialiteproviders/providers",
"docs": "https://socialiteproviders.com/authentik"
},
"require": {
"php": "^8.2",
"ext-json": "*",
"socialiteproviders/manager": "^4.8"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Authelia\\": ""
}
}
}

0 comments on commit 8e6aead

Please sign in to comment.