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 |
||
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 | |||
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.google.com/identity/protocols/oauth2/openid-connect |
||
27 | |||
28 | require_once __DIR__ . '/../../../../includes/oidplus.inc.php'; |
||
29 | |||
30 | OIDplus::init(true); |
||
1050 | daniel-mar | 31 | set_exception_handler(array(OIDplusGui::class, 'html_exception_handler')); |
635 | daniel-mar | 32 | |
1050 | daniel-mar | 33 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPagePublicLoginGoogle', false)) { |
635 | daniel-mar | 34 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
35 | } |
||
36 | |||
37 | if (!OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) { |
||
38 | throw new OIDplusException(_L('Google OAuth authentication is disabled on this system.')); |
||
39 | } |
||
40 | |||
41 | _CheckParamExists($_GET, 'code'); |
||
42 | _CheckParamExists($_GET, 'state'); |
||
43 | _CheckParamExists($_COOKIE, 'csrf_token_weak'); |
||
44 | |||
45 | if ($_GET['state'] != $_COOKIE['csrf_token_weak']) { |
||
866 | daniel-mar | 46 | die(_L('Missing or wrong CSRF Token')); |
635 | daniel-mar | 47 | } |
48 | |||
1149 | daniel-mar | 49 | $cont = url_post_contents( |
50 | "https://oauth2.googleapis.com/token", |
||
51 | array( |
||
52 | "grant_type" => "authorization_code", |
||
53 | "code" => $_GET['code'], |
||
54 | "redirect_uri" => OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE_CANONICAL).'oauth.php', |
||
55 | "client_id" => OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'), |
||
56 | "client_secret" => OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_SECRET') |
||
57 | ) |
||
58 | ); |
||
59 | |||
60 | if ($cont === false) { |
||
61 | throw new OIDplusException(_L('Communication with %1 server failed', 'Google')); |
||
635 | daniel-mar | 62 | } |
63 | |||
64 | // Get ID token and Access token |
||
65 | $data = json_decode($cont,true); |
||
66 | if (isset($data['error'])) { |
||
67 | throw new OIDplusException(_L('Error receiving the authentication token from %1: %2','Google',$data['error'].' '.$data['error_description'])); |
||
68 | } |
||
69 | $id_token = $data['id_token']; |
||
70 | $access_token = $data['access_token']; |
||
71 | |||
72 | try { |
||
73 | |||
74 | // Decode JWT "id_token" |
||
75 | // see https://medium.com/@darutk/understanding-id-token-5f83f50fa02e |
||
76 | // Note: We do not need to verify the signature because the token comes directly from Google, |
||
77 | // but we do it anyway. Just to be sure! |
||
1149 | daniel-mar | 78 | $certs = url_get_contents('https://www.googleapis.com/oauth2/v1/certs'); |
79 | if ($certs === false) { |
||
80 | throw new OIDplusException(_L('Communication with %1 server failed', 'Google')); |
||
81 | } |
||
82 | $verification_certs = json_decode($certs, true); |
||
635 | daniel-mar | 83 | \Firebase\JWT\JWT::$leeway = 60; // leeway in seconds |
84 | $data = (array) \Firebase\JWT\JWT::decode($id_token, $verification_certs, array('ES256', 'ES384', 'RS256', 'RS384', 'RS512')); |
||
85 | if (!isset($data['iss']) || ($data['iss'] !== 'https://accounts.google.com')) { |
||
86 | throw new OIDplusException(_L('JWT token could not be decoded')); |
||
87 | } |
||
88 | |||
89 | // Check if the email was verified |
||
90 | $email = $data['email']; |
||
91 | if ($data['email_verified'] != 'true') { |
||
92 | throw new OIDplusException(_L('The email address %1 was not verified. Please verify it first!',$email)); |
||
93 | } |
||
94 | |||
95 | // Everything's done! Now login and/or create account |
||
96 | if (!empty($email)) { |
||
97 | $ra = new OIDplusRA($email); |
||
98 | if (!$ra->existing()) { |
||
99 | $ra->register_ra(null); // create a user account without password |
||
100 | |||
101 | // Query user infos |
||
1149 | daniel-mar | 102 | $result = url_post_contents( |
103 | 'https://www.googleapis.com/oauth2/v3/userinfo', |
||
104 | array( |
||
105 | "client_id" => OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'), |
||
106 | "client_secret" => OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_SECRET'), |
||
107 | "token_type_hint" => "access_token", |
||
108 | "token" => $access_token |
||
109 | ), |
||
110 | array( |
||
111 | "Authorization" => "Bearer $access_token" |
||
112 | ) |
||
113 | ); |
||
114 | |||
115 | if ($result === false) { |
||
116 | throw new OIDplusException(_L('Communication with %1 server failed', 'Google')); |
||
117 | } |
||
118 | |||
635 | daniel-mar | 119 | $data = json_decode($result,true); |
120 | $personal_name = $data['name']; // = given_name + " " + family_name |
||
121 | |||
122 | OIDplus::db()->query("update ###ra set ra_name = ?, personal_name = ? where email = ?", array($personal_name, $personal_name, $email)); |
||
123 | |||
124 | OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' was created because of successful Google OAuth2 login"); |
||
125 | } |
||
126 | |||
127 | OIDplus::authUtils()->raLoginEx($email, $remember_me=false, 'Google-OAuth2'); |
||
128 | |||
129 | OIDplus::db()->query("UPDATE ###ra set last_login = ".OIDplus::db()->sqlDate()." where email = ?", array($email)); |
||
130 | |||
1005 | daniel-mar | 131 | OIDplus::invoke_shutdown(); |
132 | |||
635 | daniel-mar | 133 | // Go back to OIDplus |
134 | |||
801 | daniel-mar | 135 | header('Location:'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)); |
635 | daniel-mar | 136 | } |
137 | |||
138 | } finally { |
||
139 | |||
140 | // We now have the data of the person that wanted to log in |
||
141 | // So we can log off again |
||
1149 | daniel-mar | 142 | $cont = url_post_contents( |
143 | "https://oauth2.googleapis.com/revoke", |
||
144 | array( |
||
145 | "client_id" => OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'), |
||
146 | "client_secret" => OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_SECRET'), |
||
147 | "token_type_hint" => "access_token", |
||
148 | "token" => urlencode($access_token) |
||
149 | ) |
||
635 | daniel-mar | 150 | ); |
151 | |||
1149 | daniel-mar | 152 | if ($cont === false) { |
153 | // throw new OIDplusException(_L('Communication with %1 server failed', 'Google')); |
||
154 | } |
||
155 | |||
635 | daniel-mar | 156 | } |