]> git.mxchange.org Git - friendica.git/blob - src/Security/OAuth.php
64a942bba73eacd14bb93b50bbcebccb2c09b2ef
[friendica.git] / src / Security / OAuth.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Security;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * OAuth Server
31  */
32 class OAuth
33 {
34         const SCOPE_READ   = 'read';
35         const SCOPE_WRITE  = 'write';
36         const SCOPE_FOLLOW = 'follow';
37         const SCOPE_PUSH   = 'push';
38
39         /**
40          * @var bool|int
41          */
42         protected static $current_user_id = 0;
43         /**
44          * @var array
45          */
46         protected static $current_token = [];
47
48         /**
49          * Get current user id, returns 0 if not logged in
50          *
51          * @return int User ID
52          */
53         public static function getCurrentUserID()
54         {
55                 if (empty(self::$current_user_id)) {
56                         $token = self::getCurrentApplicationToken();
57                         if (!empty($token['uid'])) {
58                                 self::$current_user_id = $token['uid'];
59                         } else {
60                                 self::$current_user_id = 0;
61                         }
62                 }
63
64                 return (int)self::$current_user_id;
65         }
66
67         /**
68          * Get current application token
69          *
70          * @return array token
71          */
72         public static function getCurrentApplicationToken()
73         {
74                 if (empty(self::$current_token)) {
75                         self::$current_token = self::getTokenByBearer();
76                 }
77
78                 return self::$current_token;
79         }
80
81         /**
82          * Check if the provided scope does exist
83          *
84          * @param string $scope the requested scope (read, write, follow, push)
85          *
86          * @return bool "true" if the scope is allowed
87          */
88         public static function isAllowedScope(string $scope)
89         {
90                 $token = self::getCurrentApplicationToken();
91
92                 if (empty($token)) {
93                         Logger::notice('Empty application token');
94                         return false;
95                 }
96
97                 if (!isset($token[$scope])) {
98                         Logger::warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]);
99                         return false;
100                 }
101
102                 if (empty($token[$scope])) {
103                         Logger::warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]);
104                         return false;
105                 }
106
107                 return true;
108         }
109
110         /**
111          * Get the user token via the Bearer token
112          *
113          * @return array User Token
114          */
115         private static function getTokenByBearer()
116         {
117                 $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
118
119                 if (substr($authorization, 0, 7) != 'Bearer ') {
120                         return [];
121                 }
122
123                 $condition = ['access_token' => trim(substr($authorization, 7))];
124
125                 $token = DBA::selectFirst('application-view', ['uid', 'id', 'name', 'website', 'created_at', 'read', 'write', 'follow', 'push'], $condition);
126                 if (!DBA::isResult($token)) {
127                         Logger::warning('Token not found', $condition);
128                         return [];
129                 }
130                 Logger::debug('Token found', $token);
131                 return $token;
132         }
133
134         /**
135          * Get the application record via the provided request header fields
136          *
137          * @param string $client_id
138          * @param string $client_secret
139          * @param string $redirect_uri
140          * @return array application record
141          */
142         public static function getApplication(string $client_id, string $client_secret, string $redirect_uri)
143         {
144                 $condition = ['client_id' => $client_id];
145                 if (!empty($client_secret)) {
146                         $condition['client_secret'] = $client_secret;
147                 }
148                 if (!empty($redirect_uri)) {
149                         $condition['redirect_uri'] = $redirect_uri;
150                 }
151
152                 $application = DBA::selectFirst('application', [], $condition);
153                 if (!DBA::isResult($application)) {
154                         Logger::warning('Application not found', $condition);
155                         return [];
156                 }
157                 return $application;
158         }
159
160         /**
161          * Check if an token for the application and user exists
162          *
163          * @param array $application
164          * @param integer $uid
165          * @return boolean
166          */
167         public static function existsTokenForUser(array $application, int $uid)
168         {
169                 return DBA::exists('application-token', ['application-id' => $application['id'], 'uid' => $uid]);
170         }
171
172         /**
173          * Fetch the token for the given application and user
174          *
175          * @param array $application
176          * @param integer $uid
177          * @return array application record
178          */
179         public static function getTokenForUser(array $application, int $uid)
180         {
181                 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);
182         }
183
184         /**
185          * Create and fetch an token for the application and user
186          *
187          * @param array   $application
188          * @param integer $uid
189          * @param string  $scope
190          * @return array application record
191          */
192         public static function createTokenForUser(array $application, int $uid, string $scope)
193         {
194                 $code         = bin2hex(random_bytes(32));
195                 $access_token = bin2hex(random_bytes(32));
196
197                 $fields = [
198                         'application-id' => $application['id'],
199                         'uid'            => $uid,
200                         'code'           => $code,
201                         'access_token'   => $access_token,
202                         'scopes'         => $scope,
203                         'read'           => (stripos($scope, self::SCOPE_READ) !== false),
204                         'write'          => (stripos($scope, self::SCOPE_WRITE) !== false),
205                         'follow'         => (stripos($scope, self::SCOPE_FOLLOW) !== false),
206                         'push'           => (stripos($scope, self::SCOPE_PUSH) !== false),
207                         'created_at'     => DateTimeFormat::utcNow(DateTimeFormat::MYSQL)];
208
209                 foreach ([self::SCOPE_READ, self::SCOPE_WRITE, self::SCOPE_WRITE, self::SCOPE_PUSH] as $scope) {
210                         if ($fields[$scope] && !$application[$scope]) {
211                                 Logger::warning('Requested token scope is not allowed for the application', ['token' => $fields, 'application' => $application]);
212                         }
213                 }
214
215                 if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) {
216                         return [];
217                 }
218
219                 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);
220         }
221 }