Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSO (Single Sign-On) #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Desenvolvido pela equipe Blitz, o projeto tem como finalidade gerenciar os está

<p align="center">
<a href="https://travis-ci.org/blitzcti/sgecti"><img src="https://travis-ci.org/blitzcti/sgecti.svg?branch=master" alt="Build Status"></a>
<a href="https://github.com/laravel/laravel"><img src="https://img.shields.io/badge/Laravel-6.5.1-red.svg" alt="Laravel Version"></a>
<a href="https://github.com/laravel/laravel"><img src="https://img.shields.io/badge/Laravel-6.18.0-red.svg" alt="Laravel Version"></a>
<a href="https://github.com/ColorlibHQ/AdminLTE"><img src="https://img.shields.io/badge/AdminLTE-2.4.18-blue.svg" alt="AdminLTE Version"></a>
</p>

Expand Down Expand Up @@ -119,7 +119,7 @@ Para isso, crie uma nova tarefa que execute o Laravel Scheduler:
3. Ações

Crie uma nova ação com as seguintes opções:
1. Em `Programa/script` selecione o executável do PHP;
1. Em `Programa/script`, selecione o executável do PHP;
2. Em `Adicione argumentos`, digite `artisan schedule:run`;
3. Em `Iniciar em`, insira o diretório onde o SGE está instalado.

Expand Down
2 changes: 1 addition & 1 deletion app/APIUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function search(array $array, string $q, $col = null)
/**
* Properly sorts an array
*
* @param $array
* @param array $array
* @param null|string $col
*/
public static function sort(array &$array, $col = null)
Expand Down
31 changes: 31 additions & 0 deletions app/AdminLTE/LangFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\AdminLTE;

use JeroenNoten\LaravelAdminLte\Menu\Builder;

class LangFilter extends \JeroenNoten\LaravelAdminLte\Menu\Filters\LangFilter
{
public function transform($item, Builder $builder)
{
if (isset($item['header'])) {
$item['header'] = $this->getTranslation($item['header']) ?? $item['header'];
}
if (isset($item['text'])) {
$item['text'] = $this->getTranslation($item['text']) ?? $item['text'];
}

return $item;
}

protected function getTranslation($item)
{
if ($this->langGenerator->has("menu.{$item}")) {
return $this->langGenerator->get("menu.{$item}");
} elseif ($this->langGenerator->has("adminlte::menu.{$item}")) {
return $this->langGenerator->get("adminlte::menu.{$item}");
}

return $item;
}
}
28 changes: 28 additions & 0 deletions app/AdminLTE/RoleFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\AdminLTE;

use App\Auth;
use JeroenNoten\LaravelAdminLte\Menu\Builder;
use JeroenNoten\LaravelAdminLte\Menu\Filters\FilterInterface;

class RoleFilter implements FilterInterface
{
public function transform($item, Builder $builder)
{
if (!$this->isVisible($item)) {
return false;
}

return $item;
}

protected function isVisible($item)
{
if (!isset($item['role'])) {
return true;
}

return Auth::user()->hasRole($item['role']);
}
}
28 changes: 28 additions & 0 deletions app/AdminLTE/SSOFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\AdminLTE;

use JeroenNoten\LaravelAdminLte\Menu\Builder;
use JeroenNoten\LaravelAdminLte\Menu\Filters\FilterInterface;

