Skip to content

Commit 03744ef

Browse files
author
olimar
committed
Added Server POST methods to Oauth2 Facade; Doc Updated;
1 parent 79f9726 commit 03744ef

File tree

3 files changed

+167
-45
lines changed

3 files changed

+167
-45
lines changed

README.md

Lines changed: 99 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# Univiçosa Laravel OpenId Client
22

3-
| **Laravel** | **laravel-openid-client** |
4-
|------|------|
5-
| 5.4 | ^0.7.3 |
6-
| 5.5 | ^0.7.3 |
7-
| 5.6 | ^0.7.3 |
8-
93
`univicosa/laravel-openid-client` is a Laravel package which created to integrate the Oauth server to ours Laravel project's that requires authentication.
104

115
## Install
@@ -16,7 +10,7 @@ Installation using composer:
1610
composer require univicosa/laravel-openid-client
1711
```
1812

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

2115
```
2216
Modules\OpenId\Providers\OpenIdServiceProvider::class
@@ -30,17 +24,23 @@ php artisan vendor:publish --tag=openid-config
3024

3125
The file `config/openid.php` will be generated.
3226

33-
## Redirecting to _Login_
27+
### Oauth `public key`
28+
29+
Your system need the `oauth public key` to connect and communicate with the Oauth Server.So you need to copy the public key file to `storage` folder of your project.
30+
31+
### Redirecting to _Login_
3432

3533
In the file `app\Exceptions\Handler.php` find or overwrite the `unauthenticated` method and change the redirect route to:
3634

3735
```php
3836
config('openid.server') . '/login?continue=' . env('APP_URL')
3937
```
4038

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.
39+
### _Session Lifetime_
40+
41+
Set a variable called `SESSION_LIFETIME` in the `.env` file and define it to the time in minutes you want to keep the logged session. The max time of the Oauth Server keeps the session is 240 minutes (4 hours).
4242

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

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

