MarkV28 / lumenoauth2 by ecmXperts

The EcmXperts LumenOauth2 package
185
0
1
Package Data
Maintainer Username: ecmXperts
Maintainer Contact: m.vels@ecmxperts.nl (Mark Vels)
Package Create Date: 2020-11-23
Package Last Update: 2021-10-13
Language: PHP
License: MIT
Last Refreshed: 2024-04-19 15:16:17
Package Statistics
Total Downloads: 185
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

LumenOauth2

Introduction

Lumen Oauth2 provides a simple API to authenticate your Lumen api with an Openid/Oauth2 server.

Installation

Via composer

Run composer require ecmxperts/lumenoauth2 0.*

Usage

In your .env add the following Environment Variable

IDENTITY_URL=openid/oauth2 server address

In your app\Providers\AuthServiceProvider.php inside the boot method add the following lines

$this->app['auth']->viaRequest('api', function ($request) {
    if (($token = $request->bearerToken()) != null) {
        try {
            return LumenOAuth2::setProviderUrl(env('IDENTITY_URL'))
                ->setAudience('expected audience')
                ->authenticate($token);
        } catch (LumenOauth2Exception $e) {
            Log::error($e, ['exception' => $e]);
        }
    }
});

See Lumen Authentication for the rest on Authentication

The following methods can be used to access the authenticated user properties:

  • The global identifier for the user.
    $userGuid = Auth::user()->guid();

    $userGuid = $request->user()->guid();
  • The firstname for the user.
    $firstname = Auth::user()->firstname();

    $firstname = $request->user()->firstname();
  • The surname for the user.
    $surname = Auth::user()->surname();

    $surname = $request->user()->surname();
  • The fullname for the user.
    $fullname = Auth::user()->fullname();

    $fullname = $request->user()->fullname();
  • The tenant global identifier for the user.
    $tenant = Auth::user()->tenant();

    $tenant = $request->user()->tenant();