From 2095c18871d22647ff025a002cc0e29792275d38 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Sun, 22 Aug 2021 13:55:51 +0800 Subject: [PATCH 1/9] cross login --- .../forms/CrossPlatformLoginFormComponent.vue | 48 +++++++++++++++++++ .../views/pages/accounts/login.blade.php | 1 + 2 files changed, 49 insertions(+) create mode 100644 resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue diff --git a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue new file mode 100644 index 00000000..93634ed4 --- /dev/null +++ b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue @@ -0,0 +1,48 @@ + + \ No newline at end of file diff --git a/resources/views/pages/accounts/login.blade.php b/resources/views/pages/accounts/login.blade.php index 789d3a68..863e7523 100644 --- a/resources/views/pages/accounts/login.blade.php +++ b/resources/views/pages/accounts/login.blade.php @@ -26,6 +26,7 @@
+
From 2d49f4aabf8dcf180b4197fc3ad3e30e2a81d1cf Mon Sep 17 00:00:00 2001 From: glovetleong Date: Wed, 25 Aug 2021 10:01:32 +0800 Subject: [PATCH 2/9] cross login --- .../accounts/forms/CrossPlatformLoginFormComponent.vue | 4 ++-- routes/web.php | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue index 93634ed4..6cd0b199 100644 --- a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue +++ b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue @@ -12,7 +12,7 @@
- +
@@ -36,7 +36,7 @@ }, methods:{ iframeEvent(event) { - if(event.origin !== 'http://unity.test') return; + if(event.origin !== 'http://exchange-2.0.test') return; console.log(event.data); } }, diff --git a/routes/web.php b/routes/web.php index da4792e8..6781c3f3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -64,6 +64,4 @@ Route::get('/customer/{marking}', function ($marking) { return view('pages.customers.profile', ['id' => $id]); })->name('customer.profile'); -Route::get('/order/{id}/download', 'Orders\DownloadOrderQrPdfController@download')->name('order.qr.download'); - -Route::get('/test', 'TestController@test')->name('test'); \ No newline at end of file +Route::get('/order/{id}/download', 'Orders\DownloadOrderQrPdfController@download')->name('order.qr.download'); \ No newline at end of file From 0554cc895f29d33b32c2a1990a39bf14040e6101 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Sun, 5 Sep 2021 12:55:11 +0800 Subject: [PATCH 3/9] checkout --- .../CrossAuthenticateUserLogic.php | 44 ++++++++++++ .../CrossAuthenticationCredentialsObject.php | 43 ++++++++++++ .../CrossAuthenticationProcessor.php | 70 +++++++++++++++++++ .../Services/CrossAuthenticatesUser.php | 44 ++++++++++++ .../Rules/CanCrossAuthenticateUser.php | 54 ++++++++++++++ .../UserCrossAuthenticationValidation.php | 43 ++++++++++++ .../ValueObjects/Constants/PlatformType.php | 9 +++ .../UserCrossAuthenticationController.php | 21 ++++++ app/Models/UserSocialAccount.php | 11 +++ ...2235_create_user_social_accounts_table.php | 35 ++++++++++ database/seeders/DatabaseSeeder.php | 4 +- .../forms/CrossPlatformLoginFormComponent.vue | 5 +- routes/account.php | 5 ++ 13 files changed, 385 insertions(+), 3 deletions(-) create mode 100644 app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php create mode 100644 app/Classes/Modules/Accounts/DataTransferObjects/CrossAuthenticationCredentialsObject.php create mode 100644 app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php create mode 100644 app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php create mode 100644 app/Classes/Modules/Accounts/Standards/Rules/CanCrossAuthenticateUser.php create mode 100644 app/Classes/Modules/Accounts/Standards/Validators/UserCrossAuthenticationValidation.php create mode 100644 app/Classes/ValueObjects/Constants/PlatformType.php create mode 100644 app/Http/Controllers/Accounts/UserCrossAuthenticationController.php create mode 100644 app/Models/UserSocialAccount.php create mode 100644 database/migrations/2021_09_01_212235_create_user_social_accounts_table.php diff --git a/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php b/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php new file mode 100644 index 00000000..5819a4f0 --- /dev/null +++ b/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php @@ -0,0 +1,44 @@ + 'Cross Authentication', + 'message' => 'You have successfully logged in to your account' + ]; + } + + /** @var CrossAuthenticationProcessor */ + private $cressAuthenticationProcessor; + + + /** + * CrossAuthenticateUserLogic constructor. + * @param CrossAuthenticationProcessor $authenticationProcessor + */ + public function __construct(CrossAuthenticationProcessor $cressAuthenticationProcessor) + { + $this->cressAuthenticationProcessor = $cressAuthenticationProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\AccessUnauthorisedException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function logic(Request $request): JsonResponse { + return $this->response($this->cressAuthenticationProcessor->execute($request)); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/DataTransferObjects/CrossAuthenticationCredentialsObject.php b/app/Classes/Modules/Accounts/DataTransferObjects/CrossAuthenticationCredentialsObject.php new file mode 100644 index 00000000..67abf905 --- /dev/null +++ b/app/Classes/Modules/Accounts/DataTransferObjects/CrossAuthenticationCredentialsObject.php @@ -0,0 +1,43 @@ +access_token = $access_token; + $this->platform = $platform; + } + + /** + * @return string + */ + public function getAccessToken(): string + { + return $this->access_token; + } + + /** + * @return string + */ + public function getPlatform(): int + { + return $this->platform; + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php b/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php new file mode 100644 index 00000000..1f976a42 --- /dev/null +++ b/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php @@ -0,0 +1,70 @@ +canCrossAuthenticateUser = $canCrossAuthenticateUser; + $this->crossAuthenticatesUser = $crossAuthenticatesUser; + $this->generatesAuthenticationToken = $generatesAuthenticationToken; + $this->fetchesUser = $fetchesUser; + $this->authenticationRedirect = $authenticationRedirect; + } + + + /** + * @param Request $request + * @return array + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\AccessUnauthorisedException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function execute(Request $request): array { + + $object = new CrossAuthenticationCredentialsObject($request->input('access_token'), $request->input('platform')); + + $this->canCrossAuthenticateUser->passes($object); + + $this->crossAuthenticatesUser->execute($object); + + $user = $this->fetchesUser->execute(['email' => $object->getEmail()]); + + return ['access_token' => $this->generatesAuthenticationToken->execute($user), 'redirect_url' => $this->authenticationRedirect->url($user)]; + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php new file mode 100644 index 00000000..e76b9197 --- /dev/null +++ b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php @@ -0,0 +1,44 @@ +getPlatform() == PlatformType::EXCHANGE) { + + // $headers = [ + // 'Content-Type' => 'application/json', + // 'Authorization' => 'Bearer ' . $object->getAccessToken(), + // ]; + // $client = new \GuzzleHttp\Client(['headers' => $headers]); + + // $response = $client->get('http://exchange-2.0.test/api/v1/account/user/list'); + + $response = Http::withToken($object->getAccessToken())->get('http://shipping-portal.test/api/v1/account/user/list')->object(); + dd($response); + + } + + if (! auth()->validate(['email' => $object->getEmail(), 'password' => $object->getPassword()])) { + throw new AccessUnauthorisedException('These credentials do not match our records.'); + } + + return true; + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Standards/Rules/CanCrossAuthenticateUser.php b/app/Classes/Modules/Accounts/Standards/Rules/CanCrossAuthenticateUser.php new file mode 100644 index 00000000..115a7ea6 --- /dev/null +++ b/app/Classes/Modules/Accounts/Standards/Rules/CanCrossAuthenticateUser.php @@ -0,0 +1,54 @@ +userCrossAuthenticationValidation = $userCrossAuthenticationValidation; + } + + /** + * @return bool + */ + protected function authorized(): bool + { + return true; + + } + + + /** + * @param AuthenticationCredentialsObject $object + * @return bool + * @throws RequestValidationException + */ + protected function validators($object): bool + { + return $this->userCrossAuthenticationValidation->validate($object); + } + + /** + * @param AuthenticationCredentialsObject $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Standards/Validators/UserCrossAuthenticationValidation.php b/app/Classes/Modules/Accounts/Standards/Validators/UserCrossAuthenticationValidation.php new file mode 100644 index 00000000..ec170dc7 --- /dev/null +++ b/app/Classes/Modules/Accounts/Standards/Validators/UserCrossAuthenticationValidation.php @@ -0,0 +1,43 @@ + $object->getAccesstoken(), + 'platform' => $object->getPlatform() + ]; + } + + /** + * @return array + */ + protected function rules(): array { + return [ + 'access_token' => 'required', + 'platform' => 'required' + ]; + } + + /** + * @return array + */ + protected function messages(): array { + return []; + } + + + + +} \ No newline at end of file diff --git a/app/Classes/ValueObjects/Constants/PlatformType.php b/app/Classes/ValueObjects/Constants/PlatformType.php new file mode 100644 index 00000000..d9ddd0e3 --- /dev/null +++ b/app/Classes/ValueObjects/Constants/PlatformType.php @@ -0,0 +1,9 @@ +execute($request); + } + +} diff --git a/app/Models/UserSocialAccount.php b/app/Models/UserSocialAccount.php new file mode 100644 index 00000000..b8f01866 --- /dev/null +++ b/app/Models/UserSocialAccount.php @@ -0,0 +1,11 @@ +id(); + $table->foreignId('user_id'); + $table->string('app_id')->nullable(); + $table->integer('platform')->default(0); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_social_accounts'); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index bba317e6..983c7aad 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -23,7 +23,7 @@ class DatabaseSeeder extends Seeder $this->call(CountriesTableSeeder::class); $this->call(CurrenciesTableSeeder::class); - $this->call(CompaniesTableSeeder::class); - $this->call(OrdersTableSeeder::class); + // $this->call(CompaniesTableSeeder::class); + // $this->call(OrdersTableSeeder::class); } } \ No newline at end of file diff --git a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue index 6cd0b199..6b44f6ba 100644 --- a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue +++ b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue @@ -37,7 +37,10 @@ methods:{ iframeEvent(event) { if(event.origin !== 'http://exchange-2.0.test') return; - console.log(event.data); + if (event.data.data) { + console.log(event.data.data.payload.access_token); + + } } }, mounted() { diff --git a/routes/account.php b/routes/account.php index 7a9e08df..cd4e33ae 100644 --- a/routes/account.php +++ b/routes/account.php @@ -6,9 +6,14 @@ Route::group(['prefix' => 'account', 'namespace' => 'Accounts', 'as' => 'account Route::post('/registration', 'CreateCustomerController@create')->name('registration.register'); + + Route::group(['prefix' => 'authentication', 'as' => 'authentication.'], function () { Route::group(['prefix' => 'login', 'as' => 'authenticate.'], function () { Route::post('/attempt', 'UserAuthenticationController@authenticate')->name('attempt'); + + Route::post('/cross-attempt', 'UserCrossAuthenticationController@authenticate')->name('cross.attempt'); + Route::post('/check_email', 'CheckEmailController@check')->name('email.check'); }); From f3854078042bfa925260babda0c5c9e6570cf4d4 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Tue, 28 Sep 2021 12:07:05 +0800 Subject: [PATCH 4/9] debug --- .../Accounts/Services/CrossAuthenticatesUser.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php index e76b9197..acf1f15e 100644 --- a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php +++ b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Http; use App\Classes\Exceptions\AccessUnauthorisedException; use App\Classes\Modules\Accounts\DataTransferObjects\CrossAuthenticationCredentialsObject; use App\Classes\ValueObjects\Constants\PlatformType; + use Tymon\JWTAuth\Facades\JWTAuth; class CrossAuthenticatesUser { @@ -20,16 +21,12 @@ class CrossAuthenticatesUser if ($object->getPlatform() == PlatformType::EXCHANGE) { - // $headers = [ - // 'Content-Type' => 'application/json', - // 'Authorization' => 'Bearer ' . $object->getAccessToken(), - // ]; - // $client = new \GuzzleHttp\Client(['headers' => $headers]); - // $response = $client->get('http://exchange-2.0.test/api/v1/account/user/list'); - $response = Http::withToken($object->getAccessToken())->get('http://shipping-portal.test/api/v1/account/user/list')->object(); - dd($response); + $response = Http::withToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leGNoYW5nZS0yLjAudGVzdFwvYXBpXC92MVwvYWNjb3VudFwvYXV0aGVudGljYXRpb25cL2xvZ2luXC9hdHRlbXB0IiwiaWF0IjoxNjMyODAwMzE1LCJleHAiOjk5NjA2MDAzMjE1LCJuYmYiOjE2MzI4MDAzMTUsImp0aSI6Ilo2cGpNMExRbWM5Q1BQQ0YiLCJ1c2VyIjp7ImlkIjo3LCJuYW1lIjoiTGVvbmcgQ2hlZSBTaW9uZyIsImVtYWlsIjoiZ2xvdmV0bGVvbmdAZ21haWwuY29tIiwidHlwZSI6Mywic3RhdHVzIjoyLCJjb21wYW55X2lkIjo5fSwic3ViIjo3LCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.j_Zr9r6W7gE7zM66NQQk9eHbzHPIz-BYbsVOE7ARukI')->get('http://exchange-2.0.test/api/v1/account/user/list')->object(); + + + dd($response, $object->getAccessToken()); } From 5d34201579db23cc54b0d2477c4cf230e2a16588 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Tue, 28 Sep 2021 22:07:19 +0800 Subject: [PATCH 5/9] pull --- .../Services/CrossAuthenticatesUser.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php index acf1f15e..e80bef3d 100644 --- a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php +++ b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php @@ -6,7 +6,6 @@ use Illuminate\Support\Facades\Http; use App\Classes\Exceptions\AccessUnauthorisedException; use App\Classes\Modules\Accounts\DataTransferObjects\CrossAuthenticationCredentialsObject; use App\Classes\ValueObjects\Constants\PlatformType; - use Tymon\JWTAuth\Facades\JWTAuth; class CrossAuthenticatesUser { @@ -21,20 +20,21 @@ class CrossAuthenticatesUser if ($object->getPlatform() == PlatformType::EXCHANGE) { + $token = $object->getAccessToken(); + $tokenParts = explode(".", $token); + $tokenHeader = base64_decode($tokenParts[0]); + $tokenPayload = base64_decode($tokenParts[1]); + $jwtHeader = json_decode($tokenHeader); + $jwtPayload = json_decode($tokenPayload); + dd($jwtPayload); - $response = Http::withToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leGNoYW5nZS0yLjAudGVzdFwvYXBpXC92MVwvYWNjb3VudFwvYXV0aGVudGljYXRpb25cL2xvZ2luXC9hdHRlbXB0IiwiaWF0IjoxNjMyODAwMzE1LCJleHAiOjk5NjA2MDAzMjE1LCJuYmYiOjE2MzI4MDAzMTUsImp0aSI6Ilo2cGpNMExRbWM5Q1BQQ0YiLCJ1c2VyIjp7ImlkIjo3LCJuYW1lIjoiTGVvbmcgQ2hlZSBTaW9uZyIsImVtYWlsIjoiZ2xvdmV0bGVvbmdAZ21haWwuY29tIiwidHlwZSI6Mywic3RhdHVzIjoyLCJjb21wYW55X2lkIjo5fSwic3ViIjo3LCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.j_Zr9r6W7gE7zM66NQQk9eHbzHPIz-BYbsVOE7ARukI')->get('http://exchange-2.0.test/api/v1/account/user/list')->object(); - + $response = Http::withToken($object->getAccessToken())->post('https://dev.exchange.cief-malaysia.com/api/v1/account/authentication/user/show')->object(); dd($response, $object->getAccessToken()); - } - if (! auth()->validate(['email' => $object->getEmail(), 'password' => $object->getPassword()])) { - throw new AccessUnauthorisedException('These credentials do not match our records.'); - } - - return true; + return $response; } From ee87443ca45b08cb1bd26e2325c9da35d5b61ea2 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Tue, 28 Sep 2021 22:23:51 +0800 Subject: [PATCH 6/9] pull --- .../accounts/forms/CrossPlatformLoginFormComponent.vue | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue index 6b44f6ba..6eb111db 100644 --- a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue +++ b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue @@ -31,6 +31,10 @@ export default { data(){ return { + parameters: { + access_token: this.email, + platform: 1 + }, loading: false } }, @@ -38,8 +42,8 @@ iframeEvent(event) { if(event.origin !== 'http://exchange-2.0.test') return; if (event.data.data) { - console.log(event.data.data.payload.access_token); - + this.parameters.access_token = event.data.data.payload.access_token; + this.submit(this.route('api.account.authentication.authenticate.cross.attempt'), 'post', this.section, false, false) } } }, From ee77fd236ab717ed39cc4e79c55e9ca46e072d37 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Wed, 29 Sep 2021 14:23:50 +0800 Subject: [PATCH 7/9] cross login --- .../Accounts/Processors/CrossAuthenticationProcessor.php | 7 ++----- .../Modules/Accounts/Services/CrossAuthenticatesUser.php | 7 +------ .../accounts/forms/CrossPlatformLoginFormComponent.vue | 5 +++-- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php b/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php index 1f976a42..7d77aa05 100644 --- a/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php +++ b/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php @@ -59,12 +59,9 @@ class CrossAuthenticationProcessor $this->canCrossAuthenticateUser->passes($object); - $this->crossAuthenticatesUser->execute($object); - - $user = $this->fetchesUser->execute(['email' => $object->getEmail()]); + $cross = $this->crossAuthenticatesUser->execute($object); + dd($cross); return ['access_token' => $this->generatesAuthenticationToken->execute($user), 'redirect_url' => $this->authenticationRedirect->url($user)]; - } - } \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php index e80bef3d..93b2b200 100644 --- a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php +++ b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php @@ -23,15 +23,10 @@ class CrossAuthenticatesUser $token = $object->getAccessToken(); $tokenParts = explode(".", $token); - $tokenHeader = base64_decode($tokenParts[0]); $tokenPayload = base64_decode($tokenParts[1]); - $jwtHeader = json_decode($tokenHeader); $jwtPayload = json_decode($tokenPayload); - dd($jwtPayload); - $response = Http::withToken($object->getAccessToken())->post('https://dev.exchange.cief-malaysia.com/api/v1/account/authentication/user/show')->object(); - - dd($response, $object->getAccessToken()); + $response = Http::withToken($object->getAccessToken())->get(env('EXCHANGE_URL') . 'api/v1/company/' . $jwtPayload->user->company_id . '/show')->object(); } return $response; diff --git a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue index 6eb111db..e9a38a4f 100644 --- a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue +++ b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue @@ -12,7 +12,7 @@
- +
@@ -35,12 +35,13 @@ access_token: this.email, platform: 1 }, + exchange_url: process.env.MIX_EXCHANGE_URL, loading: false } }, methods:{ iframeEvent(event) { - if(event.origin !== 'http://exchange-2.0.test') return; + if(event.origin !== this.exchange_url) return; if (event.data.data) { this.parameters.access_token = event.data.data.payload.access_token; this.submit(this.route('api.account.authentication.authenticate.cross.attempt'), 'post', this.section, false, false) From 592e8c489b29543c96f841f5b5f57129233f19c4 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Wed, 29 Sep 2021 14:24:37 +0800 Subject: [PATCH 8/9] env --- .env.example | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.env.example b/.env.example index 696b7171..a75778c8 100644 --- a/.env.example +++ b/.env.example @@ -51,3 +51,5 @@ MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" UNITY_APP_URL="https://unity.izyim.com" +EXCHANGE_URL=https://dev.exchange.cief-malaysia.com/ +MIX_EXCHANGE_URL="${EXCHANGE_URL}" From 05cb3dd35ef7f7defe2d1c74bebf88325e16712e Mon Sep 17 00:00:00 2001 From: glovetleong Date: Wed, 13 Oct 2021 14:10:32 +0800 Subject: [PATCH 9/9] cross login --- .../CrossAuthenticateUserLogic.php | 8 +- .../UserSocialAccountObject.php | 54 +++++++ .../CrossAuthenticationProcessor.php | 148 +++++++++++++++++- .../Services/CreatesUserSocialAccount.php | 24 +++ .../Services/CrossAuthenticatesUser.php | 4 + .../Services/FetchesUserSocialAccount.php | 33 ++++ app/Models/UserSocialAccount.php | 29 +++- .../forms/CrossPlatformLoginFormComponent.vue | 16 +- 8 files changed, 303 insertions(+), 13 deletions(-) create mode 100644 app/Classes/Modules/Accounts/DataTransferObjects/UserSocialAccountObject.php create mode 100644 app/Classes/Modules/Accounts/Services/CreatesUserSocialAccount.php create mode 100644 app/Classes/Modules/Accounts/Services/FetchesUserSocialAccount.php diff --git a/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php b/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php index 5819a4f0..33bce845 100644 --- a/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php +++ b/app/Classes/Modules/Accounts/ControllersLogic/CrossAuthenticateUserLogic.php @@ -18,16 +18,16 @@ class CrossAuthenticateUserLogic extends AbstractControllerLogic } /** @var CrossAuthenticationProcessor */ - private $cressAuthenticationProcessor; + private $crossAuthenticationProcessor; /** * CrossAuthenticateUserLogic constructor. * @param CrossAuthenticationProcessor $authenticationProcessor */ - public function __construct(CrossAuthenticationProcessor $cressAuthenticationProcessor) + public function __construct(CrossAuthenticationProcessor $crossAuthenticationProcessor) { - $this->cressAuthenticationProcessor = $cressAuthenticationProcessor; + $this->crossAuthenticationProcessor = $crossAuthenticationProcessor; } /** @@ -38,7 +38,7 @@ class CrossAuthenticateUserLogic extends AbstractControllerLogic * @throws \App\Classes\Exceptions\RequestValidationException */ protected function logic(Request $request): JsonResponse { - return $this->response($this->cressAuthenticationProcessor->execute($request)); + return $this->response($this->crossAuthenticationProcessor->execute($request)); } } \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/DataTransferObjects/UserSocialAccountObject.php b/app/Classes/Modules/Accounts/DataTransferObjects/UserSocialAccountObject.php new file mode 100644 index 00000000..7740a04e --- /dev/null +++ b/app/Classes/Modules/Accounts/DataTransferObjects/UserSocialAccountObject.php @@ -0,0 +1,54 @@ +app_id = $app_id; + $this->user = $user; + $this->platform = $platform; + } + + /** + * @return string + */ + public function getAppId(): String + { + return $this->app_id; + } + + /** + * @return User + */ + public function getUser(): User + { + return $this->user; + } + + /** + * @return int + */ + public function getPlatform(): int + { + return $this->platform; + } +} diff --git a/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php b/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php index 7d77aa05..5658f932 100644 --- a/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php +++ b/app/Classes/Modules/Accounts/Processors/CrossAuthenticationProcessor.php @@ -3,12 +3,35 @@ namespace App\Classes\Modules\Accounts\Processors; use App\Classes\Modules\Accounts\DataTransferObjects\CrossAuthenticationCredentialsObject; +use App\Classes\Modules\Companies\DataTransferObjects\CompanyConnectionObject; +use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject; +use App\Classes\Modules\Accounts\DataTransferObjects\UserSocialAccountObject; + use App\Classes\Modules\Accounts\Services\CrossAuthenticatesUser; use App\Classes\Modules\Accounts\Services\AuthenticationRedirect; use App\Classes\Modules\Accounts\Services\FetchesUser; use App\Classes\Modules\Accounts\Services\GeneratesAuthenticationToken; use App\Classes\Modules\Accounts\Standards\Rules\CanCrossAuthenticateUser; +use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject; + +use App\Classes\Modules\Accounts\Processors\CreateUserProcessor; +use App\Classes\Modules\Companies\Processors\CreateCompanyProcessor; +use App\Classes\Modules\Contacts\Processors\CreateContactProcessor; +use App\Classes\Modules\Companies\Processors\CreateCompanyModuleProcessor; +use App\Classes\Modules\Companies\Services\CreatesCompanyConnection; +use App\Classes\General\Services\GeneratesInitials; +use App\Classes\Modules\Companies\Services\ApprovesCompanyConnection; +use App\Classes\Modules\Companies\Processors\AssignEmployeeProcessor; +use App\Classes\Modules\Documents\Processors\UploadIdentityDocumentProcessor; +use App\Classes\Modules\Accounts\Services\CreatesUserSocialAccount; + +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\BusinessType; +use App\Classes\ValueObjects\Constants\CompanyType; +use App\Classes\ValueObjects\Constants\RoleTypes; + use Illuminate\Http\Request; +use App\Models\UserSocialAccount; class CrossAuthenticationProcessor { @@ -28,6 +51,33 @@ class CrossAuthenticationProcessor /** @var AuthenticationRedirect */ private $authenticationRedirect; + /** @var CreateUserProcessor */ + private $createUserProcessor; + + /** @var CreateCompanyProcessor */ + private $createCompanyProcessor; + + /** @var CreateContactProcessor */ + private $createContactProcessor; + + /** @var CreateCompanyModuleProcessor */ + private $createCompanyModuleProcessor; + + /** @var CreatesCompanyConnection */ + private $createsCompanyConnection; + + /** @var ApprovesCompanyConnection */ + private $approvesCompanyConnection; + + /** @var AssignEmployeeProcessor */ + private $assignEmployeeProcessor; + + /** @var UploadIdentityDocumentProcessor */ + private $uploadIdentityDocumentProcessor; + + /** @var CreatesUserSocialAccount */ + private $createsUserSocialAccount; + /** * CrossAuthenticationProcessor constructor. * @param CanCrossAuthenticateUser $canCrossAuthenticateUser @@ -35,14 +85,48 @@ class CrossAuthenticationProcessor * @param GeneratesAuthenticationToken $generatesAuthenticationToken * @param FetchesUser $fetchesUser * @param AuthenticationRedirect $authenticationRedirect + * @param CreateUserProcessor $createUserProcessor + * @param CreateCompanyProcessor $createCompanyProcessor + * @param CreateContactProcessor $createContactProcessor + * @param CreateCompanyModuleProcessor $createCompanyModuleProcessor + * @param CreatesCompanyConnection $createsCompanyConnection + * @param ApprovesCompanyConnection $approvesCompanyConnection + * @param AssignEmployeeProcessor $assignEmployeeProcessor + * @param UploadIdentityDocumentProcessor $uploadIdentityDocumentProcessor + * @param CreatesUserSocialAccount $createsUserSocialAccount */ - public function __construct(CanCrossAuthenticateUser $canCrossAuthenticateUser, CrossAuthenticatesUser $crossAuthenticatesUser, GeneratesAuthenticationToken $generatesAuthenticationToken, FetchesUser $fetchesUser, AuthenticationRedirect $authenticationRedirect) + public function __construct( + CanCrossAuthenticateUser $canCrossAuthenticateUser, + CrossAuthenticatesUser $crossAuthenticatesUser, + GeneratesAuthenticationToken $generatesAuthenticationToken, + FetchesUser $fetchesUser, + AuthenticationRedirect $authenticationRedirect, + CreateUserProcessor $createUserProcessor, + CreateCompanyProcessor $createCompanyProcessor, + CreateContactProcessor $createContactProcessor, + CreateCompanyModuleProcessor $createCompanyModuleProcessor, + CreatesCompanyConnection $createsCompanyConnection, + ApprovesCompanyConnection $approvesCompanyConnection, + AssignEmployeeProcessor $assignEmployeeProcessor, + UploadIdentityDocumentProcessor $uploadIdentityDocumentProcessor, + CreatesUserSocialAccount $createsUserSocialAccount + ) { $this->canCrossAuthenticateUser = $canCrossAuthenticateUser; $this->crossAuthenticatesUser = $crossAuthenticatesUser; $this->generatesAuthenticationToken = $generatesAuthenticationToken; $this->fetchesUser = $fetchesUser; $this->authenticationRedirect = $authenticationRedirect; + $this->createUserProcessor = $createUserProcessor; + $this->createCompanyProcessor = $createCompanyProcessor; + $this->createContactProcessor = $createContactProcessor; + $this->createCompanyModuleProcessor = $createCompanyModuleProcessor; + $this->createsCompanyConnection = $createsCompanyConnection; + $this->createsCompanyConnection = $createsCompanyConnection; + $this->approvesCompanyConnection = $approvesCompanyConnection; + $this->assignEmployeeProcessor = $assignEmployeeProcessor; + $this->uploadIdentityDocumentProcessor = $uploadIdentityDocumentProcessor; + $this->createsUserSocialAccount = $createsUserSocialAccount; } @@ -60,7 +144,67 @@ class CrossAuthenticationProcessor $this->canCrossAuthenticateUser->passes($object); $cross = $this->crossAuthenticatesUser->execute($object); - dd($cross); + + $user_social_account = UserSocialAccount::where('app_id', $cross->token->user->id)->first(); + if (!$user_social_account) { + + $request->request->add([ + 'name' => $cross->token->user->name, + 'email' => $cross->token->user->email, + 'password' => 'pass123', + 'password_confirmation' => 'pass123', + ]); + + $user = $this->createUserProcessor->execute( + $request, + RoleTypes::USER, + env('APP_ENV') != 'production' ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION + ); + + $company = $this->createCompanyProcessor->execute( + $cross->payload->data->name, + CompanyType::COMPANY_BUSINESS, + ApprovalStatus::PENDING_SUBMISSION + ); + + $contactObject = new ContactObject( + $cross->payload->data->contact->reference, + $cross->payload->data->contact->phone, + $cross->payload->data->contact->email, + $cross->payload->data->contact->wechat_id + ); + $contact = $this->createContactProcessor->execute($contactObject, $company); + + $companyModule = $this->createCompanyModuleProcessor->execute($company, BusinessType::IMPORTER); + + $connectionObject = new CompanyConnectionObject( + $companyModule, 'CIEF', + mt_rand(1000, 9999).(new GeneratesInitials())->name($company->name)->length(3)->generate() + ); + $connection = $this->createsCompanyConnection->execute($connectionObject); + + $this->approvesCompanyConnection->execute($connection); + + $object = new EmploymentObject($companyModule, $user); + $this->assignEmployeeProcessor->execute($object); + + $request->request->add([ + 'files' => [$cross->document->src], + 'identification_no' => $cross->payload->data->identification->reference, + ]); + + $this->uploadIdentityDocumentProcessor->execute($request, $company); + + $object = new UserSocialAccountObject( + $cross->token->user->id, + $user, + $request->input('platform') + ); + $this->createsUserSocialAccount->execute($object); + } + else { + $user = $user_social_account->user()->first(); + } return ['access_token' => $this->generatesAuthenticationToken->execute($user), 'redirect_url' => $this->authenticationRedirect->url($user)]; } diff --git a/app/Classes/Modules/Accounts/Services/CreatesUserSocialAccount.php b/app/Classes/Modules/Accounts/Services/CreatesUserSocialAccount.php new file mode 100644 index 00000000..a6fd8fcd --- /dev/null +++ b/app/Classes/Modules/Accounts/Services/CreatesUserSocialAccount.php @@ -0,0 +1,24 @@ +user_id = $object->getUser()->id; + $model->app_id = $object->getAppId(); + $model->platform = $object->getPlatform(); + return $this->handler($model); + + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php index 93b2b200..54835b17 100644 --- a/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php +++ b/app/Classes/Modules/Accounts/Services/CrossAuthenticatesUser.php @@ -27,6 +27,10 @@ class CrossAuthenticatesUser $jwtPayload = json_decode($tokenPayload); $response = Http::withToken($object->getAccessToken())->get(env('EXCHANGE_URL') . 'api/v1/company/' . $jwtPayload->user->company_id . '/show')->object(); + + $response_document = Http::withToken($object->getAccessToken())->get(env('EXCHANGE_URL') . 'api/v1/storage/' . $response->payload->data->identification->files[0]->file->file_info[0]->original->file . '/fetch')->object(); + $response->token = $jwtPayload; + $response->document = $response_document->payload; } return $response; diff --git a/app/Classes/Modules/Accounts/Services/FetchesUserSocialAccount.php b/app/Classes/Modules/Accounts/Services/FetchesUserSocialAccount.php new file mode 100644 index 00000000..c2e1c676 --- /dev/null +++ b/app/Classes/Modules/Accounts/Services/FetchesUserSocialAccount.php @@ -0,0 +1,33 @@ +repository = $repository; + } + + + /** + * @return Builder + */ + public function getRepository(): Builder + { + return $this->repository->newQuery(); + } +} \ No newline at end of file diff --git a/app/Models/UserSocialAccount.php b/app/Models/UserSocialAccount.php index b8f01866..71aa73d8 100644 --- a/app/Models/UserSocialAccount.php +++ b/app/Models/UserSocialAccount.php @@ -2,10 +2,31 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\SoftDeletes; -class UserSocialAccount extends Model +/** + * Class District + * @package App\Models + * + * @property \App\Models\User user_id + * @property string app_id + * @property int platform + */ +class UserSocialAccount extends AbstractModel { - use HasFactory; + use SoftDeletes; + + protected $table = 'user_social_accounts'; + + protected $dates = ['deleted_at']; + + /** + * @return BelongsTo + */ + public function user(): BelongsTo + { + return $this->belongsTo(User::class, 'user_id', 'id'); + } } diff --git a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue index e9a38a4f..0634ddbe 100644 --- a/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue +++ b/resources/assets/vue/components/accounts/forms/CrossPlatformLoginFormComponent.vue @@ -28,26 +28,36 @@