Skip to content

Commit f03cde2

Browse files
authored
[3.x] New library structure & use PSR (#9)
* Bump PHP and Laravel Version * The library structure has been completely updated * Now the library does not use hard dependencies * Added tests * Added static analysis
1 parent 6e163e0 commit f03cde2

24 files changed

+990
-582
lines changed

.github/workflows/psalm.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Psalm Static Analysis
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.php'
7+
- 'psalm.xml'
8+
9+
jobs:
10+
psalm:
11+
name: Psalm
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Code
15+
uses: actions/checkout@v2
16+
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: '8.2'
21+
22+
- name: Install Dependencies
23+
run: |
24+
php -v
25+
composer install --prefer-dist --no-interaction
26+
27+
- name: Run Psalm
28+
run: ./vendor/bin/psalm

.github/workflows/tests.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
name: PHP ${{ matrix.php }}, Laravel ${{ matrix.laravel }}
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
php: [8.1, 8.2]
13+
laravel: [9.*, 10.*]
14+
15+
steps:
16+
- name: Checkout Code
17+
uses: actions/checkout@v2
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php }}
23+
24+
- name: Install Dependencies
25+
run: |
26+
php -v
27+
composer install --prefer-dist --no-interaction
28+
29+
- name: Execute Tests
30+
run: XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover coverage.xml
31+
32+
- name: Upload coverage reports to Codecov
33+
uses: codecov/codecov-action@v3

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
.idea
1+
/.idea/
2+
/composer.lock
3+
/vendor/
4+
/.phpunit.cache/
5+
/.phpunit.result.cache

README.md

+74-71
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Steam Auth for Laravel
22
[![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)
34
[![Total Downloads](https://img.shields.io/packagist/dt/ilzrv/laravel-steam-auth.svg)](https://packagist.org/packages/ilzrv/laravel-steam-auth)
45
[![License](https://img.shields.io/github/license/ilzrv/laravel-steam-auth.svg)](https://packagist.org/packages/ilzrv/laravel-steam-auth)
56

67
Package allows you to implement Steam authentication in your Laravel project.
78

89
## Requirements
9-
* Laravel 7+
10+
* Laravel 9+
11+
* PHP 8.1+
1012

1113
## Installation
1214
#### Install the package
@@ -31,18 +33,34 @@ STEAM_AUTH_API_KEYS=YourSteamApiKey1,YourSteamApiKey2
3133

3234
## Tips
3335

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):
3638

3739
```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
4441

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...
4664
}
4765
```
4866

@@ -52,6 +70,8 @@ If you want to make a proxy domain. Update `redirect_url` inside `steam-auth.php
5270
```php
5371
<?php
5472

73+
declare(strict_types=1);
74+
5575
return [
5676
'redirect_url' => env('APP_ENV', 'production') == 'production'
5777
? 'https://auth.test/#'
@@ -68,93 +88,76 @@ server {
6888
}
6989
```
7090

71-
## Example
91+
## Basic example
7292

7393
In `routes/web.php`:
7494

7595
```php
76-
Route::get('login', [\App\Http\Controllers\Auth\SteamAuthController::class, 'login']);
96+
Route::get('login', \App\Http\Controllers\Auth\SteamAuthController::class);
7797
```
7898

7999
Create a controller `SteamAuthController.php`:
80100

81101
```php
82102
<?php
83103

84-
namespace App\Http\Controllers\Auth;
104+
declare(strict_types=1);
85105

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;
92107

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
94121
{
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+
);
118134

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+
);
128141
}
129142

130-
$data = $this->steamAuth->getUserData();
131-
132-
if (is_null($data)) {
133-
return $this->steamAuth->redirect();
134-
}
143+
$steamUser = $steamAuthenticator->getSteamUser();
135144

136-
Auth::login(
137-
$this->firstOrCreate($data),
145+
$authManager->login(
146+
$this->firstOrCreate($steamUser),
138147
true
139148
);
140149

141-
return redirect($this->redirectTo);
150+
return $redirector->to('/');
142151
}
143152

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
151154
{
152155
return User::firstOrCreate([
153-
'steam_id' => $data->getSteamId(),
156+
'steam_id' => $steamUser->getSteamId(),
154157
], [
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(),
158161
// ...and other what you need
159162
]);
160163
}

UPGRADE.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Upgrade guide
2+
3+
## 2.0 to 3.0
4+
- You should completely redefine the authentication process according to the README (for example your controller)
5+
- The configuration remains the same
6+
- Now the authenticator uses PSR abstractions

composer.json

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
"name": "ilzrv/laravel-steam-auth",
33
"description": "Steam Auth for Laravel",
44
"require": {
5-
"php": "^7.2|^8.0",
6-
"illuminate/http": "^7.0|^8.0|^9.0|^10.0"
5+
"php": "^8.1",
6+
"illuminate/support": "^9.0|^10.0",
7+
"psr/http-message": "^2.0",
8+
"psr/http-client": "^1.0",
9+
"psr/http-factory": "^1.0"
10+
},
11+
"require-dev": {
12+
"roave/security-advisories": "dev-latest",
13+
"phpunit/phpunit": "^10.0",
14+
"orchestra/testbench": "7.x|^8.5",
15+
"vimeo/psalm": "^5.12"
716
},
817
"license": "MIT",
918
"authors": [
@@ -17,6 +26,11 @@
1726
"Ilzrv\\LaravelSteamAuth\\": "src"
1827
}
1928
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Ilzrv\\LaravelSteamAuth\\Tests\\": "tests/"
32+
}
33+
},
2034
"extra": {
2135
"laravel": {
2236
"providers": [

config/steam-auth.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
| This makes an additional request in the Steam API.
2525
|
2626
*/
27-
'getting_level' => true,
27+
'getting_level' => false,
2828

2929
/*
3030
|--------------------------------------------------------------------------

phpunit.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
beStrictAboutOutputDuringTests="true"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
colors="true"
11+
>
12+
<testsuites>
13+
<testsuite name="Tests">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
<exclude>
23+
<file>src/SteamUserDto.php</file>
24+
</exclude>
25+
</source>
26+
27+
<php>
28+
<env name="STEAM_AUTH_API_KEYS" value="api_key_1,api_key_2"/>
29+
</php>
30+
</phpunit>

psalm.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="2"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
findUnusedBaselineEntry="true"
9+
findUnusedCode="false"
10+
>
11+
<projectFiles>
12+
<directory name="src" />
13+
<ignoreFiles>
14+
<directory name="vendor" />
15+
</ignoreFiles>
16+
</projectFiles>
17+
</psalm>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ilzrv\LaravelSteamAuth\Exceptions\Authentication;
6+
7+
use Exception;
8+
9+
abstract class AuthenticationException extends Exception
10+
{
11+
//
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ilzrv\LaravelSteamAuth\Exceptions\Authentication;
6+
7+
final class SteamIdNotFoundAuthenticationException extends AuthenticationException
8+
{
9+
//
10+
}

0 commit comments

Comments
 (0)