Skip to content

Commit a43e210

Browse files
committed
✨ Use facade for OAuthClient and change namespaces
1 parent 8373259 commit a43e210

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

README.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ A package that allows secure communication between two or more projects, focused
3333
```php
3434
protected $routeMiddleware = [
3535
// Other middleware...
36-
'jwt' => \Diimolabs\Oauth2Client\Middleware\EnsureJwtIsValid::class
36+
'jwt' => \Diimolabs\OAuth\Middleware\EnsureJwtIsValid::class
3737
];
3838
```
3939
4040
## Use
4141
4242
Example of requesting a resource to a microservice
43-
4443
```php
45-
use Diimolabs\Oauth2Client\OAuthHttpClient;
44+
use Diimolabs\OAuth\Facades\OAuthClient;
45+
use Illuminate\Support\Facades\Route;
4646
4747
Route::prefix('v1')->group(function(){
4848
Route::get('message', function(){
49-
return OAuthHttpClient::oauthRequest()
49+
return OAuthClient::request()
5050
->get('http://msa-2.test/api/v1/hello-world')
5151
->body();
5252
});
@@ -55,8 +55,9 @@ Route::prefix('v1')->group(function(){
5555
```
5656

5757
Example of a request from a microservice client
58-
5958
```php
59+
use Illuminate\Support\Facades\Route;
60+
6061
Route::prefix('v1')->middleware('jwt')->group(function ()
6162
{
6263
Route::get('/hello-world', function ()
@@ -69,9 +70,7 @@ Route::prefix('v1')->middleware('jwt')->group(function ()
6970
# Extra
7071

7172
import the configuration file using:
72-
7373
```
7474
php artisan vendor:publish --tag=oauth-client
7575
```
76-
7776
in `external_services` you can manage the urls of your different services

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
},
2323
"autoload": {
2424
"psr-4": {
25-
"Diimolabs\\Oauth2Client\\": "src/"
25+
"Diimolabs\\OAuth\\": "src/"
2626
}
2727
},
2828
"extra": {
2929
"laravel": {
3030
"providers": [
31-
"Diimolabs\\Oauth2Client\\OAuthServiceProvider"
31+
"Diimolabs\\OAuth\\OAuthServiceProvider"
3232
]
3333
}
3434
}

src/Facades/OAuthClient.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Diimolabs\OAuth\Facades;
4+
5+
use Diimolabs\OAuth\OAuthHttpClient;
6+
use Illuminate\Support\Facades\Facade;
7+
8+
class OAuthClient extends Facade
9+
{
10+
/**
11+
* Get the registered name of the component.
12+
*
13+
* @return string
14+
*/
15+
protected static function getFacadeAccessor()
16+
{
17+
return OAuthHttpClient::class;
18+
}
19+
}

src/OAuthHttpClient.php

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
<?php
22

3-
namespace Diimolabs\Oauth2Client;
3+
namespace Diimolabs\OAuth;
44

55
use Illuminate\Support\Facades\Http;
66
use Illuminate\Support\Facades\Storage;
77

88
class OAuthHttpClient
99
{
10-
private static $path;
10+
private $path;
1111

12-
private static $host;
12+
private $host;
1313

14-
private static $clientId;
14+
private $clientId;
1515

16-
private static $clientSecret;
16+
private $clientSecret;
1717

18-
private static function getCredentialsInfo()
18+
private function getCredentialsInfo()
1919
{
2020
return collect(json_decode(
21-
file_get_contents(self::$path)
21+
file_get_contents($this->path)
2222
));
2323
}
2424

25-
private static function requestToken()
25+
private function requestToken()
2626
{
2727
$response = Http::acceptJson()
28-
->post(self::$host . '/oauth/token', [
28+
->post($this->host . '/oauth/token', [
2929
'grant_type' => 'client_credentials',
30-
'client_id' => self::$clientId,
31-
'client_secret' => self::$clientSecret,
30+
'client_id' => $this->clientId,
31+
'client_secret' => $this->clientSecret,
3232
]);
3333

3434
if ($response->failed()) {
@@ -38,44 +38,44 @@ private static function requestToken()
3838
Storage::put('oauth-credentials.json.key', $response->body());
3939
}
4040

41-
private static function validateToken()
41+
private function validateToken()
4242
{
43-
if (! file_exists(self::$path)) {
44-
self::requestToken();
43+
if (! file_exists($this->path)) {
44+
$this->requestToken();
4545
}
4646

47-
$data = self::getCredentialsInfo();
47+
$data = $this->getCredentialsInfo();
4848

4949
$expiredDate = now()->parse(date("Y-m-d H:i:s", $data['expires_in']));
5050

5151
if (now()->lt($expiredDate)) {
52-
self::requestToken();
52+
$this->requestToken();
5353
}
5454
}
5555

56-
private static function getAuthorizationToken()
56+
private function getAuthorizationToken()
5757
{
58-
self::validateToken();
58+
$this->validateToken();
5959

60-
return self::getCredentialsInfo()['access_token'];
60+
return $this->getCredentialsInfo()['access_token'];
6161
}
6262

63-
private static function getOauthAuthorization()
63+
private function getOauthAuthorization()
6464
{
6565
return [
66-
'Authorization' => 'Bearer ' . self::getAuthorizationToken()
66+
'Authorization' => 'Bearer ' . $this->getAuthorizationToken()
6767
];
6868
}
6969

70-
public static function oauthRequest()
70+
public function request()
7171
{
72-
self::$path = storage_path('app/oauth-credentials.json.key');
73-
self::$host = config('oauth-client.host');
74-
self::$clientId = config('oauth-client.client_id');
75-
self::$clientSecret = config('oauth-client.client_secret');
72+
$this->path = storage_path('app/oauth-credentials.json.key');
73+
$this->host = config('oauth-client.host');
74+
$this->clientId = config('oauth-client.client_id');
75+
$this->clientSecret = config('oauth-client.client_secret');
7676

7777
return Http::withHeaders(
78-
self::getOauthAuthorization()
78+
$this->getOauthAuthorization()
7979
);
8080
}
8181
}

src/OAuthServiceProvider.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Diimolabs\Oauth2Client;
3+
namespace Diimolabs\OAuth;
44

5-
use Diimolabs\Oauth2Client\OAuthHttpClient;
5+
use Diimolabs\OAuth\Facades\OAuthClient;
66
use Illuminate\Support\ServiceProvider;
77

88
class OAuthServiceProvider extends ServiceProvider
@@ -16,8 +16,8 @@ public function boot()
1616

1717
public function register()
1818
{
19-
$this->app->singleton(OAuthHttpClient::class, function(){
20-
return new OAuthHttpClient();
19+
$this->app->bind(OAuthClient::class, function(){
20+
return new OAuthClient;
2121
});
2222
}
2323
}

0 commit comments

Comments
 (0)