@@ -56,17 +56,99 @@ change the file `config\auth.php` to:
5656
]
5757
```
5858

59-
## Oauth `public key`
59+
## _Facades_
6060

61-
Copy the `oauth public key` to `storage` folder of your project.
61+
The client methods are available under the facade **\OpenId**.
6262

63-
## _Facades_
63+
The authentication methods like the verifier `\Auth::check()` are available under the Facade **\Illuminate\Support\Facades\Auth**;
64+
65+
The facade **\Oauth2** provides all helpers needed to get and post data from the Oauth Server.
6466

65-
The client methods are available under the facade OpenId.
67+
### \Ouath2 methods available
6668

67-
The authentication methods like the verifier `\Auth::check()` are available under the Facade `\Illuminate\Support\Facades\Auth`;
69+
```php
70+
@method \Oauth2::getSystems(): array
71+
@api GET '/api/{version}/system'
72+
73+
@return array With Systems available in Oauth Server
74+
```
75+
76+
```php
77+
@method \Oauth2::getSystemRoles(): array
78+
@api GET '/api/{version}/system/roles'
79+
80+
@return array With System givable roles
81+
```
6882

69-
The facade Oauth2 provides all helpers needed to get and post data from the Oauth Server.
83+
```php
84+
@method \Oauth2::getUser(): array
85+
@api GET '/api/{version}/user'
86+
87+
@return array With logged user data
88+
```
89+
90+
```php
91+
@method \Oauth2::getUserByCpf(string $cpf): array
92+
@api POST '/api/{version}/user/cpf'
93+
94+
@return array With the data of user owner of document given
95+
```
96+
97+
```php
98+
@method \Oauth2::getUserSystems(): array
99+
@api GET '/api/{version}/user/systems'
100+
101+
@return array With the systems that the user is allowed to access
102+
```
103+
104+
```php
105+
@method \Oauth2::getUserPermissions(): array
106+
@api GET '/api/{version}/user/permissions'
107+
108+
@return array With the roles that the logged user has in the request owner
109+
```
110+
111+
```php
112+
@method \Oauth2::getGenders(): array
113+
@api GET '/api/{version}/profile/genders'
114+
115+
@return array With th data of all genders available for select on the Oauth Server
116+
```
117+
118+
```php
119+
@method \Oauth2::setUserPermission(string $cpf, string $role, string $expires_at = ''): array
120+
@api POST '/api/{version}/user/permission'
121+
122+
@return array with the response of Post action
123+
```
124+
125+
```php
126+
@method \Oauth2::getStates(): array
127+
@api GET '/api/{version}/address/states'
128+
129+
@return array With the data of all Brazilian states present on the Oauth Server
130+
```
131+
132+
```php
133+
@method \Oauth2::getCities(string $state): array
134+
@api GET '/api/{version}/address/cities/{state}'
135+
136+
@return array With the data of all Brazilian cities according to the state given present on the Oauth Server
137+
```
138+
139+
```php
140+
@method \Oauth2::setAddress(array $data): array
141+
@api POST '/api/{version}/address'
142+
143+
@return array with the response of Post action
144+
```
145+
146+
```php
147+
@method \Oauth2::setProfile(array $data): array
148+
@api POST '/api/{version}/profile'
149+
150+
@return array with the response of Post action
151+
```
70152

71153
## _View components_
72154

@@ -85,5 +167,5 @@ The dynamic route from Oauth system can redirect the user back to the source usi
85167
The following example will be redirect back to the source after the user executes the actions needed in the Oauth Service page:
86168

87169
```php
88-
config('openid.server') . '{$ouath_service_page}?' . http_build_query(['continue' => {$route_to_redirect_back}])
170+
config('openid.server') . '{ouath_service_page}?' . http_build_query(['continue' => {route_to_redirect_back}])
89171
```

Services/Api.php

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,75 +35,85 @@ private static function initialize()
3535
}
3636

3737
/**
38-
* @api (GET '/api/{version}/system')
38+
* @api GET '/api/{version}/system'
3939
*
40-
* @return array
40+
* @return array With Systems available in Oauth Server
4141
*/
4242
public static function getSystems()
4343
{
4444
return self::getResponse('system');
4545
}
4646

4747
/**
48-
* @api (GET '/api/{version}/system/roles')
48+
* @api GET '/api/{version}/system/roles'
4949
*
50-
* @return array
50+
* @return array With System givable roles
5151
*/
5252
public static function getSystemRoles()
5353
{
5454
return self::getResponse('system/roles');
5555
}
5656

5757
/**
58-
* @api (GET '/api/{version}/user')
58+
* @api GET '/api/{version}/user'
5959
*
60-
* @return array
60+
* @return array With logged user data
6161
*/
6262
public static function getUser()
6363
{
6464
return self::getResponse('user');
6565
}
6666

6767
/**
68-
* @api (POST '/api/{version}/user/cpf')
68+
* @api POST '/api/{version}/user/cpf'
6969
*
7070
* @param string $cpf
7171
*
72-
* @return array
72+
* @return array With the data of user owner of document given
7373
*/
7474
public static function getUserByCpf(string $cpf)
7575
{
7676
return self::getResponse('user/cpf', ['cpf' => $cpf]);
7777
}
7878

7979
/**
80-
* @api (GET '/api/{version}/user/systems')
80+
* @api GET '/api/{version}/user/systems'
8181
*
82-
* @return array
82+
* @return array With the systems that the user is allowed to access
8383
*/
8484
public static function getUserSystems()
8585
{
8686
return self::getResponse('user/systems');
8787
}
8888

8989
/**
90-
* @api (GET '/api/{version}/user/permissions')
90+
* @api GET '/api/{version}/user/permissions'
9191
*
92-
* @return array
92+
* @return array With the roles that the logged user has in the request owner
9393
*/
9494
public static function getUserPermissions()
9595
{
9696
return self::getResponse('user/permissions');
9797
}
9898

9999
/**
100-
* @api (POST '/api/{version}/user/permission')
100+
* @api GET '/api/{version}/user/genders'
101+
*
102+
* @return array With the data of all genders available for select on the Oauth Server
103+
*/
104+
public static function getGenders()
105+
{
106+
return self::getResponse('profile/genders');
107+
}
108+
109+
/**
110+
* @api POST '/api/{version}/user/permission'
101111
*
102112
* @param string $cpf
103113
* @param string $role
104114
* @param string $expires_at
105115
*
106-
* @return array
116+
* @return array with the response of Post action
107117
*/
108118
public static function setUserPermission(string $cpf, string $role, string $expires_at = '')
109119
{
@@ -118,44 +128,57 @@ public static function setUserPermission(string $cpf, string $role, string $expi
118128
}
119129

120130
/**
121-
* @api (GET '/api/{version}/address/states')
131+
* @api POST '/api/{version}/user/profile'
122132
*
123-
* @return array
133+
* @param array $data Requires 'name', 'social_name', 'gender', 'birthday_date', 'identity' and 'phones' keys
134+
*
135+
* @return array with the response of Post action
136+
*/
137+
public static function setProfile(array $data)
138+
{
139+
$params = array_only($data, ['name', 'social_name', 'gender', 'birthday_date', 'identity', 'phones']);
140+
141+
return self::postData('profile', $params);
142+
}
143+
144+
/**
145+
* @api GET '/api/{version}/address/states'
146+
*
147+
* @return array With the data of all Brazilian states present on the Oauth Server
124148
*/
125149
public static function getStates()
126150
{
127151
return self::getResponse('address/states');
128152
}
129153

130154
/**
131-
* @api (GET '/api/{version}/address/cities/{state}')
155+
* @api GET '/api/{version}/address/cities/{state}'
132156
*
133157
* @param string $state
134158
*
135-
* @return array
159+
* @return array With the data of all Brazilian cities according to the state given present on the Oauth Server
136160
*/
137161
public static function getCities(string $state)
138162
{
139163
return self::getResponse('address/cities/' . $state);
140164
}
141165

142166
/**
143-
* @api (GET '/api/{version}/address/filled')
167+
* @api GET '/api/{version}/address/filled'
144168
*
145-
* @return array
169+
* @return array With the Boolean response if the user address data is populated on the Oauth Server
146170
*/
147171
public static function isAddressFilled()
148172
{
149173
return self::getResponse('address/filled');
150174
}
151175

152176
/**
153-
* @api (POST '/api/{version}/address')
154-
* @example
177+
* @api POST '/api/{version}/address'
155178
*
156179
* @param array $data Requires 'zip', 'street', 'number', 'complement', 'district' and 'city' keys
157180
*
158-
* @return array
181+
* @return array with the response of Post action
159182
*/
160183
public static function setAddress(array $data)
161184
{
@@ -174,7 +197,7 @@ private static function getResponse(string $uri, array $params = []): array
174197
{
175198
self::initialize();
176199

177-
if(empty($params)) {
200+
if (empty($params)) {
178201
$response = self::$client->get('api/' . self::$version . '/' . $uri);
179202
} else {
180203
$response = self::$client->post('api/' . self::$version . '/' . $uri, [

Services/Client.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,28 @@ public static function getServerClient()
2525
{
2626
return new \GuzzleHttp\Client([
2727
'base_uri' => config('openid.server'),
28-
'headers' => [
29-
'ClientId' => config('openid.client.id'),
28+
'headers' => [
29+
'ClientId' => config('openid.client.id'),
3030
'ClientSecret' => config('openid.client.secret'),
31-
'Accept' => 'application/json',
32-
]
31+
'Accept' => 'application/json',
32+
],
3333
]);
3434
}
35+
36+
/**
37+
* @param array $data
38+
*
39+
* @return array
40+
*/
41+
public static function createUser(array $data): array
42+
{
43+
$client = self::getServerClient();
44+
$params = array_only($data, ['name', 'email', 'cpf']);
45+
46+
$response = $client->post('api/' . config('openid.api-version') . '/user', [
47+
'form_params' => $params,
48+
]);
49+
50+
return json_decode($response->getBody(), TRUE);
51+
}
3552
}

0 commit comments

Comments
 (0)