Skip to content

Commit a954e36

Browse files
committed
chore: add tenant logic
chore: define custom provider for LFID
1 parent 930c37a commit a954e36

File tree

7 files changed

+165
-31
lines changed

7 files changed

+165
-31
lines changed

app/Providers/AppServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\libs\Utils\TextUtils;
1616
use Illuminate\Support\Facades\App;
1717
use Illuminate\Support\Facades\Config;
18+
use Illuminate\Support\Facades\Event;
1819
use Illuminate\Support\Facades\Log;
1920
use Illuminate\Support\ServiceProvider;
2021
use Illuminate\Support\Facades\Validator;
@@ -127,6 +128,11 @@ public function boot()
127128

128129
return true;
129130
});
131+
132+
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
133+
// custom tenants for AUTH0 providers
134+
$event->extendSocialite('lfid', \SocialiteProviders\Auth0\Provider::class);
135+
});
130136
}
131137

132138
/**
Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?php namespace App\libs\Auth;
2-
use Illuminate\Support\Facades\Config;
3-
42
/**
53
* Copyright 2021 OpenStack Foundation
64
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +12,10 @@
1412
* limitations under the License.
1513
**/
1614

15+
use Illuminate\Support\Facades\Config;
16+
use Illuminate\Support\Facades\Log;
17+
use Illuminate\Support\Facades\Request;
18+
1719
/**
1820
* Class SocialLoginProviders
1921
* @package App\libs\Auth
@@ -25,44 +27,109 @@ final class SocialLoginProviders
2527
const LinkedIn = "linkedin";
2628
const Google = "google";
2729
const OKTA = 'okta';
28-
29-
const AUTH0 = 'auth0';
30+
const LFID = 'lfid';
3031

3132
const ValidProviders = [
3233
self::Facebook,
3334
self::LinkedIn,
3435
self::Apple,
3536
//self::Google
3637
self::OKTA,
37-
self::AUTH0,
38+
self::LFID,
3839
];
3940

4041
/**
4142
* @param string $provider
4243
* @return bool
4344
*/
44-
public static function isSupportedProvider(string $provider):bool{
45+
public static function isSupportedProvider(string $provider): bool
46+
{
4547
return in_array($provider, self::ValidProviders);
4648
}
4749

48-
/**
49-
* @param string $provider
50-
* @return bool
51-
*/
52-
public static function isEnabledProvider(string $provider):bool{
53-
return !empty(Config::get("services.".$provider.".client_id", null)) &&
54-
!empty(Config::get("services.".$provider.".client_secret", null));
55-
}
56-
5750
/**
5851
* @return string[]
5952
*/
60-
public static function buildSupportedProviders():array{
53+
public static function buildSupportedProviders(): array
54+
{
6155
$res = [];
62-
foreach(self::ValidProviders as $provider){
63-
if(self::isEnabledProvider($provider))
64-
$res[$provider] = ucfirst($provider);
56+
$tenant = trim(Request::get('tenant', ''));
57+
$allowed_3rd_party_providers = self::toList(
58+
Config::get("tenants.$tenant.allowed_3rd_party_providers", '')
59+
);
60+
61+
Log::debug("SocialLoginProviders::buildSupportedProviders", ["tenant" => $tenant, "allowed_3rd_party_providers" => $allowed_3rd_party_providers]);
62+
foreach (self::ValidProviders as $provider) {
63+
Log::debug("SocialLoginProviders::buildSupportedProviders", ["tenant" => $tenant, "provider" => $provider]);
64+
65+
if (!self::isEnabledProvider($provider)) {
66+
Log::warning("SocialLoginProviders::buildSupportedProviders provider is not enabled.", ["tenant" => $tenant, "provider" => $provider]);
67+
continue;
68+
}
69+
70+
// check if the 3rd party provider has defined some exclusive tenants ...
71+
$tenants = self::toList(
72+
Config::get("services.$provider.tenants", '')
73+
);
74+
75+
Log::debug(sprintf("SocialLoginProviders::buildSupportedProviders provider %s is enabled", $provider));
76+
// 1. check if we have exclusive tenants defined at provider level
77+
if (count($tenants) > 0 && !in_array($tenant, $tenants)) {
78+
// tenant is not defined on the exclusive collection of the provider
79+
Log::warning
80+
(
81+
sprintf
82+
(
83+
"SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s",
84+
$provider,
85+
$tenant
86+
),
87+
["tenants" => $tenants]
88+
);
89+
continue;
90+
}
91+
// 2. check if the tenant has that provider enabled
92+
if (count($tenants) == 0 && !empty($tenant) && !in_array($provider, $allowed_3rd_party_providers)) {
93+
Log::warning
94+
(
95+
sprintf
96+
(
97+
"SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s",
98+
$provider,
99+
$tenant
100+
),
101+
["allowed_3rd_party_providers" => $allowed_3rd_party_providers]
102+
);
103+
continue;
104+
}
105+
106+
Log::debug(sprintf("SocialLoginProviders::buildSupportedProviders provider %s is added", $provider));
107+
$res[$provider] = ucfirst($provider);
65108
}
109+
66110
return $res;
67111
}
112+
113+
private static function toList($value): array
114+
{
115+
if (is_array($value)) {
116+
return array_values(array_filter(array_map('trim', $value), static fn($v) => $v !== ''));
117+
}
118+
if (is_string($value)) {
119+
if ($value === '') return [];
120+
return array_values(array_filter(array_map('trim', explode(',', $value)), static fn($v) => $v !== ''));
121+
}
122+
return [];
123+
}
124+
125+
/**
126+
* @param string $provider
127+
* @return bool
128+
*/
129+
public static function isEnabledProvider(string $provider): bool
130+
{
131+
return !empty(Config::get("services." . $provider . ".client_id", null)) &&
132+
!empty(Config::get("services." . $provider . ".client_secret", null));
133+
}
134+
68135
}

app/libs/OAuth2/Discovery/DiscoveryDocumentBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ public function addUserInfoEncryptionEncSupported($enc)
261261
* @return $this
262262
*/
263263
public function addAvailableThirdPartyIdentityProviders(){
264-
foreach(SocialLoginProviders::ValidProviders as $provider)
265-
if(SocialLoginProviders::isEnabledProvider($provider))
266-
$this->addArrayValue("third_party_identity_providers", $provider);
264+
$providers = SocialLoginProviders::buildSupportedProviders();
265+
foreach($providers as $provider => $value)
266+
$this->addArrayValue("third_party_identity_providers", $provider);
267267
return $this;
268268
}
269269

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"s-ichikawa/laravel-sendgrid-driver": "^4.0",
5151
"smarcet/jose4php": "2.0.0",
5252
"socialiteproviders/apple": "^5.6.1",
53+
"socialiteproviders/auth0": "^4.2",
5354
"socialiteproviders/facebook": "^4.1.0",
5455
"socialiteproviders/google": "^4.1.0",
5556
"socialiteproviders/linkedin": "^5.0.0",

composer.lock

Lines changed: 51 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<?php
2+
$custom_auth0_tenants = [
3+
'lfid' => [
4+
'client_id' => env('LFID_CLIENT_ID'),
5+
'client_secret' => env('LFID_CLIENT_SECRET'),
6+
'redirect' => env('LFID_REDIRECT_URI'),
7+
'base_url' => env('LFID_BASE_URL'),
8+
'tenants' => env('LFID_TENANTS','lf'),
9+
]
10+
];
211

3-
return [
12+
return array_merge([
413

514
/*
615
|--------------------------------------------------------------------------
@@ -66,10 +75,4 @@
6675
'base_url' => env("OKTA_BASE_URL"),
6776
'redirect' => env('OKTA_REDIRECT_URI')
6877
],
69-
'auth0' => [
70-
'client_id' => env('AUTH0_CLIENT_ID'),
71-
'client_secret' => env('AUTH0_CLIENT_SECRET'),
72-
'redirect' => env('AUTH0_REDIRECT_URI'),
73-
'base_url' => env('AUTH0_BASE_URL'),
74-
]
75-
];
78+
], $custom_auth0_tenants);

config/tenants.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'lf' => [
5+
'allowed_3rd_party_providers' => env('LFID_ALLOWED_3RD_PARTY_PROVIDERS', '')
6+
],
7+
];

0 commit comments

Comments
 (0)