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,41 +27,116 @@ final class SocialLoginProviders
2527 const LinkedIn = "linkedin " ;
2628 const Google = "google " ;
2729 const OKTA = 'okta ' ;
30+ const LFID = 'lfid ' ;
2831
2932 const ValidProviders = [
3033 self ::Facebook,
3134 self ::LinkedIn,
3235 self ::Apple,
3336 //self::Google
3437 self ::OKTA ,
38+ self ::LFID ,
3539 ];
3640
3741 /**
3842 * @param string $provider
3943 * @return bool
4044 */
41- public static function isSupportedProvider (string $ provider ):bool {
45+ public static function isSupportedProvider (string $ provider ): bool
46+ {
4247 return in_array ($ provider , self ::ValidProviders);
4348 }
4449
4550 /**
46- * @param string $provider
47- * @return bool
48- */
49- public static function isEnabledProvider (string $ provider ):bool {
50- return !empty (Config::get ("services. " .$ provider .".client_id " , null )) &&
51- !empty (Config::get ("services. " .$ provider .".client_secret " , null ));
52- }
53-
54- /**
55- * @return string[]
51+ * @param string $provided_tenant
52+ * @return array
5653 */
57- public static function buildSupportedProviders ():array {
54+ public static function buildSupportedProviders (string $ provided_tenant = '' ): array
55+ {
56+ Log::debug ("SocialLoginProviders::buildSupportedProviders " , ["provided_tenant " => $ provided_tenant ]);
5857 $ res = [];
59- foreach (self ::ValidProviders as $ provider ){
60- if (self ::isEnabledProvider ($ provider ))
58+ $ tenant = trim (Request::get ('tenant ' , $ provided_tenant ));
59+ $ allowed_3rd_party_providers = self ::toList (
60+ Config::get ("tenants. $ tenant.allowed_3rd_party_providers " , '' )
61+ );
62+
63+ Log::debug ("SocialLoginProviders::buildSupportedProviders " , ["tenant " => $ tenant , "allowed_3rd_party_providers " => $ allowed_3rd_party_providers ]);
64+ foreach (self ::ValidProviders as $ provider ) {
65+ Log::debug ("SocialLoginProviders::buildSupportedProviders " , ["tenant " => $ tenant , "provider " => $ provider ]);
66+
67+ if (!self ::isEnabledProvider ($ provider )) {
68+ Log::warning ("SocialLoginProviders::buildSupportedProviders provider is not enabled. " , ["tenant " => $ tenant , "provider " => $ provider ]);
69+ continue ;
70+ }
71+
72+ // check if the 3rd party provider has defined some exclusive tenants ...
73+ $ tenants = self ::toList (
74+ Config::get ("services. $ provider.tenants " , '' )
75+ );
76+
77+ // If no tenant param was provided, any enabled provider is allowed.
78+ if ($ tenant === '' && count ($ tenants ) == 0 ) {
6179 $ res [$ provider ] = ucfirst ($ provider );
80+ continue ;
81+ }
82+ Log::debug (sprintf ("SocialLoginProviders::buildSupportedProviders provider %s is enabled " , $ provider ));
83+ // 1. check if we have exclusive tenants defined at provider level
84+ if (count ($ tenants ) > 0 && !in_array ($ tenant , $ tenants )) {
85+ // tenant is not defined on the exclusive collection of the provider
86+ Log::warning
87+ (
88+ sprintf
89+ (
90+ "SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s " ,
91+ $ provider ,
92+ $ tenant
93+ ),
94+ ["tenants " => $ tenants ]
95+ );
96+ continue ;
97+ }
98+ // 2. check if the tenant has that provider enabled
99+ if (!count ($ tenants ) && !in_array ($ provider , $ allowed_3rd_party_providers )) {
100+ Log::warning
101+ (
102+ sprintf
103+ (
104+ "SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s " ,
105+ $ provider ,
106+ $ tenant
107+ ),
108+ ["allowed_3rd_party_providers " => $ allowed_3rd_party_providers ]
109+ );
110+ continue ;
111+ }
112+
113+ Log::debug (sprintf ("SocialLoginProviders::buildSupportedProviders provider %s is added " , $ provider ));
114+ $ res [$ provider ] = ucfirst ($ provider );
62115 }
116+
63117 return $ res ;
64118 }
119+
120+ private static function toList ($ value ): array
121+ {
122+ if (is_array ($ value )) {
123+ return array_values (array_filter (array_map ('trim ' , $ value ), static fn ($ v ) => $ v !== '' ));
124+ }
125+ if (is_string ($ value )) {
126+ if ($ value === '' ) return [];
127+ return array_values (array_filter (array_map ('trim ' , explode (', ' , $ value )), static fn ($ v ) => $ v !== '' ));
128+ }
129+ return [];
130+ }
131+
132+ /**
133+ * @param string $provider
134+ * @return bool
135+ */
136+ public static function isEnabledProvider (string $ provider ): bool
137+ {
138+ return !empty (Config::get ("services. " . $ provider . ".client_id " , null )) &&
139+ !empty (Config::get ("services. " . $ provider . ".client_secret " , null ));
140+ }
141+
65142}
0 commit comments