friendica/src/Network/FKOAuth1.php

76 lines
2.0 KiB
PHP
Raw Normal View History

2017-12-04 14:09:23 -05:00
<?php
/**
2018-01-30 23:29:05 -05:00
* @file src/Network/FKOAuth1.php
2017-12-04 14:09:23 -05:00
*/
namespace Friendica\Network;
2017-12-04 14:09:23 -05:00
use Friendica\Core\Addon;
2017-12-04 14:09:23 -05:00
use Friendica\Core\PConfig;
use Friendica\Core\System;
use Friendica\Database\DBM;
use Friendica\Network\FKOAuthDataStore;
use Friendica\Util\Temporal;
2017-12-04 14:09:23 -05:00
use dba;
2017-12-04 20:30:10 -05:00
use OAuthServer;
2017-12-04 21:18:48 -05:00
use OAuthSignatureMethod_HMAC_SHA1;
2018-01-30 23:29:05 -05:00
use OAuthSignatureMethod_PLAINTEXT;
2017-12-04 14:09:23 -05:00
/**
* @brief OAuth protocol
*/
class FKOAuth1 extends OAuthServer
{
/**
* @brief Constructor
*/
public function __construct()
{
parent::__construct(new FKOAuthDataStore());
$this->add_signature_method(new OAuthSignatureMethod_PLAINTEXT());
$this->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
}
/**
* @param string $uid user id
* @return void
*/
2017-12-04 21:03:39 -05:00
public function loginUser($uid)
2017-12-04 14:09:23 -05:00
{
logger("FKOAuth1::loginUser $uid");
$a = get_app();
2018-01-10 08:36:02 -05:00
$record = dba::selectFirst('user', [], ['uid' => $uid, 'blocked' => 0, 'account_expired' => 0, 'account_removed' => 0, 'verified' => 1]);
2017-12-04 16:33:10 -05:00
if (!DBM::is_result($record)) {
logger('FKOAuth1::loginUser failure: ' . print_r($_SERVER, true), LOGGER_DEBUG);
header('HTTP/1.0 401 Unauthorized');
die('This api requires login');
2017-12-04 14:09:23 -05:00
}
$_SESSION['uid'] = $record['uid'];
$_SESSION['theme'] = $record['theme'];
$_SESSION['mobile-theme'] = PConfig::get($record['uid'], 'system', 'mobile_theme');
$_SESSION['authenticated'] = 1;
$_SESSION['page_flags'] = $record['page-flags'];
$_SESSION['my_url'] = System::baseUrl() . '/profile/' . $record['nickname'];
$_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
$_SESSION["allow_api"] = true;
$a->user = $record;
if (strlen($a->user['timezone'])) {
date_default_timezone_set($a->user['timezone']);
$a->timezone = $a->user['timezone'];
}
$contact = dba::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => 1]);
if (DBM::is_result($contact)) {
$a->contact = $contact;
$a->cid = $contact['id'];
2017-12-04 14:09:23 -05:00
$_SESSION['cid'] = $a->cid;
}
2018-01-25 23:26:51 -05:00
dba::update('user', ['login_date' => Temporal::utcNow()], ['uid' => $_SESSION['uid']]);
2017-12-04 14:09:23 -05:00
Addon::callHooks('logged_in', $a->user);
2017-12-04 14:09:23 -05:00
}
}