class SSOFilter implements FilterInterface
{

public function transform($item, Builder $builder)
{
if (!$this->isVisible($item)) {
return false;
}

return $item;
}

protected function isVisible($item)
{
if (!isset($item['sso'])) {
return true;
}

return $item['sso'] == config('broker.useSSO');
}
}
50 changes: 38 additions & 12 deletions app/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Broker extends \Jasny\SSO\Broker
{
public function __construct($cookie_lifetime = 3600)
public function __construct(int $cookie_lifetime = 3600)
{
$url = config('broker.url');
$broker = config('broker.name');
Expand All @@ -21,6 +21,11 @@ public function __construct($cookie_lifetime = 3600)
$this->saveToken();
}

public function createUser($data)
{
$data;
}

public function serverLoginPage()
{
$parameters = [
Expand All @@ -32,10 +37,21 @@ public function serverLoginPage()
return $this->generateCommandUrl('loginForm', $parameters);
}

public function serverPasswordPage()
{
$parameters = [
'return_url' => $this->getPreviousUrl(),
'broker' => $this->broker,
'session_id' => $this->getSessionId(),
];

return $this->generateCommandUrl('passwordForm', $parameters);
}

/**
* Attach client session to broker session in SSO server.
*
* @param null $returnUrl
* @param null|string $returnUrl
* @return void
*/
public function attach($returnUrl = null)
Expand All @@ -44,7 +60,7 @@ public function attach($returnUrl = null)
'return_url' => $this->getCurrentUrl(),
'broker' => $this->broker,
'token' => $this->token,
'checksum' => hash('sha256', 'attach' . $this->token . $this->secret)
'checksum' => hash('sha256', "attach{$this->token}{$this->secret}")
];

$attachUrl = $this->generateCommandUrl('attach', $parameters);
Expand All @@ -70,14 +86,14 @@ public function getUserInfo()
/**
* Login client to SSO server with user credentials.
*
* @param string $email
* @param string $password
* @param null|string $email
* @param null|string $password
* @param bool $remember
*
* @return bool
* @throws GuzzleException
*/
public function login($email = null, $password = null, $remember = false)
public function login($email = null, $password = null, bool $remember = false)
{
$this->userinfo = $this->makeRequest('POST', 'login', ["email" => $email, "password" => $password, "remember" => $remember]);

Expand Down Expand Up @@ -116,11 +132,11 @@ protected function generateCommandUrl($command, $parameters = [])
$query = '?' . http_build_query($parameters);
}

if ($command == 'loginForm') {
return $this->url . '/loginForm' . $query;
if ($command == 'loginForm' || $command == 'passwordForm') {
return "{$this->url}/{$command}{$query}";
}

return $this->url . '/api/sso/' . $command . $query;
return "{$this->url}/api/sso/{$command}{$query}";
}

/**
Expand All @@ -130,7 +146,7 @@ protected function generateCommandUrl($command, $parameters = [])
*/
protected function getSessionId()
{
$checksum = hash('sha256', 'session' . $this->token . $this->secret);
$checksum = hash('sha256', "session{$this->token}{$this->secret}");
return "SSO-{$this->broker}-{$this->token}-$checksum";
}

Expand Down Expand Up @@ -184,7 +200,7 @@ protected function makeRequest($method, $command, $parameters = [])

$headers = [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->getSessionId(),
'Authorization' => "Bearer {$this->getSessionId()}",
];

switch ($method) {
Expand Down Expand Up @@ -228,7 +244,7 @@ protected function redirect($url, $parameters = [], $httpResponseCode = 307)
$query .= http_build_query($parameters);
}

app()->abort($httpResponseCode, '', ['Location' => $url . $query]);
app()->abort($httpResponseCode, '', ['Location' => "{$url}{$query}"]);
}

/**
Expand All @@ -241,6 +257,16 @@ protected function getCurrentUrl()
return url()->full();
}

/**
* Getting previous url which can be used as return to url.
*
* @return string
*/
protected function getPreviousUrl()
{
return url()->previous();
}

