Rev 1050 | Rev 1199 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1149 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | use ViaThinkSoft\OIDplus\OIDplus; |
21 | use ViaThinkSoft\OIDplus\OIDplusGui; |
||
22 | use ViaThinkSoft\OIDplus\OIDplusException; |
||
23 | use ViaThinkSoft\OIDplus\OIDplusRA; |
||
24 | |||
635 | daniel-mar | 25 | # More information about the OAuth2 implementation: |
26 | # - https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow |
||
27 | # - https://developers.facebook.com/tools/explorer/ |
||
28 | |||
29 | require_once __DIR__ . '/../../../../includes/oidplus.inc.php'; |
||
30 | |||
31 | OIDplus::init(true); |
||
1050 | daniel-mar | 32 | set_exception_handler(array(OIDplusGui::class, 'html_exception_handler')); |
635 | daniel-mar | 33 | |
1050 | daniel-mar | 34 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPagePublicLoginFacebook', false)) { |
635 | daniel-mar | 35 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
36 | } |
||
37 | |||
38 | if (!OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_ENABLED', false)) { |
||
39 | throw new OIDplusException(_L('Facebook OAuth authentication is disabled on this system.')); |
||
40 | } |
||
41 | |||
42 | _CheckParamExists($_GET, 'code'); |
||
43 | _CheckParamExists($_GET, 'state'); |
||
44 | _CheckParamExists($_COOKIE, 'csrf_token_weak'); |
||
45 | |||
46 | if ($_GET['state'] != $_COOKIE['csrf_token_weak']) { |
||
866 | daniel-mar | 47 | die(_L('Missing or wrong CSRF Token')); |
635 | daniel-mar | 48 | } |
49 | |||
50 | // Get access token |
||
51 | |||
1149 | daniel-mar | 52 | $cont = url_post_contents( |
53 | "https://graph.facebook.com/v8.0/oauth/access_token?". |
||
54 | "client_id=".urlencode(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_ID'))."&". |
||
55 | "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE_CANONICAL).'oauth.php')."&". |
||
56 | "client_secret=".urlencode(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_SECRET'))."&". |
||
57 | "code=".$_GET['code'] |
||
635 | daniel-mar | 58 | ); |
1149 | daniel-mar | 59 | |
60 | if ($cont === false) { |
||
61 | throw new OIDplusException(_L('Communication with %1 server failed', 'Facebook')); |
||
62 | } |
||
63 | |||
635 | daniel-mar | 64 | $data = json_decode($cont,true); |
65 | if (isset($data['error'])) { |
||
66 | echo '<h2>Error at step 2</h2>'; |
||
67 | echo '<p>'.$data['error']['message'].'</p>'; |
||
68 | die(); |
||
69 | } |
||
70 | $access_token = $data['access_token']; |
||
71 | |||
72 | // Get user infos |
||
73 | |||
1149 | daniel-mar | 74 | $cont = url_post_contents( |
75 | "https://graph.facebook.com/v8.0/me?". |
||
76 | "fields=id,email,name&". |
||
77 | "access_token=".urlencode($access_token) |
||
635 | daniel-mar | 78 | ); |
1149 | daniel-mar | 79 | |
80 | if ($cont === false) { |
||
81 | throw new OIDplusException(_L('Communication with %1 server failed', 'Facebook')); |
||
82 | } |
||
83 | |||
635 | daniel-mar | 84 | $data = json_decode($cont,true); |
85 | if (isset($data['error'])) { |
||
86 | throw new OIDplusException(_L('Error receiving the authentication token from %1: %2','Facebook',$data['error']['message'])); |
||
87 | } |
||
88 | $personal_name = $data['name']; |
||
89 | $email = !isset($data['email']) ? '' : $data['email']; |
||
90 | if (empty($email)) { |
||
91 | throw new OIDplusException(_L('Your Facebook account does not have an email address.')); |
||
92 | } |
||
93 | |||
94 | // Everything's done! Now login and/or create account |
||
95 | |||
705 | daniel-mar | 96 | $ra = new OIDplusRA($email); |
97 | if (!$ra->existing()) { |
||
98 | $ra->register_ra(null); // create a user account without password |
||
635 | daniel-mar | 99 | |
705 | daniel-mar | 100 | OIDplus::db()->query("update ###ra set ra_name = ?, personal_name = ? where email = ?", array($personal_name, $personal_name, $email)); |
635 | daniel-mar | 101 | |
705 | daniel-mar | 102 | OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' was created because of successful Facebook OAuth2 login"); |
103 | } |
||
635 | daniel-mar | 104 | |
705 | daniel-mar | 105 | OIDplus::authUtils()->raLoginEx($email, $remember_me=false, 'Facebook-OAuth2'); |
635 | daniel-mar | 106 | |
705 | daniel-mar | 107 | OIDplus::db()->query("UPDATE ###ra set last_login = ".OIDplus::db()->sqlDate()." where email = ?", array($email)); |
635 | daniel-mar | 108 | |
705 | daniel-mar | 109 | // Go back to OIDplus |
635 | daniel-mar | 110 | |
1005 | daniel-mar | 111 | OIDplus::invoke_shutdown(); |
112 | |||
801 | daniel-mar | 113 | header('Location:'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)); |