Skip to content

Commit

Permalink
Ajusta plugin com configurações para logar com GOV.br
Browse files Browse the repository at this point in the history
  • Loading branch information
erleibiazzio committed Sep 23, 2022
1 parent 5bc7d51 commit 4f8a65d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 30 deletions.
72 changes: 52 additions & 20 deletions GovBr/GovBrStrategy.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Curl\Curl;
use MapasCulturais\App;

class GovBrStrategy extends OpauthStrategy
{
Expand Down Expand Up @@ -50,6 +51,7 @@ public function request()
public function oauth2callback()
{
$app = App::i();
$self = $this;

if ((array_key_exists('code', $_GET) && !empty($_GET['code'])) && (array_key_exists("state", $_GET) && $_GET['state'] == $_SESSION['govbr-state'])) {

Expand Down Expand Up @@ -78,12 +80,12 @@ public function oauth2callback()

/** @var stdClass $userinfo */
$userinfo = $this->userinfo($results->id_token);
$userinfo->access_token = $results->access_token;

//@TODO O nome deve ser o primeiro nome


$exp_name = explode(" ", $userinfo->name);

$info = [
'name' => $userinfo->name,
'name' => $exp_name[0],
'cpf' => $userinfo->sub,
'email' => $userinfo->email_verified ? $userinfo->email : null,
'phone_number' => $userinfo->phone_number_verified ? $userinfo->phone_number : null,
Expand All @@ -95,31 +97,18 @@ public function oauth2callback()
'token' => $results->id_token,
'expires' => $userinfo->exp
),
'raw' => $info,
'raw' => $userinfo,
'info' => $info
);

$app->hook("entity(Agent).insert:after", function() use ($userinfo, $token){
$this->nomeCompleto = $userinfo->name;

// @TODO definir o avatar
// $curl = new Curl;
// $curl->setHeader('Content-Type', 'application/x-www-form-urlencoded');
// $curl->setHeader('Authorization', "Basic {$token}");

// $curl->post($url, $params);
// $curl->close();
// $response = $curl->response;
});



$this->callback();
} else {
$error = array(
'code' => 'access_token_error',
'message' => 'Failed when attempting to obtain access token',
'raw' => array(
'response' => $response,
'headers' => $headers
)
);
$this->errorCallback($error);
Expand All @@ -143,4 +132,47 @@ private function userinfo($id_token)
$exp = explode(".", $id_token);
return json_decode(base64_decode($exp[1]));
}

public static function getFile($owner, $url, $token){

$curl = new Curl;
$curl->setHeader('Authorization', "Bearer {$token}");
$curl->get($url);
$curl->close();
$response = $curl->response;

$tmp = tempnam("/tmp", "");
$handle = fopen($tmp, "wb");
fwrite($handle,$response);
fclose($handle);

$class_name = $owner->fileClassName;

$basename = md5(time()).".jpg";

$file = new $class_name([
"name" => $basename,
"type" => mime_content_type($tmp),
"tmp_name" => $tmp,
"error" => 0,
"size" => filesize($tmp)
]);

$file->group = "avatar";
$file->owner = $owner;
$file->save(true);
}

public static function newUserProcessor($user, $response)
{
$app = App::i();

$userinfo = (object) $response['auth']['raw'];
$app->disableAccessControl();
$user->profile->nomeCompleto = $userinfo->name;
$user->profile->save(true);
self::getFile($user->profile, $userinfo->picture, $userinfo->access_token);

$app->enableAccessControl();
}
}
5 changes: 1 addition & 4 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ public function _init() {

// Load JS & CSS
$app->hook('<<GET|POST>>(auth.<<*>>)', function() use ($app) {
//$app->view->enqueueScript('app', 'multipleLocal', 'js/multipleLocal.js');
//s$app->view->enqueueStyle('app', 'multipleLocal', 'css/multipleLocal.css');
$app->view->enqueueStyle('app', 'multipleLocal', 'css/govbr.css');

$app->view->enqueueScript('app', 'multipleLocal', 'js/app.js');
$app->view->enqueueStyle('app', 'multipleLocal', 'css/app.css');
$app->view->enqueueStyle('app', 'fontawesome', 'https://use.fontawesome.com/releases/v5.8.2/css/all.css');
$app->view->enqueueStyle('app', 'multipleLocal-govbr', 'css/govbr.css');
});

$app->hook('<<GET|POST|ALL>>(panel.<<*>>):before', function() use ($app) {
Expand Down
27 changes: 24 additions & 3 deletions Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function __construct ($config) {
'timeBlockedloginAttemp' => env('AUTH_BLOCK_TIME', 900), // tempo de bloqueio do usuario em segundos

'metadataFieldCPF' => env('AUTH_METADATA_FIELD_DOCUMENT', 'documento'),
'metadataFieldPhone' => env('AUTH_METADATA_FIELD_PHONE', 'telefone1'),

'urlSupportChat' => env('AUTH_SUPPORT_CHAT', ''),
'urlSupportEmail' => env('AUTH_SUPPORT_EMAIL', ''),
Expand Down Expand Up @@ -91,15 +92,19 @@ function __construct ($config) {
],
'govbr' => [
'visible' => env('AUTH_GOV_BR_ID', false),
'response_type' => null,
'response_type' => 'code',
'client_id' => null,
'client_secret' => null,
'scope' => null,
'redirect_uri' => null,
'auth_endpoint' => null,
'token_endpoint' => null,
'nonce' => null,
'state' => null,
'code_verifier' => null,
'code_challenge' => null,
'code_challenge_method' => null
'code_challenge_method' => null,
'userinfo_endpoint' => null,
'state_salt' => null,
]
]
];
Expand Down Expand Up @@ -1221,6 +1226,10 @@ public function getMetadataFieldCpfFromConfig() {
return $this->_config['metadataFieldCPF'];
}

public function getMetadataFieldPhone() {
return $this->_config['metadataFieldPhone'];
}

public function _getAuthenticatedUser() {
if (is_object($this->_authenticatedUser)) {
return $this->_authenticatedUser;
Expand Down Expand Up @@ -1325,13 +1334,17 @@ protected function _createUser($response) {

$app->em->persist($user);


// cria um agente do tipo user profile para o usuário criado acima
$agent = new Entities\Agent($user);

if(isset($response['auth']['info']['name'])){
$agent->name = $response['auth']['info']['name'];
}elseif(isset($response['auth']['info']['first_name']) && isset($response['auth']['info']['last_name'])){
$agent->name = $response['auth']['info']['first_name'] . ' ' . $response['auth']['info']['last_name'];
}if(isset($response['auth']['info']['phone_number'])){
$metadataFieldPhone = $this->getMetadataFieldPhone();
$agent->setMetadata($metadataFieldPhone, $response['auth']['info']['phone_number']);
}else{
$agent->name = '';
}
Expand All @@ -1345,15 +1358,23 @@ protected function _createUser($response) {

$agent->status = $config['statusCreateAgent'];
$agent->emailPrivado = $user->email;


//$app->em->persist($agent);
$agent->save();
$app->em->flush();


$user->profile = $agent;

$user->save(true);

if($provider_class = $response['auth']['provider']."Strategy"){
if(method_exists($provider_class, "newUserProcessor")){
$provider_class::newUserProcessor($user, $response);
}
}

$app->enableAccessControl();
$redirectUrl = $agent->editUrl;
$app->applyHookBoundTo($this, 'auth.createUser:redirectUrl', [&$redirectUrl]);
Expand Down
10 changes: 9 additions & 1 deletion assets/css/govbr.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.br-sign-in{
background-color: #0000ff3d;
background-color: #eee;
}

.br-sign-in img {
margin-left: 6px;
width: 23% !important;
-webkit-filter: none !important;
filter: none !important;
color:#000;
}
23 changes: 23 additions & 0 deletions assets/img/gov.br_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions views/auth/multiple-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ function showStrategy($name, $config) {
</a>
<?php endif; ?>
<?php if (showStrategy('govbr', $config)) : ?>
<a class="br-sign-in" href="<?php echo $app->createUrl('auth', 'govbr') ?>" style=" background-color: #b000ff">
<a class="br-sign-in" href="<?php echo $app->createUrl('auth', 'govbr') ?>" style="background-color: #eee;color: black;">
Entrar com
<img src="<?php $this->asset('img/govbr-colorido-b.png'); ?>" style="margin-left: 6px;"/>
<img class="br-sign-in-img" src="<?php $this->asset('img/gov.br_logo.svg'); ?>" style="margin-left: 6px;width: 23% !important;-webkit-filter: none !important;filter: none !important;color:#000;"/>
</a>

<?php endif; ?>
Expand Down

0 comments on commit 4f8a65d

Please sign in to comment.