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");
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,115 @@ 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 ))
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+ // If no tenant param was provided, any enabled provider is allowed.
71+ if ($ tenant === '' ) {
6472 $ res [$ provider ] = ucfirst ($ provider );
73+ continue ;
74+ }
75+
76+ // check if the 3rd party provider has defined some exclusive tenants ...
77+ $ tenants = self ::toList (
78+ Config::get ("services. $ provider.tenants " , '' )
79+ );
80+
81+ Log::debug (sprintf ("SocialLoginProviders::buildSupportedProviders provider %s is enabled " , $ provider ));
82+ // 1. check if we have exclusive tenants defined at provider level
83+ if (count ($ tenants ) > 0 && !in_array ($ tenant , $ tenants )) {
84+ // tenant is not defined on the exclusive collection of the provider
85+ Log::warning
86+ (
87+ sprintf
88+ (
89+ "SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s " ,
90+ $ provider ,
91+ $ tenant
92+ ),
93+ ["tenants " => $ tenants ]
94+ );
95+ continue ;
96+ }
97+ // 2. check if the tenant has that provider enabled
98+ if (!count ($ tenants ) && !in_array ($ provider , $ allowed_3rd_party_providers )) {
99+ Log::warning
100+ (
101+ sprintf
102+ (
103+ "SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s " ,
104+ $ provider ,
105+ $ tenant
106+ ),
107+ ["allowed_3rd_party_providers " => $ allowed_3rd_party_providers ]
108+ );
109+ continue ;
110+ }
111+
112+ Log::debug (sprintf ("SocialLoginProviders::buildSupportedProviders provider %s is added " , $ provider ));
113+ $ res [$ provider ] = ucfirst ($ provider );
65114 }
115+
66116 return $ res ;
67117 }
118+
119+ private static function toList ($ value ): array
120+ {
121+ if (is_array ($ value )) {
122+ return array_values (array_filter (array_map ('trim ' , $ value ), static fn ($ v ) => $ v !== '' ));
123+ }
124+ if (is_string ($ value )) {
125+ if ($ value === '' ) return [];
126+ return array_values (array_filter (array_map ('trim ' , explode (', ' , $ value )), static fn ($ v ) => $ v !== '' ));
127+ }
128+ return [];
129+ }
130+
131+ /**
132+ * @param string $provider
133+ * @return bool
134+ */
135+ public static function isEnabledProvider (string $ provider ): bool
136+ {
137+ return !empty (Config::get ("services. " . $ provider . ".client_id " , null )) &&
138+ !empty (Config::get ("services. " . $ provider . ".client_secret " , null ));
139+ }
140+
68141}
0 commit comments