Subversion Repositories oidplus

Rev

Rev 715 | Rev 866 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  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.  
  20. # More information about the OAuth2 implementation:
  21. # - https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
  22. # - https://developers.facebook.com/tools/explorer/
  23.  
  24. require_once __DIR__ . '/../../../../includes/oidplus.inc.php';
  25.  
  26. OIDplus::init(true);
  27. set_exception_handler(array('OIDplusGui', 'html_exception_handler'));
  28.  
  29. if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_OIDplusPagePublicLoginFacebook', false)) {
  30.         throw new OIDplusException(_L('This plugin was disabled by the system administrator!'));
  31. }
  32.  
  33. if (!OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_ENABLED', false)) {
  34.         throw new OIDplusException(_L('Facebook OAuth authentication is disabled on this system.'));
  35. }
  36.  
  37. _CheckParamExists($_GET, 'code');
  38. _CheckParamExists($_GET, 'state');
  39. _CheckParamExists($_COOKIE, 'csrf_token_weak');
  40.  
  41. if ($_GET['state'] != $_COOKIE['csrf_token_weak']) {
  42.         die(_L('Wrong CSRF Token'));
  43. }
  44.  
  45. if (!function_exists('curl_init')) {
  46.         die(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
  47. }
  48.  
  49. // Get access token
  50.  
  51. $ch = curl_init();
  52. if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem');
  53. curl_setopt($ch, CURLOPT_URL,"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']
  58. );
  59. curl_setopt($ch, CURLOPT_USERAGENT, 'ViaThinkSoft-OIDplus/2.0');
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  61. $cont = curl_exec($ch);
  62. curl_close($ch);
  63. $data = json_decode($cont,true);
  64. if (isset($data['error'])) {
  65.         echo '<h2>Error at step 2</h2>';
  66.         echo '<p>'.$data['error']['message'].'</p>';
  67.         die();
  68. }
  69. $access_token = $data['access_token'];
  70.  
  71. // Get user infos
  72.  
  73. $ch = curl_init();
  74. if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem');
  75. curl_setopt($ch, CURLOPT_URL,"https://graph.facebook.com/v8.0/me?".
  76.         "fields=id,email,name&".
  77.         "access_token=".urlencode($access_token)
  78. );
  79. curl_setopt($ch, CURLOPT_USERAGENT, 'ViaThinkSoft-OIDplus/2.0');
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  81. $cont = curl_exec($ch);
  82. curl_close($ch);
  83. $data = json_decode($cont,true);
  84. if (isset($data['error'])) {
  85.         throw new OIDplusException(_L('Error receiving the authentication token from %1: %2','Facebook',$data['error']['message']));
  86. }
  87. $personal_name = $data['name'];
  88. $email = !isset($data['email']) ? '' : $data['email'];
  89. if (empty($email)) {
  90.         throw new OIDplusException(_L('Your Facebook account does not have an email address.'));
  91. }
  92.  
  93. // Everything's done! Now login and/or create account
  94.  
  95. $ra = new OIDplusRA($email);
  96. if (!$ra->existing()) {
  97.         $ra->register_ra(null); // create a user account without password
  98.  
  99.         OIDplus::db()->query("update ###ra set ra_name = ?, personal_name = ? where email = ?", array($personal_name, $personal_name, $email));
  100.  
  101.         OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' was created because of successful Facebook OAuth2 login");
  102. }
  103.  
  104. OIDplus::authUtils()->raLoginEx($email, $remember_me=false, 'Facebook-OAuth2');
  105.  
  106. OIDplus::db()->query("UPDATE ###ra set last_login = ".OIDplus::db()->sqlDate()." where email = ?", array($email));
  107.  
  108. // Go back to OIDplus
  109.  
  110. header('Location:'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL));
  111.