Added simple registration (sreg) support to the OpenID provider

This commit is contained in:
Craig Andrews 2009-10-30 15:33:04 -04:00
parent cdbf7b1da5
commit 982850c9c7

View File

@ -48,68 +48,104 @@ require_once(INSTALLDIR.'/plugins/OpenID/User_openid_trustroot.php');
*/ */
class OpenidserverAction extends Action class OpenidserverAction extends Action
{ {
var $oserver;
function prepare($args)
{
parent::prepare($args);
$this->oserver = oid_server();
return true;
}
function handle($args) function handle($args)
{ {
parent::handle($args); parent::handle($args);
$oserver = oid_server(); $request = $this->oserver->decodeRequest();
$request = $oserver->decodeRequest();
if (in_array($request->mode, array('checkid_immediate', if (in_array($request->mode, array('checkid_immediate',
'checkid_setup'))) { 'checkid_setup'))) {
$cur = common_current_user(); $user = common_current_user();
if(!$cur){ if(!$user){
if($request->immediate){
//cannot prompt the user to login in immediate mode, so answer false
$response = $this->generateDenyResponse($request);
}else{
/* Go log in, and then come back. */ /* Go log in, and then come back. */
common_set_returnto($_SERVER['REQUEST_URI']); common_set_returnto($_SERVER['REQUEST_URI']);
common_redirect(common_local_url('login')); common_redirect(common_local_url('login'));
return; return;
}else if(common_profile_url($cur->nickname) == $request->identity || $request->idSelect()){ }
}else if(common_profile_url($user->nickname) == $request->identity || $request->idSelect()){
$user_openid_trustroot = User_openid_trustroot::pkeyGet( $user_openid_trustroot = User_openid_trustroot::pkeyGet(
array('user_id'=>$cur->id, 'trustroot'=>$request->trustroot)); array('user_id'=>$user->id, 'trustroot'=>$request->trust_root));
if(empty($user_openid_trustroot)){ if(empty($user_openid_trustroot)){
if($request->immediate){ if($request->immediate){
//cannot prompt the user to trust this trust root in immediate mode, so answer false //cannot prompt the user to trust this trust root in immediate mode, so answer false
$response = &$request->answer(false); $response = $this->generateDenyResponse($request);
}else{ }else{
//ask the user to trust this trust root
$_SESSION['openid_trust_root'] = $request->trust_root;
$allowResponse = $request->answer(true, null, common_profile_url($cur->nickname));
$denyResponse = $request->answer(false);
common_ensure_session(); common_ensure_session();
$_SESSION['openid_trust_root'] = $request->trust_root;
$allowResponse = $this->generateAllowResponse($request, $user);
$this->oserver->encodeResponse($allowResponse); //sign the response
$denyResponse = $this->generateDenyResponse($request);
$this->oserver->encodeResponse($denyResponse); //sign the response
$_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl(); $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
$_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl(); $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
//ask the user to trust this trust root
common_redirect(common_local_url('openidtrust')); common_redirect(common_local_url('openidtrust'));
return; return;
} }
}else{ }else{
//user has previously authorized this trust root //user has previously authorized this trust root
$response = &$request->answer(true, null, common_profile_url($cur->nickname)); $response = $this->generateAllowResponse($request, $user);
//$response = $request->answer(true, null, common_profile_url($user->nickname));
} }
} else if ($request->immediate) { } else if ($request->immediate) {
$response = &$request->answer(false); $response = $this->generateDenyResponse($request);
} else { } else {
//invalid //invalid
$this->clientError(sprintf(_('You are not authorized to use the identity %s'),$request->identity),$code=403); $this->clientError(sprintf(_('You are not authorized to use the identity %s'),$request->identity),$code=403);
} }
} else { } else {
$response = &$oserver->handleRequest($request); $response = $this->oserver->handleRequest($request);
} }
if($response){ if($response){
$webresponse = $oserver->encodeResponse($response); $response = $this->oserver->encodeResponse($response);
if ($response->code != AUTH_OPENID_HTTP_OK) {
if ($webresponse->code != AUTH_OPENID_HTTP_OK) { header(sprintf("HTTP/1.1 %d ", $response->code),
header(sprintf("HTTP/1.1 %d ", $webresponse->code), true, $response->code);
true, $webresponse->code);
} }
if($webresponse->headers){ if($response->headers){
foreach ($webresponse->headers as $k => $v) { foreach ($response->headers as $k => $v) {
header("$k: $v"); header("$k: $v");
} }
} }
$this->raw($webresponse->body); $this->raw($response->body);
}else{ }else{
$this->clientError(_('Just an OpenID provider. Nothing to see here, move along...'),$code=500); $this->clientError(_('Just an OpenID provider. Nothing to see here, move along...'),$code=500);
} }
} }
function generateAllowResponse($request, $user){
$response = $request->answer(true, null, common_profile_url($user->nickname));
$profile = $user->getProfile();
$sreg_data = array(
'fullname' => $profile->fullname,
'nickname' => $user->nickname,
'email' => $user->email,
'language' => $user->language,
'timezone' => $user->timezone);
$sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
$sreg_response = Auth_OpenID_SRegResponse::extractResponse(
$sreg_request, $sreg_data);
$sreg_response->toMessage($response->fields);
return $response;
}
function generateDenyResponse($request){
$response = $request->answer(false);
return $response;
}
} }