Skip to content

Commit

Permalink
Merge pull request #20 from uspdev/issue_18
Browse files Browse the repository at this point in the history
colocando em um namespace da session
  • Loading branch information
masakik authored May 5, 2023
2 parents 34228a2 + 86f45a1 commit 1c45a53
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions src/Senhaunica.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,68 @@
*/
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;
}

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;
}

/**
Expand All @@ -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;
}
Expand Down

0 comments on commit 1c45a53

Please sign in to comment.