2009-10-30 05:01:25 +09:00
|
|
|
<?php
|
2020-08-04 20:12:17 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2009-10-30 05:01:25 +09:00
|
|
|
/**
|
|
|
|
* Settings for OpenID
|
|
|
|
*
|
|
|
|
* @category Settings
|
2020-08-04 20:12:17 +09:00
|
|
|
* @package GNUsocial
|
|
|
|
* @author Craig Andrews <candrews@integralblue.com>
|
2009-10-30 05:01:25 +09:00
|
|
|
* @copyright 2008-2009 StatusNet, Inc.
|
2010-05-28 07:26:47 +09:00
|
|
|
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
|
2020-08-04 20:12:17 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2009-10-30 05:01:25 +09:00
|
|
|
*/
|
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
defined('GNUSOCIAL') || die();
|
2009-10-30 05:01:25 +09:00
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
require_once INSTALLDIR . '/plugins/OpenID/openid.php';
|
2009-10-30 05:01:25 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings for OpenID
|
|
|
|
*
|
|
|
|
* Lets users add, edit and delete OpenIDs from their account
|
|
|
|
*
|
2020-08-04 20:12:17 +09:00
|
|
|
* @category Settings
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Craig Andrews <candrews@integralblue.com>
|
2010-05-28 07:26:47 +09:00
|
|
|
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
|
2020-08-04 20:12:17 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2009-10-30 05:01:25 +09:00
|
|
|
*/
|
|
|
|
class OpenidserverAction extends Action
|
|
|
|
{
|
2020-08-04 20:12:17 +09:00
|
|
|
public $oserver;
|
2009-10-31 04:33:04 +09:00
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
public function prepare(array $args = [])
|
2009-10-31 04:33:04 +09:00
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
$this->oserver = oid_server();
|
|
|
|
return true;
|
|
|
|
}
|
2009-10-30 05:01:25 +09:00
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
public function handle()
|
2009-10-30 05:01:25 +09:00
|
|
|
{
|
2016-06-01 11:05:11 +09:00
|
|
|
parent::handle();
|
2009-10-31 04:33:04 +09:00
|
|
|
$request = $this->oserver->decodeRequest();
|
2009-10-30 05:01:25 +09:00
|
|
|
if (in_array($request->mode, array('checkid_immediate',
|
|
|
|
'checkid_setup'))) {
|
2016-02-23 22:15:08 +09:00
|
|
|
if (!$this->scoped instanceof Profile) {
|
2020-08-04 20:12:17 +09:00
|
|
|
if ($request->immediate) {
|
2009-10-31 04:33:04 +09:00
|
|
|
//cannot prompt the user to login in immediate mode, so answer false
|
|
|
|
$response = $this->generateDenyResponse($request);
|
2020-08-04 20:12:17 +09:00
|
|
|
} else {
|
2010-06-17 04:30:37 +09:00
|
|
|
// Go log in, and then come back.
|
|
|
|
//
|
|
|
|
// Note: 303 redirect rather than 307 to avoid
|
|
|
|
// prompting user for form resubmission if we
|
|
|
|
// were POSTed here.
|
2009-10-31 04:33:04 +09:00
|
|
|
common_set_returnto($_SERVER['REQUEST_URI']);
|
2010-04-09 06:20:24 +09:00
|
|
|
common_redirect(common_local_url('login'), 303);
|
2009-10-31 04:33:04 +09:00
|
|
|
}
|
2020-08-04 20:12:17 +09:00
|
|
|
} elseif (
|
|
|
|
in_array($request->identity, $this->scoped->getAliases())
|
|
|
|
|| $request->idSelect()
|
|
|
|
) {
|
|
|
|
$user_openid_trustroot = User_openid_trustroot::pkeyGet([
|
|
|
|
'user_id' => $this->scoped->getID(),
|
|
|
|
'trustroot' => $request->trust_root,
|
|
|
|
]);
|
|
|
|
if (empty($user_openid_trustroot)) {
|
|
|
|
if ($request->immediate) {
|
2009-10-31 02:21:11 +09:00
|
|
|
//cannot prompt the user to trust this trust root in immediate mode, so answer false
|
2009-10-31 04:33:04 +09:00
|
|
|
$response = $this->generateDenyResponse($request);
|
2020-08-04 20:12:17 +09:00
|
|
|
} else {
|
2009-10-31 02:21:11 +09:00
|
|
|
common_ensure_session();
|
2009-10-31 04:33:04 +09:00
|
|
|
$_SESSION['openid_trust_root'] = $request->trust_root;
|
2016-02-23 22:15:08 +09:00
|
|
|
$allowResponse = $this->generateAllowResponse($request, $this->scoped);
|
2009-10-31 04:33:04 +09:00
|
|
|
$this->oserver->encodeResponse($allowResponse); //sign the response
|
|
|
|
$denyResponse = $this->generateDenyResponse($request);
|
|
|
|
$this->oserver->encodeResponse($denyResponse); //sign the response
|
2009-10-31 02:21:11 +09:00
|
|
|
$_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
|
|
|
|
$_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
|
2010-06-17 04:30:37 +09:00
|
|
|
|
|
|
|
// Ask the user to trust this trust root...
|
|
|
|
//
|
|
|
|
// Note: 303 redirect rather than 307 to avoid
|
|
|
|
// prompting user for form resubmission if we
|
|
|
|
// were POSTed here.
|
2010-04-09 06:20:24 +09:00
|
|
|
common_redirect(common_local_url('openidtrust'), 303);
|
2009-10-31 02:21:11 +09:00
|
|
|
}
|
2016-02-23 22:15:08 +09:00
|
|
|
} else {
|
2009-10-31 02:21:11 +09:00
|
|
|
//user has previously authorized this trust root
|
2016-02-23 22:15:08 +09:00
|
|
|
$response = $this->generateAllowResponse($request, $this->scoped);
|
2009-10-31 02:21:11 +09:00
|
|
|
}
|
2016-02-23 22:15:08 +09:00
|
|
|
} elseif ($request->immediate) {
|
2009-10-31 04:33:04 +09:00
|
|
|
$response = $this->generateDenyResponse($request);
|
2009-10-30 05:01:25 +09:00
|
|
|
} else {
|
|
|
|
//invalid
|
2020-08-04 20:12:17 +09:00
|
|
|
$this->clientError(sprintf(
|
|
|
|
// TRANS: OpenID plugin client error given trying to add an unauthorised OpenID to a user (403).
|
|
|
|
// TRANS: %s is a request identity.
|
|
|
|
_m('You are not authorized to use the identity %s.'),
|
|
|
|
$request->identity
|
|
|
|
), 403);
|
2009-10-30 05:01:25 +09:00
|
|
|
}
|
|
|
|
} else {
|
2009-10-31 04:33:04 +09:00
|
|
|
$response = $this->oserver->handleRequest($request);
|
2009-10-30 05:01:25 +09:00
|
|
|
}
|
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
if ($response) {
|
2009-10-31 04:33:04 +09:00
|
|
|
$response = $this->oserver->encodeResponse($response);
|
|
|
|
if ($response->code != AUTH_OPENID_HTTP_OK) {
|
2020-08-04 20:12:17 +09:00
|
|
|
http_response_code($response->code);
|
2009-10-30 05:01:25 +09:00
|
|
|
}
|
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
if ($response->headers) {
|
2009-10-31 04:33:04 +09:00
|
|
|
foreach ($response->headers as $k => $v) {
|
2009-10-30 05:01:25 +09:00
|
|
|
header("$k: $v");
|
|
|
|
}
|
|
|
|
}
|
2009-10-31 04:33:04 +09:00
|
|
|
$this->raw($response->body);
|
2020-08-04 20:12:17 +09:00
|
|
|
} else {
|
|
|
|
$this->clientError(
|
|
|
|
// TRANS: OpenID plugin client error given when not getting a response for a given OpenID provider (500).
|
|
|
|
_m('Just an OpenID provider. Nothing to see here, move along...'),
|
|
|
|
500
|
|
|
|
);
|
2009-10-30 05:01:25 +09:00
|
|
|
}
|
|
|
|
}
|
2009-10-31 04:33:04 +09:00
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
public function generateAllowResponse($request, Profile $profile)
|
|
|
|
{
|
2016-02-23 22:15:08 +09:00
|
|
|
$response = $request->answer(true, null, $profile->getUrl());
|
|
|
|
$user = $profile->getUser();
|
2011-04-28 03:37:44 +09:00
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
$sreg_data = [
|
2016-02-23 22:15:08 +09:00
|
|
|
'fullname' => $profile->getFullname(),
|
|
|
|
'nickname' => $profile->getNickname(),
|
|
|
|
'email' => $user->email, // FIXME: Should we make the email optional?
|
2009-10-31 04:33:04 +09:00
|
|
|
'language' => $user->language,
|
2020-08-04 20:12:17 +09:00
|
|
|
'timezone' => $user->timezone,
|
|
|
|
];
|
2009-10-31 04:33:04 +09:00
|
|
|
$sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
|
|
|
|
$sreg_response = Auth_OpenID_SRegResponse::extractResponse(
|
2020-08-04 20:12:17 +09:00
|
|
|
$sreg_request,
|
|
|
|
$sreg_data
|
|
|
|
);
|
2009-10-31 04:33:04 +09:00
|
|
|
$sreg_response->toMessage($response->fields);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2020-08-04 20:12:17 +09:00
|
|
|
public function generateDenyResponse($request)
|
|
|
|
{
|
2009-10-31 04:33:04 +09:00
|
|
|
$response = $request->answer(false);
|
|
|
|
return $response;
|
|
|
|
}
|
2009-10-30 05:01:25 +09:00
|
|
|
}
|