No description
Find a file
2022-04-02 22:48:43 +02:00
src fixed date time usages in jwt token conversion 2021-10-05 15:23:03 +02:00
.gitignore updated open-id-server 2022-04-02 22:48:43 +02:00
.gitlab-ci.yml add composer package deployment 2020-09-29 16:09:28 +02:00
composer.json updated open-id-server 2022-04-02 22:48:43 +02:00
composer.lock updated open-id-server 2022-04-02 22:48:43 +02:00
README.md implemented latest authorization code from onegaming-id 2020-07-11 20:25:02 +02:00

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

  1. Copy the oauth-public.key keyfile in the the storage dir of your project.
  2. Add a mongodb onegaming_system connection in your database.php config file which connects to the onegaming_system collection:
/*
 * 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'

];
  1. Add the Oauth token guard to your auth.php config:
/*
 * 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
        ]
    ]

];
  1. Add the \OneGaming\AuthorizationApi\OauthResourceServerServiceProvider::class class as service provider to your app.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) {
    /* ... */
}