1
1
# Steam Auth for Laravel
2
2
[ ![ Latest Stable Version] ( https://img.shields.io/packagist/v/ilzrv/laravel-steam-auth.svg )] ( https://packagist.org/packages/ilzrv/laravel-steam-auth )
3
+ [ ![ Codecov] ( https://img.shields.io/codecov/c/github/ilzrv/laravel-steam-auth?token=MIEA87EZGP )] ( https://app.codecov.io/github/ilzrv/laravel-steam-auth )
3
4
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/ilzrv/laravel-steam-auth.svg )] ( https://packagist.org/packages/ilzrv/laravel-steam-auth )
4
5
[ ![ License] ( https://img.shields.io/github/license/ilzrv/laravel-steam-auth.svg )] ( https://packagist.org/packages/ilzrv/laravel-steam-auth )
5
6
6
7
Package allows you to implement Steam authentication in your Laravel project.
7
8
8
9
## Requirements
9
- * Laravel 7+
10
+ * Laravel 9+
11
+ * PHP 8.1+
10
12
11
13
## Installation
12
14
#### Install the package
@@ -31,18 +33,34 @@ STEAM_AUTH_API_KEYS=YourSteamApiKey1,YourSteamApiKey2
31
33
32
34
## Tips
33
35
34
- #### PendingRequest Settings
35
- You can use any of the Laravel PendingRequest settings. Proxies and retries for example
36
+ #### Client Settings
37
+ You can use any settings that your client supports, for example, a [ Guzzle Proxy ] ( https://docs.guzzlephp.org/en/latest/request-options.html#proxy ) :
36
38
37
39
``` php
38
- public function __construct(Request $request)
39
- {
40
- $pendingRequest = \Illuminate\Support\Facades\Http::withOptions([
41
- 'proxy' => 'http://username:password@ip',
42
- 'connect_timeout' => 10,
43
- ])->retry(3);
40
+ <?php
44
41
45
- $this->steamAuth = new SteamAuth($request, $pendingRequest);
42
+ declare(strict_types=1);
43
+
44
+ use GuzzleHttp\Client;
45
+ use GuzzleHttp\Psr7\HttpFactory;
46
+ use Illuminate\Http\Request;
47
+ use Ilzrv\LaravelSteamAuth\SteamAuthenticator;
48
+
49
+ public function __invoke(
50
+ Request $request,
51
+ HttpFactory $httpFactory,
52
+ ): RedirectResponse {
53
+ $client = new Client([
54
+ 'proxy' => 'socks5://user:password@192.168.1.1:1080',
55
+ ]);
56
+
57
+ $steamAuthenticator = new SteamAuthenticator(
58
+ new Uri($request->getUri()),
59
+ $client,
60
+ $httpFactory,
61
+ );
62
+
63
+ // Continuation of your code...
46
64
}
47
65
```
48
66
@@ -52,6 +70,8 @@ If you want to make a proxy domain. Update `redirect_url` inside `steam-auth.php
52
70
``` php
53
71
<?php
54
72
73
+ declare(strict_types=1);
74
+
55
75
return [
56
76
'redirect_url' => env('APP_ENV', 'production') == 'production'
57
77
? 'https://auth.test/#'
@@ -68,93 +88,76 @@ server {
68
88
}
69
89
```
70
90
71
- ## Example
91
+ ## Basic example
72
92
73
93
In ` routes/web.php ` :
74
94
75
95
``` php
76
- Route::get('login', [ \App\Http\Controllers\Auth\SteamAuthController::class, 'login'] );
96
+ Route::get('login', \App\Http\Controllers\Auth\SteamAuthController::class);
77
97
```
78
98
79
99
Create a controller ` SteamAuthController.php ` :
80
100
81
101
``` php
82
102
<?php
83
103
84
- namespace App\Http\Controllers\Auth ;
104
+ declare(strict_types=1) ;
85
105
86
- use App\Http\Controllers\Controller;
87
- use App\Providers\RouteServiceProvider;
88
- use App\User;
89
- use Illuminate\Support\Facades\Auth;
90
- use Ilzrv\LaravelSteamAuth\SteamAuth;
91
- use Ilzrv\LaravelSteamAuth\SteamData;
106
+ namespace App\Http\Controllers\Auth;
92
107
93
- class SteamAuthController extends Controller
108
+ use App\Models\User;
109
+ use GuzzleHttp\Client;
110
+ use GuzzleHttp\Psr7\HttpFactory;
111
+ use GuzzleHttp\Psr7\Uri;
112
+ use Illuminate\Auth\AuthManager;
113
+ use Illuminate\Http\RedirectResponse;
114
+ use Illuminate\Http\Request;
115
+ use Illuminate\Routing\Redirector;
116
+ use Ilzrv\LaravelSteamAuth\Exceptions\Validation\ValidationException;
117
+ use Ilzrv\LaravelSteamAuth\SteamAuthenticator;
118
+ use Ilzrv\LaravelSteamAuth\SteamUserDto;
119
+
120
+ final class SteamAuthController
94
121
{
95
- /**
96
- * The SteamAuth instance.
97
- *
98
- * @var SteamAuth
99
- */
100
- protected $steamAuth;
101
-
102
- /**
103
- * Where to redirect users after login.
104
- *
105
- * @var string
106
- */
107
- protected $redirectTo = RouteServiceProvider::HOME;
108
-
109
- /**
110
- * SteamAuthController constructor.
111
- *
112
- * @param SteamAuth $steamAuth
113
- */
114
- public function __construct(SteamAuth $steamAuth)
115
- {
116
- $this->steamAuth = $steamAuth;
117
- }
122
+ public function __invoke(
123
+ Request $request,
124
+ Redirector $redirector,
125
+ Client $client,
126
+ HttpFactory $httpFactory,
127
+ AuthManager $authManager,
128
+ ): RedirectResponse {
129
+ $steamAuthenticator = new SteamAuthenticator(
130
+ new Uri($request->getUri()),
131
+ $client,
132
+ $httpFactory,
133
+ );
118
134
119
- /**
120
- * Get user data and login
121
- *
122
- * @return \Illuminate\Http\RedirectResponse
123
- */
124
- public function login()
125
- {
126
- if (!$this->steamAuth->validate()) {
127
- return $this->steamAuth->redirect();
135
+ try {
136
+ $steamAuthenticator->auth();
137
+ } catch (ValidationException) {
138
+ return $redirector->to(
139
+ $steamAuthenticator->buildAuthUrl()
140
+ );
128
141
}
129
142
130
- $data = $this->steamAuth->getUserData();
131
-
132
- if (is_null($data)) {
133
- return $this->steamAuth->redirect();
134
- }
143
+ $steamUser = $steamAuthenticator->getSteamUser();
135
144
136
- Auth:: login(
137
- $this->firstOrCreate($data ),
145
+ $authManager-> login(
146
+ $this->firstOrCreate($steamUser ),
138
147
true
139
148
);
140
149
141
- return redirect($this->redirectTo );
150
+ return $redirector->to('/' );
142
151
}
143
152
144
- /**
145
- * Get the first user by SteamID or create new
146
- *
147
- * @param SteamData $data
148
- * @return User|\Illuminate\Database\Eloquent\Model
149
- */
150
- protected function firstOrCreate(SteamData $data)
153
+ private function firstOrCreate(SteamUserDto $steamUser): User
151
154
{
152
155
return User::firstOrCreate([
153
- 'steam_id' => $data ->getSteamId(),
156
+ 'steam_id' => $steamUser ->getSteamId(),
154
157
], [
155
- 'name' => $data ->getPersonaName(),
156
- 'avatar' => $data ->getAvatarFull(),
157
- 'player_level' => $data ->getPlayerLevel(),
158
+ 'name' => $steamUser ->getPersonaName(),
159
+ 'avatar' => $steamUser ->getAvatarFull(),
160
+ 'player_level' => $steamUser ->getPlayerLevel(),
158
161
// ...and other what you need
159
162
]);
160
163
}
0 commit comments