]> git.mxchange.org Git - friendica.git/blob - src/Security/OAuth.php
API: New classes for OAuth and basic auth
[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          * Check if the provided scope does exist
50          *
51          * @param string $scope the requested scope (read, write, follow, push)
52          *
53          * @return bool "true" if the scope is allowed
54          */
55         public static function isAllowedScope(string $scope)
56         {
57                 $token = self::getCurrentApplicationToken();
58
59                 if (empty($token)) {
60                         Logger::notice('Empty application token');
61                         return false;
62                 }
63
64                 if (!isset($token[$scope])) {
65                         Logger::warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]);
66                         return false;
67                 }
68
69                 if (empty($token[$scope])) {
70                         Logger::warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]);
71                         return false;
72                 }
73
74                 return true;
75         }
76
77         /**
78          * Get current application token
79          *
80          * @return array token
81          */
82         public static function getCurrentApplicationToken()
83         {
84                 if (empty(self::$current_token)) {
85                         self::$current_token = self::getTokenByBearer();
86                 }
87
88                 return self::$current_token;
89         }
90
91         /**
92          * Get current user id, returns 0 if not logged in
93          *
94          * @return int User ID
95          */
96         public static function getCurrentUserID()
97         {
98                 if (empty(self::$current_user_id)) {
99                         $token = self::getCurrentApplicationToken();
100                         if (!empty($token['uid'])) {
101                                 self::$current_user_id = $token['uid'];
102                         } else {
103                                 self::$current_user_id = 0;
104                         }
105                 }
106
107                 return (int)self::$current_user_id;
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                 $bearer = trim(substr($authorization, 7));
124                 $condition = ['access_token' => $bearer];
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 = ['application-id' => $application['id'], 'uid' => $uid, 'code' => $code, 'access_token' => $access_token, 'scopes' => $scope,
198                         'read' => (stripos($scope, self::SCOPE_READ) !== false),
199                         'write' => (stripos($scope, self::SCOPE_WRITE) !== false),
200                         'follow' => (stripos($scope, self::SCOPE_FOLLOW) !== false),
201                         'push' => (stripos($scope, self::SCOPE_PUSH) !== false),
202                          'created_at' => DateTimeFormat::utcNow(DateTimeFormat::MYSQL)];
203
204                 foreach ([self::SCOPE_READ, self::SCOPE_WRITE, self::SCOPE_WRITE, self::SCOPE_PUSH] as $scope) {
205                         if ($fields[$scope] && !$application[$scope]) {
206                                 Logger::warning('Requested token scope is not allowed for the application', ['token' => $fields, 'application' => $application]);
207                         }
208                 }
209
210                 if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) {
211                         return [];
212                 }
213
214                 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);
215         }
216 }