Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $sso = Mage::app()->getLayout()->createBlock('namespace/ssohelper');
- $secret = "secret_phrase";
- $sso->setSecret($secret);
- // load payload
- $payload = $_GET['sso'];
- $signature = $_GET['sig'];
- // validate payload
- if(!($sso->validatePayload($payload,$signature))) {
- // invalid, Deny
- header("HTTPS/1.1 403 Forbidden");
- echo("Bad SSO request");
- die();
- }
- $nonce = $sso->getNonce($payload);
- $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
- if($loggedIn) {
- $sess = Mage::getSingleton('customer/session');
- $username = $sess->getCustomer()->getUsername();
- $email = $sess->getCustomer()->getEmail();
- $external_id = $sess->getCustomer()->getId();
- $extraParameters = array(
- 'username' => $username,
- 'name' => $username,
- 'avatar_url' => $avatar
- );
- }
- // build query string and redirect back to forum
- $query = $sso->getSignInString($nonce, $external_id, $email, $extraParameters);
- header('Location: https://forum.website.come/session/sso_login?' . $query);
- exit(0);
- <?xml version="1.0"?>
- <config>
- <modules>
- <Namespace_SsoHelper>
- <version>0.1.1</version>
- </Namespace_SsoHelper>
- </modules>
- <global>
- <blocks>
- <ssohelper>
- <class>Namespace_SsoHelper_Block</class>
- </ssohelper>
- </blocks>
- </global>
- </config>
- <?php
- use NamespaceSsoHelperExceptionPayloadException;
- class Namespace_SsoHelper_Block_SsoHelper extends Mage_Core_Block_Template
- {
- public function __construct(){
- parent::__construct();
- # getting default settings
- }
- private $secret;
- public function setSecret($secret)
- {
- $this->secret = $secret;
- return $this;
- }
- /**
- * @param $payload
- * @param $signature
- * @return mixed
- */
- public function validatePayload($payload, $signature)
- {
- $payload = urldecode($payload);
- return $this->signPayload($payload) === $signature;
- }
- /**
- * @param $payload
- * @return mixed
- * @throws PayloadException
- */
- public function getNonce($payload)
- {
- $payload = urldecode($payload);
- $query = array();
- parse_str(base64_decode($payload), $query);
- if (!array_key_exists('nonce', $query)) {
- throw new PayloadException('Nonce not found in payload');
- }
- return $query['nonce'];
- }
- /**
- * @param $payload
- * @return mixed
- * @throws PayloadException
- */
- public function getReturnSSOURL($payload)
- {
- $payload = urldecode($payload);
- $query = array();
- parse_str(base64_decode($payload), $query);
- if (!array_key_exists('return_sso_url', $query)) {
- throw new PayloadException('Return SSO URL not found in payload');
- }
- return $query['return_sso_url'];
- }
- /**
- * @param $nonce
- * @param $external_id
- * @param $email
- * @param array $extraParameters
- * @return string
- */
- public function getSignInString($nonce, $external_id, $email, $extraParameters = [])
- {
- $parameters = array(
- 'nonce' => $nonce,
- 'external_id' => $external_id,
- 'email' => $email,
- ) + $extraParameters;
- $payload = base64_encode(http_build_query($parameters));
- $data = array(
- 'sso' => $payload,
- 'sig' => $this->signPayload($payload),
- );
- return http_build_query($data);
- }
- /**
- * @param $payload
- * @return string
- */
- protected function signPayload($payload)
- {
- return hash_hmac('sha256', $payload, $this->secret);
- }
- }
- $sso = Mage::app()->getLayout()->createBlock('namespace/ssohelper');
- $sso = Mage::app()->getLayout()->createBlock('ssohelper/ssohelper');
Add Comment
Please, Sign In to add comment