PHPackages                             romichoirudin33/sso - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. romichoirudin33/sso

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

romichoirudin33/sso
===================

this packages for OAuth2 for NTB Goverment

v0.1.0(10mo ago)3352MITPHPPHP ^8.0

Since May 25Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/romichoirudin33/sso)[ Packagist](https://packagist.org/packages/romichoirudin33/sso)[ RSS](/packages/romichoirudin33-sso/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

Single Sign On NTBPROV
======================

[](#single-sign-on-ntbprov)

Laravel wrapper OAuth 2 libraries to make support for (NTB Goverments).

Instalation
-----------

[](#instalation)

To get started SSO, use the Composer package manager to add the package to your project's dependencies:

```
composer require romichoirudin33/sso

```

after install composer, please insert `SsoNtbServiceProvider` on `config/app.php`

```
/*
  * Application Service Providers...
*/
Romichoirudin33\Sso\SsoNtbServiceProvider::class,

```

then please insert `config/services.php`

```
'ntbprov' => [
  'client_id' => env('SSO_CLIENT_ID'),
  'client_secret' => env('SSO_CLIENT_SECRET'),
  'redirect' => env('SSO_CLIENT_REDIRECT'),
],

```

and yey, finally you must insert that **SSO\_CLIENT** in `.env` example like this

```
SSO_CLIENT_ID=xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxx
SSO_CLIENT_SECRET=xxxxxxxxxxxxxxxx
SSO_CLIENT_REDIRECT=https://example.ntbprov.go.id/auth/callback

```

For get environment `SSO_CLIENT` you must register your application on .

If you don't have account *DEVELOPER* on , you can send email to  for get more information or contact us on .

Authentication Using Laravel
----------------------------

[](#authentication-using-laravel)

### Routing

[](#routing)

To authenticate users using an OAuth provider, you will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication. The example routes below demonstrate the implementation of both routes:

```
use Romichoirudin33\Sso\Facades\Sso;

Route::get('/auth/redirect', function () {
    return Sso::driver('ntbprov')->redirect();
});

Route::get('/auth/callback', function () {
    $user = Sso::driver('ntbprov')->user();

    // $user->token
});

```

### Authentication &amp; Storage

[](#authentication--storage)

Once the user has been retrieved from the OAuth provider, you may determine if the user exists in your application's database and authenticate the user. If the user does not exist in your application's database, you will typically create a new record in your database to represent the user:

```
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Romichoirudin33\Sso\Facades\Sso;

Route::get('/auth/callback', function () {
    $ssoUser = Sso::driver('ntbprov')->user();

    $user = User::updateOrCreate([
        'email' => $ssoUser->email,
    ], [
        'name' => $ssoUser->name,
    ]);

    Auth::login($user);

    return redirect('/dashboard');
});

```

### User Details

[](#user-details)

After the user is redirected back to your application's authentication callback route, you may retrieve the user's details using Socialite's user method. The user object returned by the user method provides a variety of properties and methods you may use to store information about the user in your own database.

```
use Romichoirudin33\Sso\Facades\Sso;

Route::get('/auth/callback', function () {
    $user = Sso::driver('ntbprov')->user();

    // OAuth 2.0 providers...
    $token = $user->token;
    $refreshToken = $user->refreshToken;
    $expiresIn = $user->expiresIn;

});

```

### User Details (JSON)

[](#user-details-json)

```
{
    "id": 13819,
    "name": "ROMI CHOIRUDIN",
    "email": "romi@ntbprov.go.id",
    "email_verified_at": "2023-12-11T08:13:35.000000Z",
    "nip": "199412032023211011",
    "created_at": "2023-12-11T08:33:16.000000Z",
    "updated_at": "2023-12-11T08:33:16.000000Z"
}

```

Custom Authentication Using Laravel
-----------------------------------

[](#custom-authentication-using-laravel)

Actually the application don't have register feature from user. Because some application have limit user to access. User can access just user that register from administrator. So if your application like this, you must custom authentication, example like this

### Routes

[](#routes)

```
Route::get('auth/redirect', [\App\Http\Controllers\Auth\SingleSignOnController::class, 'redirect'])->name('auth-redirect');
Route::get('auth/callback', [\App\Http\Controllers\Auth\SingleSignOnController::class, 'callback']);

```

### Controllers

[](#controllers)

```
