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,92 @@ 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 = '' ;
57+ $ allowed_3rd_party_providers = [];
58+
59+ if (Request::has ("tenant " )) {
60+ $ tenant = trim (Request::get ("tenant " ));
61+ $ allowed_3rd_party_providers = explode (', ' , Config::get ("tenants. " . $ tenant . ".allowed_3rd_party_providers " , "" ));
62+ }
63+
64+ Log::debug ("SocialLoginProviders::buildSupportedProviders " , ["tenant " => $ tenant , "allowed_3rd_party_providers " => $ allowed_3rd_party_providers ]);
65+ foreach (self ::ValidProviders as $ provider ) {
66+ Log::debug ("SocialLoginProviders::buildSupportedProviders " , ["tenant " => $ tenant , "provider " => $ provider ]);
67+ // check if the 3rd party provider has defined some exclusive tenants ...
68+ $ tenants = Config::has ("services. " . $ provider . ".tenants " , "" ) ? explode (', ' , Config::get ("services. " . $ provider . ".tenants " , "" )): [];
69+ // check first if its enabled ...
70+ if (self ::isEnabledProvider ($ provider )) {
71+ Log::debug (sprintf ("SocialLoginProviders::buildSupportedProviders provider %s is enabled " , $ provider ));
72+ // 1. check if we have exclusive tenants defined at provider level
73+ if (count ($ tenants ) > 0 && !in_array ($ tenant , $ tenants )) {
74+ // tenant is not defined on the exclusive collection of the provider
75+ Log::warning
76+ (
77+ sprintf
78+ (
79+ "SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s " ,
80+ $ provider ,
81+ $ tenant
82+ ),
83+ ["tenants " => $ tenants ]
84+ );
85+ continue ;
86+ }
87+ // 2. check if the tenant has that provider enabled
88+ if (count ($ tenants ) == 0 && !empty ($ tenant ) && !in_array ($ provider , $ allowed_3rd_party_providers )) {
89+ Log::warning
90+ (
91+ sprintf
92+ (
93+ "SocialLoginProviders::buildSupportedProviders provider %s is not enabled for tenant %s " ,
94+ $ provider ,
95+ $ tenant
96+ ),
97+ ["allowed_3rd_party_providers " => $ allowed_3rd_party_providers ]
98+ );
99+ continue ;
100+ }
101+
102+ Log::debug (sprintf ("SocialLoginProviders::buildSupportedProviders provider %s is added " , $ provider ));
64103 $ res [$ provider ] = ucfirst ($ provider );
104+ }
65105 }
66106 return $ res ;
67107 }
108+
109+ /**
110+ * @param string $provider
111+ * @return bool
112+ */
113+ public static function isEnabledProvider (string $ provider ): bool
114+ {
115+ return !empty (Config::get ("services. " . $ provider . ".client_id " , null )) &&
116+ !empty (Config::get ("services. " . $ provider . ".client_secret " , null ));
117+ }
68118}
0 commit comments