| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentAuth\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentAuth\App\Helpers\Arr; |
| 6 |
use FluentAuth\App\Helpers\Helper; |
| 7 |
use FluentAuth\App\Services\FacebookAuthService; |
| 8 |
use FluentAuth\App\Services\GithubAuthService; |
| 9 |
use FluentAuth\App\Services\GoogleAuthService; |
| 10 |
|
| 11 |
class SocialAuthApiController |
| 12 |
{ |
| 13 |
public static function getSettings(\WP_REST_Request $request) |
| 14 |
{ |
| 15 |
return [ |
| 16 |
'settings' => Helper::getSocialAuthSettings('view'), |
| 17 |
'auth_info' => [ |
| 18 |
'github' => [ |
| 19 |
'is_available' => true, |
| 20 |
'app_redirect' => GithubAuthService::getAppRedirect(), |
| 21 |
'doc_url' => 'https://fluentauth.com/docs/github-auth-connection' |
| 22 |
], |
| 23 |
'google' => [ |
| 24 |
'is_available' => true, |
| 25 |
'app_redirect' => GoogleAuthService::getAppRedirect(), |
| 26 |
'doc_url' => 'https://fluentauth.com/docs/google-auth-connection' |
| 27 |
], |
| 28 |
'facebook' => [ |
| 29 |
'is_available' => true, |
| 30 |
'app_redirect' => FacebookAuthService::getAppRedirect(), |
| 31 |
'doc_url' => 'https://fluentauth.com/docs/facebook-auth-connection' |
| 32 |
] |
| 33 |
] |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
public static function saveSettings(\WP_REST_Request $request) |
| 38 |
{ |
| 39 |
$settings = self::validateSettings($request->get_param('settings')); |
| 40 |
if (is_wp_error($settings)) { |
| 41 |
return $settings; |
| 42 |
} |
| 43 |
|
| 44 |
update_option('__fls_social_auth_settings', $settings, false); |
| 45 |
|
| 46 |
return [ |
| 47 |
'message' => 'Social login settings has been updated' |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
private static function validateSettings($settings) |
| 52 |
{ |
| 53 |
$oldSettings = Helper::getSocialAuthSettings('view'); |
| 54 |
$settings = Arr::only($settings, array_keys($oldSettings)); |
| 55 |
|
| 56 |
if ($settings['enabled'] != 'yes' || ($settings['enable_google'] != 'yes' && $settings['enable_github'] != 'yes' && |
| 57 |
$settings['enable_facebook'] != 'yes')) { |
| 58 |
return [ |
| 59 |
'enabled' => 'no', |
| 60 |
'enable_google' => 'no', |
| 61 |
'google_one_tap' => 'no', |
| 62 |
'google_key_method' => 'wp_config', |
| 63 |
'google_client_id' => '', |
| 64 |
'google_client_secret' => '', |
| 65 |
'enable_github' => 'no', |
| 66 |
'github_key_method' => 'wp_config', |
| 67 |
'github_client_id' => '', |
| 68 |
'github_client_secret' => '', |
| 69 |
'enable_facebook' => 'no', |
| 70 |
'facebook_key_method' => 'wp_config', |
| 71 |
'facebook_client_id' => '', |
| 72 |
'facebook_client_secret' => '', |
| 73 |
'facebook_api_version' => 'v12.0' |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
if ($settings['enable_google'] != 'yes') { |
| 78 |
$settings['google_key_method'] = 'wp_config'; |
| 79 |
$settings['google_client_id'] = ''; |
| 80 |
$settings['google_client_secret'] = ''; |
| 81 |
} else if ($settings['google_key_method'] == 'wp_config') { |
| 82 |
$settings['google_client_id'] = ''; |
| 83 |
$settings['google_client_secret'] = ''; |
| 84 |
if (!defined('FLUENT_AUTH_GOOGLE_CLIENT_ID') || !defined('FLUENT_AUTH_GOOGLE_CLIENT_SECRET')) { |
| 85 |
return new \WP_Error('validation_error', 'Form Validation failed', [ |
| 86 |
'google_key_method' => [ |
| 87 |
'FLUENT_AUTH_GOOGLE_CLIENT_ID' => 'FLUENT_AUTH_GOOGLE_CLIENT_ID constant is required in wp-config.php file', |
| 88 |
'FLUENT_AUTH_GOOGLE_CLIENT_SECRET' => 'FLUENT_AUTH_GOOGLE_CLIENT_SECRET constant is required in wp-config.php file', |
| 89 |
] |
| 90 |
]); |
| 91 |
} |
| 92 |
} else { |
| 93 |
$settings['google_client_id'] = sanitize_textarea_field($settings['google_client_id']); |
| 94 |
$settings['google_client_secret'] = sanitize_textarea_field($settings['google_client_secret']); |
| 95 |
if (empty($settings['google_client_id']) || empty($settings['google_client_secret'])) { |
| 96 |
return new \WP_Error('validation_error', 'Form Validation failed', [ |
| 97 |
'google_client_id' => [ |
| 98 |
'required' => 'Google Client ID is required' |
| 99 |
], |
| 100 |
'google_client_secret' => [ |
| 101 |
'required' => 'Google Client Secret is required' |
| 102 |
] |
| 103 |
]); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ($settings['enable_github'] != 'yes') { |
| 108 |
$settings['github_key_method'] = 'wp_config'; |
| 109 |
$settings['github_client_id'] = ''; |
| 110 |
$settings['github_client_secret'] = ''; |
| 111 |
} else if ($settings['github_key_method'] == 'wp_config') { |
| 112 |
$settings['github_client_id'] = ''; |
| 113 |
$settings['github_client_secret'] = ''; |
| 114 |
if (!defined('FLUENT_AUTH_GITHUB_CLIENT_ID') || !defined('FLUENT_AUTH_GITHUB_CLIENT_SECRET')) { |
| 115 |
return new \WP_Error('validation_error', 'Form Validation failed', [ |
| 116 |
'github_key_method' => [ |
| 117 |
'FLUENT_AUTH_GITHUB_CLIENT_ID' => 'FLUENT_AUTH_GITHUB_CLIENT_ID constant is required in wp-config.php file', |
| 118 |
'FLUENT_AUTH_GITHUB_CLIENT_SECRET' => 'FLUENT_AUTH_GITHUB_CLIENT_SECRET constant is required in wp-config.php file', |
| 119 |
] |
| 120 |
]); |
| 121 |
} |
| 122 |
} else { |
| 123 |
$settings['github_client_id'] = sanitize_textarea_field($settings['github_client_id']); |
| 124 |
$settings['github_client_secret'] = sanitize_textarea_field($settings['github_client_secret']); |
| 125 |
if (empty($settings['github_client_id']) || empty($settings['github_client_secret'])) { |
| 126 |
return new \WP_Error('validation_error', 'Form Validation failed', [ |
| 127 |
'github_client_id' => [ |
| 128 |
'required' => 'Github Client ID is required' |
| 129 |
], |
| 130 |
'github_client_secret' => [ |
| 131 |
'required' => 'Github Client Secret is required' |
| 132 |
] |
| 133 |
]); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if ($settings['enable_facebook'] != 'yes') { |
| 138 |
$settings['facebook_key_method'] = 'wp_config'; |
| 139 |
$settings['facebook_client_id'] = ''; |
| 140 |
$settings['facebook_client_secret'] = ''; |
| 141 |
$settings['facebook_api_version'] = 'v12.0'; |
| 142 |
} else if ($settings['facebook_key_method'] == 'wp_config') { |
| 143 |
$settings['facebook_client_id'] = ''; |
| 144 |
$settings['facebook_client_secret'] = ''; |
| 145 |
if (!defined('FLUENT_AUTH_FACEBOOK_CLIENT_ID') || !defined('FLUENT_AUTH_FACEBOOK_CLIENT_SECRET')) { |
| 146 |
return new \WP_Error('validation_error', 'Form Validation failed', [ |
| 147 |
'facebook_key_method' => [ |
| 148 |
'FLUENT_AUTH_FACEBOOK_CLIENT_ID' => 'FLUENT_AUTH_FACEBOOK_CLIENT_ID constant is required in wp-config.php file', |
| 149 |
'FLUENT_AUTH_FACEBOOK_CLIENT_SECRET' => 'FLUENT_AUTH_FACEBOOK_CLIENT_SECRET constant is required in wp-config.php file', |
| 150 |
] |
| 151 |
]); |
| 152 |
} |
| 153 |
} else { |
| 154 |
$settings['facebook_client_id'] = sanitize_textarea_field($settings['facebook_client_id']); |
| 155 |
$settings['facebook_client_secret'] = sanitize_textarea_field($settings['facebook_client_secret']); |
| 156 |
if (empty($settings['facebook_client_id']) || empty($settings['facebook_client_secret'])) { |
| 157 |
return new \WP_Error('validation_error', 'Form Validation failed', [ |
| 158 |
'facebook_client_id' => [ |
| 159 |
'required' => 'Facebook Client ID is required' |
| 160 |
], |
| 161 |
'facebook_client_secret' => [ |
| 162 |
'required' => 'Facebook Client Secret is required' |
| 163 |
] |
| 164 |
]); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return $settings; |
| 169 |
} |
| 170 |
} |
| 171 |
|