Skip to content

Commit

Permalink
migration, route, controller trait and some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
meachel committed Jan 22, 2020
1 parent 47e2cf4 commit 4fb2871
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ GATE_REDIRECT_URL="http://localhost/auth/callback"
GATE_AUTHORIZE_URL="http://localhost:8000/oauth/authorize"
GATE_TOKEN_URL="http://localhost:8000/oauth/token"
GATE_USER_URL="http://localhost:8000/api/user"

GATE_AUTH_CALLBACK_URI="auth/callback"
GATE_AUTH_CALLBACK_CONTROLLER="App\\Http\\Controllers\\Auth\\LoginController"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,39 @@ composer require looxis/gate
## Usage


To register your provider, add it to the array into `config/app.php` file:
```php
'providers' => [
// Other Service Providers

\Looxis\Gate\GateServiceProvider::class
],
```


Add some properties to your `.env` file (see .env.example)
```php
```dotenv
GATE_ENABLED=true
GATE_URL="https://gate.example.com/"
GATE_CLIENT_ID=3
GATE_CLIENT_SECRET=client_secret
```

Publish and Run migration
```shell script
php artisan vendor:publish --tag=gate-migrations
php artisan migrate
```

Add `gate_id` and `api_token` to fillable array in User model.

Also you can change auth callback route and controller in config or in `.env` file
```dotenv
GATE_AUTH_CALLBACK_URI="auth/callback"
GATE_AUTH_CALLBACK_CONTROLLER="App\\Http\\Controllers\\Auth\\LoginController"
```

Change `use AuthenticatesUsers` statement in `App\Http\Controllers\Auth\LoginController` or any other login controller you want to use with Looxis Gate.
```php
use AuthenticatesUsers, LoginControllerTrait {
LoginControllerTrait::loggedOut insteadof AuthenticatesUsers;
LoginControllerTrait::showLoginForm insteadof AuthenticatesUsers;
}
```
`loggedOut` should be called from `logout` method of this controller (if you override it).

Also you can publish the config file with this artisan command:
``` php
```shell script
php artisan vendor:publish --tag=gate-config
```

Expand All @@ -62,6 +75,7 @@ If you discover any security related issues, please email igortsapiro@gmail.com
## Credits

- [Igor Tsapiro](https://github.com/looxis)
- [Mike Cholovskiy](https://github.com/meachel)
- [All Contributors](../../contributors)

## License
Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
"name": "Igor Tsapiro",
"email": "dev@looxis.com",
"role": "Developer"
},
{
"name": "Mike Cholovskiy",
"email": "dev@looxis.com",
"role": "Developer"
},
{
"name": "Jannik Malken",
"email": "dev@looxis.com",
"role": "Developer"
}
],
"require": {
Expand Down Expand Up @@ -48,7 +58,7 @@
"Looxis\\Gate\\GateServiceProvider"
],
"aliases": {
"Gate": "Looxis\\Gate\\GateFacade"
"LooxisGate": "Looxis\\Gate\\GateFacade"
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions config/gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* You can place your custom package configuration in here.
*/
return [
'enabled' => env('GATE_ENABLED', true),
'url' => env('GATE_URL'),
'client_id' => env('GATE_CLIENT_ID'),
'client_secret' => env('GATE_CLIENT_SECRET'),
'redirect' => env('GATE_REDIRECT_URL', env('APP_URL') . '/auth/callback'),
'authorize_url' => env('GATE_AUTHORIZE_URL', env('GATE_URL') . '/oauth/authorize'),
'token_url' => env('GATE_TOKEN_URL', env('GATE_URL') . '/oauth/token'),
'user_url' => env('GATE_USER_URL', env('GATE_URL') . '/api/user'),
'enabled' => env('GATE_ENABLED', true),
'url' => env('GATE_URL'),
'client_id' => env('GATE_CLIENT_ID'),
'client_secret' => env('GATE_CLIENT_SECRET'),
'redirect' => env('GATE_REDIRECT_URL', env('APP_URL') . '/auth/callback'),
'authorize_url' => env('GATE_AUTHORIZE_URL', env('GATE_URL') . '/oauth/authorize'),
'token_url' => env('GATE_TOKEN_URL', env('GATE_URL') . '/oauth/token'),
'user_url' => env('GATE_USER_URL', env('GATE_URL') . '/api/user'),
'auth_callback_uri' => env('GATE_AUTH_CALLBACK_URI', 'auth/callback'),
'auth_callback_controller' => env('GATE_AUTH_CALLBACK_CONTROLLER', 'App\Http\Controllers\Auth\LoginController'),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddGateIdColumnToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->unsignedBigInteger('gate_id')->unique()->nullable();
});

}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
if (DB::getDriverName() !== 'sqlite') {
$table->dropUnique(['gate_id']);
}
$table->dropColumn('gate_id');
});
}
}
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Illuminate\Support\Facades\Route;