/**
* Cookie name in which we save unique client token.
*
Expand Down
9 changes: 1 addition & 8 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Console;

use App\Models\Agreement;
use App\Models\BackupConfiguration;
use App\Models\Coordinator;
use App\Models\FinalReport;
Expand Down Expand Up @@ -45,8 +44,7 @@ private function isConnected()
protected function schedule(Schedule $schedule)
{
if ($this->isConnected()) {
$backupConfig = new BackupConfiguration();
if (Schema::hasTable($backupConfig->getTable())) {
if (Schema::hasTable((new BackupConfiguration())->getTable())) {
$backupConfig = BackupConfiguration::findOrFail(1);
$days = $backupConfig->cronDays();
$hour = $backupConfig->getHour();
Expand All @@ -55,11 +53,6 @@ protected function schedule(Schedule $schedule)
$schedule->call('App\Http\Controllers\Admin\LogController@clearLogs')->monthly()->at('00:00');
}

if (Schema::hasTable((new User())->getTable())
&& Schema::hasTable((new Agreement())->getTable())) {
$schedule->call('App\Http\Controllers\Coordinator\AgreementController@deleteUsers')->daily()->at('00:00');
}

if (Schema::hasTable((new User())->getTable())
&& Schema::hasTable((new Coordinator())->getTable())
&& Schema::hasTable((new DatabaseNotification())->getTable())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getById($id)

public function getByRA($ra)
{
$internships = Internship::where('ra', '=', $ra)->get()->sortBy('id');
$internships = Internship::where('ra', '=', $ra)->orderBy('id')->get();

return response()->json(
$internships,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/Coordinator/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getById($id)

public function getByRA($ra)
{
$jobs = Job::where('ra', '=', $ra)->get()->sortBy('id');
$jobs = Job::where('ra', '=', $ra)->orderBy('id')->get();

return response()->json(
$jobs,
Expand Down
50 changes: 36 additions & 14 deletions app/Http/Controllers/API/ExternalAPISController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@

use App\APIUtils;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ExternalAPISController extends Controller
{
public function getUFS(Request $request)
{
$url = APIUtils::parseURL('apis.ufs.url');
$column = config('apis.ufs.column');
$json = APIUtils::getData($url);
$json = array_column($json, $column);
APIUtils::sort($json);
if (Cache::has('extapi.ufs')) {
$json = Cache::get('extapi.ufs');
} else {
$url = APIUtils::parseURL('apis.ufs.url');
$column = config('apis.ufs.column');
$json = APIUtils::getData($url);
$json = array_column($json, $column);
APIUtils::sort($json);

Cache::put('extapi.ufs', $json, Carbon::now()->addDay());
}

if (!empty($request->q)) {
$json = APIUtils::search($json, $request->q);
Expand Down Expand Up @@ -48,10 +56,17 @@ public function getCities($uf, Request $request)
);
}

$url = APIUtils::parseURL('apis.ufs.url');
$column = config('apis.ufs.column');
$json = APIUtils::getData($url);
$json = array_column($json, 'id', $column);
if (Cache::has('extapi.ufsid')) {
$json = Cache::get('extapi.ufsid');
} else {
$url = APIUtils::parseURL('apis.ufs.url');
$column = config('apis.ufs.column');
$json = APIUtils::getData($url);
$json = array_column($json, 'id', $column);

Cache::put('extapi.ufsid', $json, Carbon::now()->addDay());
}


$ufId = 0;
foreach ($json as $data => $id) {
Expand All @@ -74,11 +89,18 @@ public function getCities($uf, Request $request)
);
}

$url = APIUtils::parseURL('apis.cities.url', $ufId);
$column = config('apis.cities.column');
$json = APIUtils::getData($url);
$json = array_column($json, $column);
APIUtils::sort($json);
if (Cache::has("extapi.cities_{$ufId}")) {
$json = Cache::get("extapi.cities_{$ufId}");
} else {
$url = APIUtils::parseURL('apis.cities.url', $ufId);
$column = config('apis.cities.column');
$json = APIUtils::getData($url);
$json = array_column($json, $column);
APIUtils::sort($json);

Cache::put("extapi.cities_{$ufId}", $json, Carbon::now()->addDay());
}


if (!empty($request->q)) {
$json = APIUtils::search($json, $request->q);
Expand Down
Loading