Skip to content

Commit 40dec55

Browse files
author
olimar
committed
Created resources for the auth UI components;
1 parent 55a2504 commit 40dec55

File tree

18 files changed

+489
-46
lines changed

18 files changed

+489
-46
lines changed

Config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
return [
4+
'namespace' => 'openid',
45
/*
56
| path of the public key
67
*/

Http/Controllers/OpenIdController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public function callback(Request $request)
3737
break;
3838
}
3939
}
40+
4041
$code = $request->get('code');
4142
$http = new Client();
43+
4244
try {
4345
$response = $http->post(config('openid.server') . '/oauth/token', [
4446
'form_params' => [
@@ -49,8 +51,10 @@ public function callback(Request $request)
4951
'code' => $code,
5052
],
5153
]);
54+
5255
$this->checkError($response);
5356
$response = json_decode($response->getBody());
57+
5458
session([
5559
'openid_token' => $response->id_token,
5660
'access_token' => $response->access_token,
@@ -61,6 +65,7 @@ public function callback(Request $request)
6165
if ($exception instanceof ServerException || $exception instanceof ClientException) {
6266
$this->checkError($exception->getResponse());
6367
}
68+
6469
abort($exception->getCode() !== 0 ? $exception->getCode() : 500);
6570
}
6671

@@ -73,6 +78,7 @@ public function callback(Request $request)
7378
protected function checkError(ResponseInterface $response)
7479
{
7580
$result = json_decode($response->getBody());
81+
7682
if (isset($result->error)) {
7783
switch ($result->error) {
7884
case 'invalid_request':
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 04/06/2018 - 14:35
6+
*/
7+
8+
namespace Modules\OpenId\Http\ViewComposers;
9+
10+
11+
use Illuminate\Support\Facades\Auth;
12+
use Illuminate\Support\Facades\Session;
13+
use Illuminate\View\View;
14+
15+
class UserSystemsComposer
16+
{
17+
/**
18+
* @param View $view
19+
*/
20+
public function compose(View $view)
21+
{
22+
if (\Auth::check()) {
23+
$view->with('avatar', \Auth::user()->avatar);
24+
$view->with('firstName', $this->name(TRUE));
25+
$view->with('fullName', $this->name());
26+
$view->with('email', $this->email());
27+
$view->with('greeting', $this->greeting() . ', ');
28+
$view->with('systems', $this->systems());
29+
$view->with('logout', config('openid.server') . '/logout?continue=' . env('APP_URL'));
30+
} else {
31+
$view->with('login', config('openid.server') . '/login?' . http_build_query(['continue' => env('APP_URL')]));
32+
}
33+
}
34+
35+
/**
36+
* @param bool $first
37+
*
38+
* @return string
39+
*/
40+
private function name($first = FALSE): string
41+
{
42+
$name = isset(\Auth::user()->name) ? \Auth::user()->name : 'John Doe';
43+
44+
if ($first) return explode(' ', $name)[0];
45+
46+
return $name;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
private function email(): string
53+
{
54+
return isset(\Auth::user()->email) ? \Auth::user()->email : __('undefined e-mail');
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
private function greeting(): string
61+
{
62+
if (date('H') < 12) return __('openid::greeting.good_morning');
63+
if (date('H') < 18) return __('openid::greeting.good_afternoon');
64+
if (date('H') <= 23) return __('openid::greeting.good_night');
65+
66+
return __('openid::greeting.hello');
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
private function systems(): array
73+
{
74+
if (!Session::has('systems') && Auth::check()){
75+
$client = \OpenId::getClient();
76+
77+
$response = $client->get('api/systemsByUser');
78+
79+
$response = json_decode($response->getBody() , true);
80+
Session::put($response);
81+
}
82+
83+
return \Session::get('systems');
84+
}
85+
}

Providers/OpenIdServiceProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Modules\OpenId\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Support\Facades\View;
7+
use Illuminate\Support\Facades\Blade;
68
use Modules\OpenId\Guards\CustomSessionGuard;
79
use Modules\OpenId\Guards\CustomTokenGuard;
810
use Modules\OpenId\Services\Client;
@@ -24,7 +26,12 @@ class OpenIdServiceProvider extends ServiceProvider
2426
public function boot()
2527
{
2628
$this->registerConfig();
29+
$this->registerViewComposers();
30+
2731
$this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
32+
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', config('openid.namespace'));
33+
$this->loadViewsFrom(__DIR__ . '/../Resources/views', config('openid.namespace'));
34+
2835
$this->app->singleton('openid', function ($app) {
2936
return new Client();
3037
});
@@ -38,6 +45,7 @@ public function boot()
3845
public function register()
3946
{
4047
$this->registerGuard();
48+
$this->registerBladeDirectives();
4149
}
4250

4351
/**
@@ -55,6 +63,11 @@ protected function registerConfig()
5563
);
5664
}
5765

66+
/**
67+
* Register Guard Session
68+
*
69+
* @return void
70+
*/
5871
protected function registerGuard()
5972
{
6073
\Auth::extend('openid', function ($app, $name, array $config) {
@@ -65,4 +78,28 @@ protected function registerGuard()
6578
return new CustomTokenGuard();
6679
});
6780
}
81+
82+
/**
83+
* Register ViewComposers
84+
*
85+
* @return void
86+
*/
87+
protected function registerViewComposers()
88+
{
89+
View::composer(
90+
'openid::menu', 'Modules\OpenId\Http\ViewComposers\UserSystemsComposer'
91+
);
92+
}
93+
94+
/**
95+
* Register Blade Directives
96+
*
97+
* @return void
98+
*/
99+
protected function registerBladeDirectives()
100+
{
101+
Blade::directive('openidComponents', function () {
102+
return "<?php echo view('openid::menu')->render(); ?>";
103+
});
104+
}
68105
}

README.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
| 5.5 | ^0.7.3 |
77
| 5.6 | ^0.7.3 |
88

9-
`univicosa/laravel-openid-client` is a Laravel package which created to integrate the Oauth server to ours Laravel project's that require authentication.
9+
`univicosa/laravel-openid-client` is a Laravel package which created to integrate the Oauth server to ours Laravel project's that requires authentication.
1010

1111
## Install
1212

@@ -16,13 +16,13 @@ Installation using composer:
1616
composer require univicosa/laravel-openid-client
1717
```
1818

19-
And add the service provider in `config/app.php`:
19+
For laravel versions < 5.5 add the service provider in `config/app.php`:
2020

2121
```
2222
Modules\OpenId\Providers\OpenIdServiceProvider::class
2323
```
2424

25-
Publish the package's configuration file by running:
25+
To personalize the config, publish the package's configuration file by running:
2626

2727
```
2828
php artisan vendor:publish --tag=openid-config
@@ -32,34 +32,56 @@ The file `config/openid.php` will be generated.
3232

3333
## Redirecting to _Login_
3434

35-
In the file `app\Exceptions\Handler.php` find or overwrite the `unauthenticated` method and change de redirect route to:
35+
In the file `app\Exceptions\Handler.php` find or overwrite the `unauthenticated` method and change the redirect route to:
3636

3737
```php
3838
config('server') . '/login?continue=' . env('APP_URL')
3939
```
4040

41-
**PS:** Don't forget change the session time to (minutes) you want.
41+
**PS:** Don't forget change the `SESSION_LIFETIME` in the .env file to the time in minutes you want to keep the logged session.
4242

43-
## Changing the _Guard_
43+
## For change the _Guard_
4444

4545
change the file `config\auth.php` to:
4646

4747
```
48-
'guards' => [
49-
'web' => [
50-
'driver' => 'openid',
51-
'provider' => NULL,
52-
],
53-
/*
54-
* ...
55-
*/
56-
]
48+
'guards' => [
49+
'web' => [
50+
'driver' => 'openid',
51+
'provider' => NULL,
52+
],
53+
/*
54+
* ...
55+
*/
56+
]
5757
```
5858

5959
## Oauth `public key`
6060

6161
Copy the `oauth public key` to `storage` folder of your project.
6262

63-
## Facades
63+
## _Facades_
6464

65-
The client methods are available under the facade OpenId
65+
The client methods are available under the facade OpenId.
66+
67+
The authentication methods like the verifier `\Auth::check()` are available under the Facade `\Illuminate\Support\Facades\Auth`;
68+
69+
## _View components_
70+
71+
`@openidComponents`:
72+
73+
For load the user's logged menu, the fast access with the users permissions and render the Login Button in case you have not authenticated page, just call the Blade directive under your header component.
74+
75+
`@login(route.name)`:
76+
77+
The login directive will call the route you
78+
79+
## _Redirecting routes_
80+
81+
The dynamic route from Oauth system can redirect the user back to the source using the `?continue` url parameter.
82+
83+
The following example will be redirect back to the source after the user executes the actions needed in the Oauth Service page:
84+
85+
```
86+
config('openid.server') . '{$ouath_service_page}?' . http_build_query(['continue' => {$route_to_redirect_back}])
87+
```

Resources/lang/en/errors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 04/06/2018 - 17:36
6+
*/
7+
8+
return [
9+
10+
];

Resources/lang/en/greeting.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 04/06/2018 - 17:08
6+
*/
7+
8+
return [
9+
'hello' => 'Hello',
10+
'good_morning' => 'Good morning',
11+
'good_afternoon' => 'Good afternoon',
12+
'good_night' => 'Good night',
13+
];

Resources/lang/en/menu.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 04/06/2018 - 17:12
6+
*/
7+
8+
return [
9+
'fast_access' => 'Fast access',
10+
'shortcuts' => 'Atalhos',
11+
];

Resources/lang/en/options.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 04/06/2018 - 16:55
6+
*/
7+
8+
return [
9+
'login' => 'Login',
10+
'my_profile' => 'My profile',
11+
'section' => 'Sessão',
12+
'my_profile' => 'Meu perfil',
13+
'activity' => 'Activity',
14+
'update_profile' => 'Update profile',
15+
'update_email' => 'Update email',
16+
'update_address' => 'Update ddress',
17+
'change_password' => 'Change password',
18+
'support' => 'Support',
19+
'logout' => 'Logout',
20+
];

Resources/lang/pt-BR/errors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 04/06/2018 - 17:36
6+
*/
7+
8+
return [
9+
10+
];

0 commit comments

Comments
 (0)