Skip to content

Commit 2f5a679

Browse files
Merge pull [zefy#18](zefy#18)
2 parents b2226e5 + 2e30130 commit 2f5a679

File tree

7 files changed

+127
-6
lines changed

7 files changed

+127
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22
composer.lock
3-
vendor
3+
vendor
4+
.history/

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<p align="center"><img src="https://laravel.com/assets/img/components/logo-laravel.svg"></p>
1212

13+
This Package Fork from https://github.com/zefy/laravel-sso to modify some change
1314

1415
This package based on [Simple PHP SSO skeleton](https://github.com/zefy/php-simple-sso) package and made suitable for Laravel framework.
1516
### Requirements
@@ -29,7 +30,7 @@ Client visits Broker and unique token is generated. When new token is generated
2930
### Server
3031
Install this package using composer.
3132
```shell
32-
$ composer require zefy/laravel-sso
33+
$ composer require kcpal/laravel-sso
3334
```
3435

3536

@@ -61,6 +62,22 @@ You can create new broker using following Artisan CLI command:
6162
$ php artisan sso:broker:create {name}
6263
```
6364

65+
After that in user model you can add the relation for broker as below:
66+
```php
67+
public function broker()
68+
{
69+
return $this->belongsToMany(Broker::class)
70+
->where('deleted_at', NULL)
71+
->withTimestamps();
72+
}
73+
```
74+
75+
Now, you can register or create users using basic Laravel functionality and then set the brokers of that user as below:
76+
```php
77+
$user->broker()->sync($brokers)
78+
```
79+
Here, `$user` is the object of `User` Model and `$brokers` is the array of ids of broker.
80+
6481
----------
6582

6683
### Broker
@@ -142,4 +159,4 @@ Example `.env` options:
142159
SSO_SERVER_URL=https://server.test
143160
SSO_BROKER_NAME=site1
144161
SSO_BROKER_SECRET=892asjdajsdksja74jh38kljk2929023
145-
```
162+
```

config/laravel-sso.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525

2626
'usersModel' => \App\User::class,
2727
'brokersModel' => Zefy\LaravelSSO\Models\Broker::class,
28+
'brokersUserModel' => Zefy\LaravelSSO\Models\BrokerUser::class,
2829

2930
// Table used in Zefy\LaravelSSO\Models\Broker model
3031
'brokersTable' => 'brokers',
32+
'brokerUserTable' => 'broker_user',
3133

3234
// Logged in user fields sent to brokers.
3335
'userFields' => [
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateBrokerUserTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create(config('laravel-sso.brokerUserTable', 'broker_user'), function (Blueprint $table) {
17+
$table->increments('id');
18+
19+
$table->integer('user_id')->foreign('user_id')->references('id')->on('users');
20+
$table->integer('broker_id')->foreign('broker_id')->references('id')->on('brokers');
21+
$table->timestamps();
22+
$table->softDeletes();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists(config('laravel-sso.brokerUserTable', 'broker_user'));
34+
}
35+
}

src/Controllers/ServerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ public function logout(LaravelSSOServer $server)
5454
*/
5555
public function userInfo(LaravelSSOServer $server)
5656
{
57-
return $server->userInfo();
57+
return $server->checkUserApplicationAuth();
5858
}
5959
}

src/LaravelSSOServer.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Session;
99
use Zefy\SimpleSSO\SSOServer;
1010
use Zefy\LaravelSSO\Resources\UserResource;
11+
use Zefy\SimpleSSO\Exceptions\SSOServerException;
1112

1213
class LaravelSSOServer extends SSOServer
1314
{
@@ -66,7 +67,7 @@ protected function returnJson(?array $response = null, int $httpResponseCode = 2
6667
*/
6768
protected function authenticate(string $username, string $password)
6869
{
69-
if (!Auth::attempt(['username' => $username, 'password' => $password])) {
70+
if (!Auth::attempt(['email' => $username, 'password' => $password])) {
7071
return false;
7172
}
7273

@@ -97,6 +98,51 @@ protected function getBrokerInfo(string $brokerId)
9798
return $broker;
9899
}
99100

101+
/**
102+
* Check for User Auth with Broker Application.
103+
*
104+
* @return boolean
105+
*/
106+
protected function checkBrokerUserAuthentication()
107+
{
108+
$userInfo = $this->userInfo();
109+
$broker = $this->getBrokerDetail();
110+
if(!empty($userInfo->id) && !empty($broker)) {
111+
$brokerUser = config('laravel-sso.brokersUserModel')::where('user_id', $userInfo->id)->where('broker_id', $broker->id)->first();
112+
if(empty($brokerUser)) {
113+
return false;
114+
}
115+
}
116+
return true;
117+
}
118+
119+
/**
120+
* Check for the User authorization with application and return error or userinfo
121+
*
122+
* @return string
123+
*/
124+
public function checkUserApplicationAuth()
125+
{
126+
try {
127+
if(empty($this->checkBrokerUserAuthentication())) {
128+
$this->fail('User authorization failed with application.');
129+
}
130+
} catch (SSOServerException $e) {
131+
return $this->returnJson(['error' => $e->getMessage()]);
132+
}
133+
return $this->userInfo();
134+
}
135+
136+
/**
137+
* Returning the broker details
138+
*
139+
* @return string
140+
*/
141+
public function getBrokerDetail()
142+
{
143+
return $this->getBrokerInfo($this->brokerId);
144+
}
145+
100146
/**
101147
* Get the information about a user
102148
*
@@ -107,7 +153,7 @@ protected function getBrokerInfo(string $brokerId)
107153
protected function getUserInfo(string $username)
108154
{
109155
try {
110-
$user = config('laravel-sso.usersModel')::where('username', $username)->firstOrFail();
156+
$user = config('laravel-sso.usersModel')::where('email', $username)->firstOrFail();
111157
} catch (ModelNotFoundException $e) {
112158
return null;
113159
}

src/Models/BrokerUser.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Zefy\LaravelSSO\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class BrokerUser extends Model
9+
{
10+
use SoftDeletes;
11+
/**
12+
* Get the table associated with the model.
13+
*
14+
* @return string
15+
*/
16+
public function getTable()
17+
{
18+
return config('laravel-sso.brokerUserTable', 'broker_user');
19+
}
20+
}

0 commit comments

Comments
 (0)