diff --git a/src/Senhaunica.php b/src/Senhaunica.php index ea6c006..06a2dba 100644 --- a/src/Senhaunica.php +++ b/src/Senhaunica.php @@ -12,40 +12,60 @@ */ class Senhaunica { + /** + * Namespace da SESSION + */ + const NS = 'uspdev-senhaunica'; + + /** + * Método que verifica e executa o login + * + * Processo do oauth: + * primeiro processa step 1, que direciona o usuário para a página de login do Oauth; + * o retorno do oauth é direcionado para cá novamente que processa step 2; + * o step 2 redireciona para cá novamente que processa step 3 e + * retorna os dados do usuário autenticado para a rota que chamou inicialmente + * + * @param $clientCredentials + * @return Null|Array + */ public static function login($clientCredentials = []) { - if (empty($clientCredentials)) { - $clientCredentials['identifier'] = getenv('SENHAUNICA_KEY'); - $clientCredentials['secret'] = getenv('SENHAUNICA_SECRET'); - $clientCredentials['callback_id'] = getenv('SENHAUNICA_CALLBACK_ID'); + if (!session_id()) { + session_start(); } + // se não passou as credentials por parâmetro vamos buscar no env + if (empty($clientCredentials)) { + $clientCredentials = [ + 'identifier' => getenv('SENHAUNICA_KEY'), + 'secret' => getenv('SENHAUNICA_SECRET'), + 'callback_id' => getenv('SENHAUNICA_CALLBACK_ID'), + ]; + } $server = new ServerUSP($clientCredentials); // step 3: tudo ok - if (isset($_SESSION['token_credentials'])) { - $tokenCredentials = unserialize($_SESSION['token_credentials']); - $_SESSION['oauth_user'] = $server->getUserDetails($tokenCredentials); - session_write_close(); - return $_SESSION['oauth_user']; + if (isset($_SESSION[self::NS]['token_credentials'])) { + $tokenCredentials = unserialize($_SESSION[self::NS]['token_credentials']); + $_SESSION[self::NS]['oauth_user'] = $server->getUserDetails($tokenCredentials); + return $_SESSION[self::NS]['oauth_user']; } // step 2: recebendo o retorno do oauth if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) { - $temporaryCredentials = unserialize($_SESSION['temporary_credentials']); + $temporaryCredentials = unserialize($_SESSION[self::NS]['temporary_credentials']); $tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']); - unset($_SESSION['temporary_credentials']); - $_SESSION['token_credentials'] = serialize($tokenCredentials); - $_SESSION['oauth_user'] = $server->getUserDetails($tokenCredentials); - session_write_close(); + unset($_SESSION[self::NS]['temporary_credentials']); + $_SESSION[self::NS]['token_credentials'] = serialize($tokenCredentials); + $_SESSION[self::NS]['oauth_user'] = $server->getUserDetails($tokenCredentials); header('Location: ' . $_SERVER['PHP_SELF']); exit; } // step 1: credenciais temporárias e redirecionamento para login USP $temporaryCredentials = $server->getTemporaryCredentials(); - $_SESSION['temporary_credentials'] = serialize($temporaryCredentials); - session_write_close(); + $_SESSION[self::NS]['temporary_credentials'] = serialize($temporaryCredentials); $url = $server->getAuthorizationUrl($temporaryCredentials) . '&callback_id=' . $clientCredentials['callback_id']; header('Location: ' . $url); exit; @@ -53,7 +73,7 @@ public static function login($clientCredentials = []) public static function getUserDetail() { - return isset($_SESSION['oauth_user']) ? $_SESSION['oauth_user'] : null; + return isset($_SESSION[self::NS]['oauth_user']) ? $_SESSION[self::NS]['oauth_user'] : null; } /** @@ -63,14 +83,14 @@ public static function getUserDetail() */ public static function obterVinculo($campo, $valores) { - if (!isset($_SESSION['oauth_user']['vinculo'])) { + if (!isset($_SESSION[self::NS]['oauth_user']['vinculo'])) { return null; } if (!is_array($valores)) { $valores = [$valores]; } foreach ($valores as $valor) { - foreach ($_SESSION['oauth_user']['vinculo'] as $v) { + foreach ($_SESSION[self::NS]['oauth_user']['vinculo'] as $v) { if ($v[$campo] == $valor) { return $v; }