Skip to content

Commit 79f9726

Browse files
author
olimar
committed
Added Client POST methods to Oauth2 Facade;
1 parent 0547e63 commit 79f9726

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

Providers/OpenIdServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function registerBladeDirectives()
108108
});
109109

110110
Blade::directive('login', function ($route) {
111-
if(\Auth::check()) return route($route);
111+
if(\Auth::check()) return "<?php echo route($route); ?>";
112112

113113
return "<?php echo config('openid.server') . '/login?' . http_build_query(['continue' => route($route)]); ?>";
114114
});

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The file `config/openid.php` will be generated.
3535
In the file `app\Exceptions\Handler.php` find or overwrite the `unauthenticated` method and change the redirect route to:
3636

3737
```php
38-
config('server') . '/login?continue=' . env('APP_URL')
38+
config('openid.server') . '/login?continue=' . env('APP_URL')
3939
```
4040

4141
**PS:** Don't forget change the `SESSION_LIFETIME` in the .env file to the time in minutes you want to keep the logged session.
@@ -44,7 +44,7 @@ In the file `app\Exceptions\Handler.php` find or overwrite the `unauthenticated`
4444

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

47-
```
47+
```php
4848
'guards' => [
4949
'web' => [
5050
'driver' => 'openid',
@@ -66,22 +66,24 @@ The client methods are available under the facade OpenId.
6666

6767
The authentication methods like the verifier `\Auth::check()` are available under the Facade `\Illuminate\Support\Facades\Auth`;
6868

69+
The facade Oauth2 provides all helpers needed to get and post data from the Oauth Server.
70+
6971
## _View components_
7072

7173
`@openidComponents`:
7274

7375
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.
7476

75-
`@login(route.name)`:
77+
`@login('route.name')`:
7678

77-
The login directive will call the route you
79+
The login directive will call the route you pass or return the oauth path formated with the continue parameter to the required route.
7880

7981
## _Redirecting routes_
8082

8183
The dynamic route from Oauth system can redirect the user back to the source using the `?continue` url parameter.
8284

8385
The following example will be redirect back to the source after the user executes the actions needed in the Oauth Service page:
8486

85-
```
87+
```php
8688
config('openid.server') . '{$ouath_service_page}?' . http_build_query(['continue' => {$route_to_redirect_back}])
8789
```

Services/Api.php

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Api
2323
private static $initialized = FALSE;
2424

2525
/**
26-
* Class constructor
26+
* Static class constructor
2727
*/
2828
private static function initialize()
2929
{
@@ -35,6 +35,8 @@ private static function initialize()
3535
}
3636

3737
/**
38+
* @api (GET '/api/{version}/system')
39+
*
3840
* @return array
3941
*/
4042
public static function getSystems()
@@ -43,6 +45,8 @@ public static function getSystems()
4345
}
4446

4547
/**
48+
* @api (GET '/api/{version}/system/roles')
49+
*
4650
* @return array
4751
*/
4852
public static function getSystemRoles()
@@ -51,6 +55,8 @@ public static function getSystemRoles()
5155
}
5256

5357
/**
58+
* @api (GET '/api/{version}/user')
59+
*
5460
* @return array
5561
*/
5662
public static function getUser()
@@ -59,6 +65,20 @@ public static function getUser()
5965
}
6066

6167
/**
68+
* @api (POST '/api/{version}/user/cpf')
69+
*
70+
* @param string $cpf
71+
*
72+
* @return array
73+
*/
74+
public static function getUserByCpf(string $cpf)
75+
{
76+
return self::getResponse('user/cpf', ['cpf' => $cpf]);
77+
}
78+
79+
/**
80+
* @api (GET '/api/{version}/user/systems')
81+
*
6282
* @return array
6383
*/
6484
public static function getUserSystems()
@@ -67,6 +87,8 @@ public static function getUserSystems()
6787
}
6888

6989
/**
90+
* @api (GET '/api/{version}/user/permissions')
91+
*
7092
* @return array
7193
*/
7294
public static function getUserPermissions()
@@ -75,14 +97,29 @@ public static function getUserPermissions()
7597
}
7698

7799
/**
100+
* @api (POST '/api/{version}/user/permission')
101+
*
102+
* @param string $cpf
103+
* @param string $role
104+
* @param string $expires_at
78105
*
106+
* @return array
79107
*/
80-
public static function createUserPermission()
108+
public static function setUserPermission(string $cpf, string $role, string $expires_at = '')
81109
{
82-
//
110+
$params = [
111+
'user' => $cpf,
112+
'role' => $role,
113+
];
114+
115+
if ($expires_at !== '') $params['expires_at'] = date('Y-m-d', strtotime($expires_at));
116+
117+
return self::postData('user/permission', $params);
83118
}
84119

85120
/**
121+
* @api (GET '/api/{version}/address/states')
122+
*
86123
* @return array
87124
*/
88125
public static function getStates()
@@ -91,6 +128,8 @@ public static function getStates()
91128
}
92129

93130
/**
131+
* @api (GET '/api/{version}/address/cities/{state}')
132+
*
94133
* @param string $state
95134
*
96135
* @return array
@@ -101,23 +140,64 @@ public static function getCities(string $state)
101140
}
102141

103142
/**
143+
* @api (GET '/api/{version}/address/filled')
144+
*
104145
* @return array
105146
*/
106147
public static function isAddressFilled()
107148
{
108149
return self::getResponse('address/filled');
109150
}
110151

152+
/**
153+
* @api (POST '/api/{version}/address')
154+
* @example
155+
*
156+
* @param array $data Requires 'zip', 'street', 'number', 'complement', 'district' and 'city' keys
157+
*
158+
* @return array
159+
*/
160+
public static function setAddress(array $data)
161+
{
162+
$params = array_only($data, ['zip', 'street', 'number', 'complement', 'district', 'city']);
163+
164+
return self::postData('address', $params);
165+
}
166+
167+
/**
168+
* @param string $uri
169+
* @param array $params
170+
*
171+
* @return array
172+
*/
173+
private static function getResponse(string $uri, array $params = []): array
174+
{
175+
self::initialize();
176+
177+
if(empty($params)) {
178+
$response = self::$client->get('api/' . self::$version . '/' . $uri);
179+
} else {
180+
$response = self::$client->post('api/' . self::$version . '/' . $uri, [
181+
'form_params' => $params,
182+
]);
183+
}
184+
185+
return json_decode($response->getBody(), TRUE);
186+
}
187+
111188
/**
112189
* @param string $uri
190+
* @param array $data
113191
*
114192
* @return array
115193
*/
116-
private static function getResponse(string $uri): array
194+
private static function postData(string $uri, array $data): array
117195
{
118196
self::initialize();
119197

120-
$response = self::$client->get('api/' . self::$version . '/' . $uri);
198+
$response = self::$client->post('api/' . self::$version . '/' . $uri, [
199+
'form_params' => $data,
200+
]);
121201

122202
return json_decode($response->getBody(), TRUE);
123203
}

0 commit comments

Comments
 (0)