[initial import Olivier Schwander **20110427121826 Ignore-this: 84c7d9885b46ece0c6d8b5d9d4d14880 ] addfile ./index.php hunk ./index.php 1 + + + + + Socialized web page + + +

Your name

+loginbox() ?> +logoutbox() ?> +messages() ?> + +visitor()) { + $visitor = $socialize->visitor(); +?> +

You are succesfully authenticated with the following informations:

+ Identity:
+ Email:
+ Nickname:
+ Fullname:
+ + +owner()) { ?> +

This a private information, only for the owner of this site.

+ + +friend()) { ?> +

This a semi-public information, only for the friends of the owner of this site.

+ + + + + addfile ./socialize.php hunk ./socialize.php 1 +owner = $owner; + + if (isset($_GET['openid'])) + $this->login(); + if (isset($_GET['action']) and $_GET['action'] == "login") + $this->login_finish(); + if (isset($_GET['action']) and $_GET['action'] == "logout") + $this->logout(); + + if (isset($_SESSION['visitor'])) + $this->visitor = $_SESSION['visitor']; + } + + public function loginbox() { + if (! $this->visitor) { + print "
\n"; + print "\n"; + print "\n"; + print "
\n"; + } + } + + public function logoutbox() { + if ($this->visitor) + print "logout\n"; + } + + private function logout() { + $_SESSION['visitor'] = false; + $this->success = "Logout successful"; + } + + public function messages() { + if ($this->error) + print "
" . $this->error . "
\n"; + if ($this->success) + print "
" . $this->success . "
\n"; + } + + public function visitor() { + if ($this->visitor) + return $this->visitor; + else + return false; + } + + public function owner() { + if ($this->visitor and $this->visitor['openid'] == $this->owner) + return $this->visitor; + else + return false; + } + + public function friend() { + if ($this->owner() or $this->visitor and $this->visitor['openid'] == $this->owner) + return $this->visitor; + else + return false; + } + + private function login(){ + if (empty($_GET['openid'])) { + $this->error = "Authentication error; not a valid OpenID."; + } + else { + $openid = $_GET['openid']; + $consumer = getConsumer(); + $auth_request = $consumer->begin($openid); + + if (!$auth_request) { + $this->error = "Authentication error; not a valid OpenID."; + } + else { + $sreg_request = Auth_OpenID_SRegRequest::build( + // Required + array('nickname'), + // Optional + array('fullname', 'email')); + + if ($sreg_request) { + $auth_request->addExtension($sreg_request); + } + + $policy_uris = $_GET['policies']; + + $pape_request = new Auth_OpenID_PAPE_Request($policy_uris); + if ($pape_request) { + $auth_request->addExtension($pape_request); + } + + if ($auth_request->shouldSendRedirect()) { + $redirect_url = $auth_request->redirectURL(getTrustRoot(), + getReturnTo()); + + if (Auth_OpenID::isFailure($redirect_url)) { + $this->error = "Could not redirect to server: " . $redirect_url->message; + } else { + $this->header("Location: ".$redirect_url, $now = True); + } + } else { + $form_id = 'openid_message'; + $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(), + false, array('id' => $form_id)); + + if (Auth_OpenID::isFailure($form_html)) { + $this->error = "Could not redirect to server: " . $form_html->message; + } else { + print $form_html; + } + } + } + } + } + + private function login_finish() { + $consumer = getConsumer(); + + $return_to = getReturnTo(); + $response = $consumer->complete($return_to); + + if ($response->status == Auth_OpenID_CANCEL) { + $this->error = 'Verification cancelled.'; + } else if ($response->status == Auth_OpenID_FAILURE) { + $this->error = "OpenID authentication failed: " . $response->message; + } else if ($response->status == Auth_OpenID_SUCCESS) { + $openid = $response->getDisplayIdentifier(); + + $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response); + + $sreg = $sreg_resp->contents(); + + $visitor = array( + 'identity' => escape($openid), + 'email' => escape($sreg['email']), + 'nickname' => escape($sreg['nickname']), + 'fullname' => escape($sreg['fullname']), + ); + + /* $pape_resp = Auth_OpenID_PAPE_Response::fromSuccessResponse($response); */ + + /* if ($pape_resp) { */ + /* if ($pape_resp->auth_policies) { */ + /* $success .= "

The following PAPE policies affected the authentication:

"; */ + /* } else { */ + /* $success .= "

No PAPE policies affected the authentication.

"; */ + /* } */ + + /* if ($pape_resp->auth_age) { */ + /* $age = escape($pape_resp->auth_age); */ + /* $success .= "

The authentication age returned by the " . */ + /* "server is: ".$age."

"; */ + /* } */ + + /* if ($pape_resp->nist_auth_level) { */ + /* $auth_level = escape($pape_resp->nist_auth_level); */ + /* $success .= "

The NIST auth level returned by the " . */ + /* "server is: ".$auth_level."

"; */ + /* } */ + + /* } else { */ + /* $success .= "

No PAPE response was sent by the provider.

"; */ + /* } */ + + $this->success = "Login successful"; + $this->visitor = $visitor; + $_SESSION['visitor'] = $visitor; + } + } +} + +?> addfile ./socpages.php hunk ./socpages.php 1 +error = "Authentication error; not a valid OpenID."; + } + else { + $openid = $_GET['openid_identifier']; + $consumer = getConsumer(); + $auth_request = $consumer->begin($openid); + + if (!$auth_request) { + $this->error = "Authentication error; not a valid OpenID."; + } + else { + $sreg_request = Auth_OpenID_SRegRequest::build( + // Required + array('nickname'), + // Optional + array('fullname', 'email')); + + if ($sreg_request) { + $auth_request->addExtension($sreg_request); + } + + $policy_uris = $_GET['policies']; + + $pape_request = new Auth_OpenID_PAPE_Request($policy_uris); + if ($pape_request) { + $auth_request->addExtension($pape_request); + } + + if ($auth_request->shouldSendRedirect()) { + $redirect_url = $auth_request->redirectURL(getTrustRoot(), + getReturnTo()); + + if (Auth_OpenID::isFailure($redirect_url)) { + $this->error = "Could not redirect to server: " . $redirect_url->message; + } else { + $this->header("Location: ".$redirect_url, $now = True); + } + } else { + $form_id = 'openid_message'; + $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(), + false, array('id' => $form_id)); + + if (Auth_OpenID::isFailure($form_html)) { + $this->error = "Could not redirect to server: " . $form_html->message; + } else { + print $form_html; + } + } + } + } + } + + function auth_finish() { + $consumer = getConsumer(); + + // Complete the authentication process using the server's + // response. + $return_to = getReturnTo(); + $response = $consumer->complete($return_to); + + // Check the response status. + if ($response->status == Auth_OpenID_CANCEL) { + // This means the authentication was cancelled. + $this->error = 'Verification cancelled.'; + } else if ($response->status == Auth_OpenID_FAILURE) { + // Authentication failed; display the error message. + $this->error = "OpenID authentication failed: " . $response->message; + } else if ($response->status == Auth_OpenID_SUCCESS) { + // This means the authentication succeeded; extract the + // identity URL and Simple Registration data (if it was + // returned). + $openid = $response->getDisplayIdentifier(); + $identity = escape($openid); + + $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response); + + $sreg = $sreg_resp->contents(); + + /* if (@$sreg['email']) { */ + /* $success .= " You also returned '".escape($sreg['email']). */ + /* "' as your email."; */ + /* } */ + + if (@$sreg['nickname']) { + $nickname = escape($sreg['nickname']); + } + + /* if (@$sreg['fullname']) { */ + /* $success .= " Your fullname is '".escape($sreg['fullname']). */ + /* "'."; */ + /* } */ + + /* $pape_resp = Auth_OpenID_PAPE_Response::fromSuccessResponse($response); */ + + /* if ($pape_resp) { */ + /* if ($pape_resp->auth_policies) { */ + /* $success .= "

The following PAPE policies affected the authentication:

"; */ + /* } else { */ + /* $success .= "

No PAPE policies affected the authentication.

"; */ + /* } */ + + /* if ($pape_resp->auth_age) { */ + /* $age = escape($pape_resp->auth_age); */ + /* $success .= "

The authentication age returned by the " . */ + /* "server is: ".$age."

"; */ + /* } */ + + /* if ($pape_resp->nist_auth_level) { */ + /* $auth_level = escape($pape_resp->nist_auth_level); */ + /* $success .= "

The NIST auth level returned by the " . */ + /* "server is: ".$auth_level."

"; */ + /* } */ + + /* } else { */ + /* $success .= "

No PAPE response was sent by the provider.

"; */ + /* } */ + $success = sprintf('Hello ' . + '%s.', + $identity, $nickname); + + + $this->success = $success; + $this->visitor_identity = $identity; + $this->visitor_nickname = $nickname; + } + } +} + +R('auth/try')->controller('OpenID')->action('auth_try')->on('GET'); +R('auth/finish')->controller('OpenID')->action('auth_finish')->on('GET'); + +?>