1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Providers;
7
8use App\Http\Controllers\Passport\AuthorizationController;
9use App\Models\OAuth\Client;
10use App\Models\OAuth\Token;
11use Auth;
12use Carbon\Carbon;
13use Illuminate\Contracts\Auth\StatefulGuard;
14use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
15use Laravel\Passport\Http\Controllers\AccessTokenController;
16use Laravel\Passport\Http\Controllers\ApproveAuthorizationController;
17use Laravel\Passport\Http\Controllers\DenyAuthorizationController;
18use Laravel\Passport\Passport;
19use Route;
20
21class AuthServiceProvider extends ServiceProvider
22{
23 /**
24 * Register any authentication / authorization services.
25 *
26 * @return void
27 */
28 public function register()
29 {
30 Passport::ignoreMigrations();
31 Passport::ignoreRoutes();
32
33 // Copied from PassportServiceProvider with the correct
34 // AuthorizationController class.
35 $this->app->when(AuthorizationController::class)
36 ->needs(StatefulGuard::class)
37 ->give(fn () => Auth::guard($GLOBALS['cfg']['passport']['guard']));
38 }
39
40 public function boot()
41 {
42 Passport::tokensExpireIn(Carbon::now()->addDays(1));
43 Passport::refreshTokensExpireIn(Carbon::now()->addMonths(3));
44
45 Passport::useTokenModel(Token::class);
46 Passport::useClientModel(Client::class);
47
48 if ($path = $GLOBALS['cfg']['services']['passport']['path']) {
49 Passport::keyPath($path);
50 }
51
52 // Override/selectively pick routes.
53 // RouteServiceProvider current runs before our provider, so Passport's default routes will override
54 // those set in routes/web.php.
55 Route::group(['prefix' => 'oauth', 'as' => 'oauth.'], function () {
56 Route::post('token', AccessTokenController::class.'@issueToken')->middleware('throttle')->name('passport.token');
57 Route::get('authorize', AuthorizationController::class.'@authorize')
58 ->middleware(['web', 'verify-user'])
59 ->name('authorizations.authorize');
60
61 Route::post('authorize', ApproveAuthorizationController::class.'@approve')
62 ->middleware(['web', 'auth']);
63
64 Route::delete('authorize', DenyAuthorizationController::class.'@deny')
65 ->middleware(['web', 'auth']);
66 });
67
68 Passport::tokensCan([
69 'delegate' => '',
70 'forum.write' => osu_trans('api.scopes.forum.write'),
71 'chat.read' => osu_trans('api.scopes.chat.read'),
72 'chat.write' => osu_trans('api.scopes.chat.write'),
73 'chat.write_manage' => osu_trans('api.scopes.chat.write_manage'),
74 'friends.read' => osu_trans('api.scopes.friends.read'),
75 'identify' => osu_trans('api.scopes.identify'),
76 'public' => osu_trans('api.scopes.public'),
77 ]);
78 }
79}