No description
- PHP 100%
| src | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| composer.json | ||
| composer.lock | ||
| README.md | ||
OneGaming Authorization API
This project is an internal OneGaming library and may only be used for internal projects.
This is a Laravel plugin and can be used in lumen and laravel apps. The following setup explains the installation using lumen. Use this to verify Oauth authorization codes issued by the onegaming-id oauth server.
Setup
- Copy the
oauth-public.keykeyfile in the the storage dir of your project. - Add a mongodb
onegaming_systemconnection in yourdatabase.phpconfig file which connects to theonegaming_systemcollection:
/*
* Example database config file
*/
return [
'default' => env('DB_CONNECTION', 'mongodb'),
'connections' => [
// Add this:
'onegaming_system' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '27017'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => env('DB_AUTH_DATABASE', 'admin')
]
]
],
'migrations' => 'migrations'
];
- Add the Oauth token guard to your
auth.phpconfig:
/*
* Example auth config file
*/
use OneGaming\AuthorizationApi\User;
return [
'defaults' => [
'guard' => 'oauth',
'passwords' => 'users',
],
'guards' => [
'oauth' => [
'driver' => 'oauth',
'provider' => 'users',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => User::class
]
]
];
- Add the
\OneGaming\AuthorizationApi\OauthResourceServerServiceProvider::classclass as service provider to yourapp.php:
$app->register(OneGaming\AuthorizationApi\OauthResourceServerServiceProvider::class);
Usage
Add the oauth guard to your AuthMiddleware.php:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, ...$guards)
{
if ($this->auth->guard('oauth')->check()) {
$this->auth->shouldUse('oauth');
return $next($request);
}
return response()->json(['error' => $request->hasHeader('Authorization')
? 'Invalid or outdated token!'
: 'Missing Authentication via token!'
], 401);
}
If you want permit routes only for requests that has a specific scope,
utilize the \OneGaming\AuthorizationApi\Middleware\CheckScopes
or \OneGaming\AuthorizationApi\Middleware\CheckForAnyScopes middleware:
// bootstrap/app.php
$app->routeMiddleware([
'scopes' => \OneGaming\AuthorizationApi\Middleware\CheckScopes::class,
'scope' => \OneGaming\AuthorizationApi\Middleware\CheckForAnyScope::class
]);
// routes/api.php
// The scopes middleware may be assigned to a route to verify that the incoming request's access token has all of the listed scopes:
$router->group(['middleware' => 'scope:manage_account'], function () use ($router) {
/* ... */
}
// The scope middleware may be assigned to a route to verify that the incoming request's access token has at least one of the listed scopes:
$router->group(['middleware' => 'scope:manage_account'], function () use ($router) {
/* ... */
}