Route::get(config('gate.auth_callback_uri', 'auth/callback'), config('gate.auth_callback_controller', 'App\Http\Controllers\Auth\LoginController') . '@handleProviderCallback');
2 changes: 1 addition & 1 deletion src/GateFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class GateFacade extends Facade
*/
protected static function getFacadeAccessor()
{
return 'gate';
return 'LooxisGate';
}
}
24 changes: 20 additions & 4 deletions src/GateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Looxis\Gate;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Laravel\Socialite\Contracts\Factory;
use Laravel\Socialite\Facades\Socialite;
Expand All @@ -19,14 +20,18 @@ public function boot()
*/
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'gate');
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'gate');
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// $this->loadRoutesFrom(__DIR__.'/routes.php');
//$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
//$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
$this->registerRoutes();

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/gate.php' => config_path('gate.php'),
], 'gate-config');

$timestamp = date('Y_m_d_His', time());
$this->publishes([
__DIR__.'/../database/migrations/2018_02_21_230848_add_gate_id_column_to_users_table.php' => database_path('migrations/'.$timestamp.'_add_gate_id_column_to_users_table.php'),
], 'gate-migrations');

// Publishing the views.
/*$this->publishes([
Expand All @@ -48,6 +53,17 @@ public function boot()
}
}

/**
* Register the Horizon routes.
*
* @return void
*/
protected function registerRoutes()
{
Route::middleware('web')
->group(__DIR__ . '/../routes/web.php');
}

/**
* Register the application services.
*/
Expand All @@ -57,7 +73,7 @@ public function register()
$this->mergeConfigFrom(__DIR__.'/../config/gate.php', 'gate');
// Register the main class to use with the facade

$this->app->bind('gate', function () {
$this->app->bind('LooxisGate', function () {
return Socialite::driver('gate');
});

Expand Down
99 changes: 99 additions & 0 deletions src/LoginControllerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php


namespace Looxis\Gate;

use Auth;
use Redirect;
use Exception;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;

trait LoginControllerTrait
{
protected $redirectAfterLogout = '/login';

public function showLoginForm()
{
if (\LooxisGate::enabled() && !request()->exists('local')) {
return $this->redirectToProvider();
}
return view('auth.login');
}

public function loggedOut(Request $request)
{
if (\LooxisGate::enabled()) {
return redirect($this->gate()->getLogoutUrl());
}

return redirect('/');
}


public function gate()
{
return Socialite::driver('gate');
}


/**
* Redirect the user to the auth provider's authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
return $this->gate()->redirect();
}

/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback()
{
try {
$user = $this->gate()->user();
} catch (Exception $e) {
return Redirect::to('auth');
}

$authUser = $this->findOrCreateUser($user);

Auth::login($authUser, true);

return Redirect::to($this->redirectTo);
}

/**
* Return user if exists; create and return if doesn't
*
* @param $gateUser
* @return User
*/
private function findOrCreateUser($gateUser)
{
if ($authUser = User::where('gate_id', $gateUser->id)->first()) {
return $authUser;
}
if ($authUser = User::where('email', $gateUser->email)->first()) {
$authUser->update([
'gate_id' => $gateUser->id
]);
return $authUser;
}

return User::create([
'name' => $gateUser->name,
'email' => $gateUser->email,
'gate_id' => $gateUser->id,
'api_token' => Str::random(60),
'password' => '',
]);
}
}

0 comments on commit 4fb2871

Please sign in to comment.