2009-10-29 20:01:25 +00:00
|
|
|
<?php
|
2020-08-04 12:12:17 +01: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-29 20:01:25 +00:00
|
|
|
/**
|
|
|
|
* Settings for OpenID
|
|
|
|
*
|
|
|
|
* @category Settings
|
2020-08-04 12:12:17 +01:00
|
|
|
* @package GNUsocial
|
|
|
|
* @author Craig Andrews <candrews@integralblue.com>
|
2009-10-29 20:01:25 +00:00
|
|
|
* @copyright 2008-2009 StatusNet, Inc.
|
2010-05-27 23:26:47 +01:00
|
|
|
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
|
2020-08-04 12:12:17 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2009-10-29 20:01:25 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
defined('GNUSOCIAL') || die();
|
2009-10-29 20:01:25 +00:00
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
require_once INSTALLDIR . '/plugins/OpenID/openid.php';
|
2009-10-29 20:01:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings for OpenID
|
|
|
|
*
|
|
|
|
* Lets users add, edit and delete OpenIDs from their account
|
|
|
|
*
|
2020-08-04 12:12:17 +01:00
|
|
|
* @category Settings
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Craig Andrews <candrews@integralblue.com>
|
2010-05-27 23:26:47 +01:00
|
|
|
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
|
2020-08-04 12:12:17 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2009-10-29 20:01:25 +00:00
|
|
|
*/
|
|
|
|
class OpenidserverAction extends Action
|
|
|
|
{
|
2020-08-04 12:12:17 +01:00
|
|
|
public $oserver;
|
2009-10-30 19:33:04 +00:00
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
public function prepare(array $args = [])
|
2009-10-30 19:33:04 +00:00
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
$this->oserver = oid_server();
|
|
|
|
return true;
|
|
|
|
}
|
2009-10-29 20:01:25 +00:00
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
public function handle()
|
2009-10-29 20:01:25 +00:00
|
|
|
{
|
2016-06-01 03:05:11 +01:00
|
|
|
parent::handle();
|
2009-10-30 19:33:04 +00:00
|
|
|
$request = $this->oserver->decodeRequest();
|
2009-10-29 20:01:25 +00:00
|
|
|
if (in_array($request->mode, array('checkid_immediate',
|
|
|
|
'checkid_setup'))) {
|
2016-02-23 13:15:08 +00:00
|
|
|
if (!$this->scoped instanceof Profile) {
|
2020-08-04 12:12:17 +01:00
|
|
|
if ($request->immediate) {
|
2009-10-30 19:33:04 +00:00
|
|
|
//cannot prompt the user to login in immediate mode, so answer false
|
|
|
|
$response = $this->generateDenyResponse($request);
|
2020-08-04 12:12:17 +01:00
|
|
|
} else {
|
2010-06-16 20:30:37 +01: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-30 19:33:04 +00:00
|
|
|
common_set_returnto($_SERVER['REQUEST_URI']);
|
2010-04-08 22:20:24 +01:00
|
|
|
common_redirect(common_local_url('login'), 303);
|
2009-10-30 19:33:04 +00:00
|
|
|
}
|
2020-08-04 12:12:17 +01: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-30 17:21:11 +00:00
|
|
|
//cannot prompt the user to trust this trust root in immediate mode, so answer false
|
2009-10-30 19:33:04 +00:00
|
|
|
$response = $this->generateDenyResponse($request);
|
2020-08-04 12:12:17 +01:00
|
|
|
} else {
|
2009-10-30 17:21:11 +00:00
|
|
|
common_ensure_session();
|
2009-10-30 19:33:04 +00:00
|
|
|
$_SESSION['openid_trust_root'] = $request->trust_root;
|
2016-02-23 13:15:08 +00:00
|
|
|
$allowResponse = $this->generateAllowResponse($request, $this->scoped);
|
2009-10-30 19:33:04 +00:00
|
|
|
$this->oserver->encodeResponse($allowResponse); //sign the response
|
|
|
|
$denyResponse = $this->generateDenyResponse($request);
|
|
|
|
$this->oserver->encodeResponse($denyResponse); //sign the response
|
2009-10-30 17:21:11 +00:00
|
|
|
$_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
|
|
|
|
$_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
|
2010-06-16 20:30:37 +01: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-08 22:20:24 +01:00
|
|
|
common_redirect(common_local_url('openidtrust'), 303);
|
2009-10-30 17:21:11 +00:00
|
|
|
}
|
2016-02-23 13:15:08 +00:00
|
|
|
} else {
|
2009-10-30 17:21:11 +00:00
|
|
|
//user has previously authorized this trust root
|
2016-02-23 13:15:08 +00:00
|
|
|
$response = $this->generateAllowResponse($request, $this->scoped);
|
2009-10-30 17:21:11 +00:00
|
|
|
}
|
2016-02-23 13:15:08 +00:00
|
|
|
} elseif ($request->immediate) {
|
2009-10-30 19:33:04 +00:00
|
|
|
$response = $this->generateDenyResponse($request);
|
2009-10-29 20:01:25 +00:00
|
|
|
} else {
|
|
|
|
//invalid
|
2020-08-04 12:12:17 +01: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-29 20:01:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-10-30 19:33:04 +00:00
|
|
|
$response = $this->oserver->handleRequest($request);
|
2009-10-29 20:01:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
if ($response) {
|
2009-10-30 19:33:04 +00:00
|
|
|
$response = $this->oserver->encodeResponse($response);
|
|
|
|
if ($response->code != AUTH_OPENID_HTTP_OK) {
|
2020-08-04 12:12:17 +01:00
|
|
|
http_response_code($response->code);
|
2009-10-29 20:01:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
if ($response->headers) {
|
2009-10-30 19:33:04 +00:00
|
|
|
foreach ($response->headers as $k => $v) {
|
2009-10-29 20:01:25 +00:00
|
|
|
header("$k: $v");
|
|
|
|
}
|
|
|
|
}
|
2009-10-30 19:33:04 +00:00
|
|
|
$this->raw($response->body);
|
2020-08-04 12:12:17 +01: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-29 20:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-30 19:33:04 +00:00
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
public function generateAllowResponse($request, Profile $profile)
|
|
|
|
{
|
2016-02-23 13:15:08 +00:00
|
|
|
$response = $request->answer(true, null, $profile->getUrl());
|
|
|
|
$user = $profile->getUser();
|
2011-04-27 19:37:44 +01:00
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
$sreg_data = [
|
2016-02-23 13:15:08 +00:00
|
|
|
'fullname' => $profile->getFullname(),
|
|
|
|
'nickname' => $profile->getNickname(),
|
|
|
|
'email' => $user->email, // FIXME: Should we make the email optional?
|
2009-10-30 19:33:04 +00:00
|
|
|
'language' => $user->language,
|
2020-08-04 12:12:17 +01:00
|
|
|
'timezone' => $user->timezone,
|
|
|
|
];
|
2009-10-30 19:33:04 +00:00
|
|
|
$sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
|
|
|
|
$sreg_response = Auth_OpenID_SRegResponse::extractResponse(
|
2020-08-04 12:12:17 +01:00
|
|
|
$sreg_request,
|
|
|
|
$sreg_data
|
|
|
|
);
|
2009-10-30 19:33:04 +00:00
|
|
|
$sreg_response->toMessage($response->fields);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:12:17 +01:00
|
|
|
public function generateDenyResponse($request)
|
|
|
|
{
|
2009-10-30 19:33:04 +00:00
|
|
|
$response = $request->answer(false);
|
|
|
|
return $response;
|
|
|
|
}
|
2009-10-29 20:01:25 +00:00
|
|
|
}
|