|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Oneofftech\Identities\Auth; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use Illuminate\Support\Facades\Auth; |
| 8 | +use Illuminate\Foundation\Auth\RedirectsUsers; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | +use Oneofftech\Identities\Facades\IdentityCrypt; |
| 11 | +use Oneofftech\Identities\Facades\Identity; |
| 12 | +use Oneofftech\Identities\Support\FindIdentity; |
| 13 | +use Oneofftech\Identities\Support\InteractsWithPreviousUrl; |
| 14 | +use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; |
| 15 | + |
| 16 | +trait ConnectUserIdentity |
| 17 | +{ |
| 18 | + use RedirectsUsers, InteractsWithPreviousUrl, InteractsWithAdditionalAttributes, FindIdentity; |
| 19 | + |
| 20 | + /** |
| 21 | + * Redirect the user to the Authentication provider authentication page. |
| 22 | + * |
| 23 | + * @return \Illuminate\Http\Response |
| 24 | + */ |
| 25 | + public function redirect(Request $request, $provider) |
| 26 | + { |
| 27 | + // save the previous url as the callback will |
| 28 | + // probably have the referrer header set |
| 29 | + // and in case of validation errors the |
| 30 | + // referrer has precedence over _previous.url |
| 31 | + $this->savePreviousUrl(); |
| 32 | + |
| 33 | + // get additional user defined attributes |
| 34 | + $this->pushAttributes($request); |
| 35 | + |
| 36 | + return Identity::driver($provider) |
| 37 | + ->redirectUrl(route('oneofftech::connect.callback', ['provider' => $provider])) |
| 38 | + ->redirect(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Obtain the user information from Authentication provider. |
| 43 | + * |
| 44 | + * if the identity exists it will be updated, otherwise a new identity will be created |
| 45 | + * |
| 46 | + * @return \Illuminate\Http\Response |
| 47 | + */ |
| 48 | + public function connect(Request $request, $provider) |
| 49 | + { |
| 50 | + // Load the previous url from the |
| 51 | + // session to redirect back in |
| 52 | + // case of errors |
| 53 | + $previous_url = $this->getPreviousUrl(); |
| 54 | + |
| 55 | + $oauthUser = Identity::driver($provider) |
| 56 | + ->redirectUrl(route('oneofftech::connect.callback', ['provider' => $provider])) |
| 57 | + ->user(); |
| 58 | + |
| 59 | + // if user denies the authorization request we get |
| 60 | + // GuzzleHttp\Exception\ClientException |
| 61 | + // Client error: `POST https://gitlab.com/oauth/token` resulted in a `401 Unauthorized` |
| 62 | + // response: {"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not ma (truncated...) |
| 63 | + |
| 64 | + // GuzzleHttp\Exception\ClientException |
| 65 | + // Client error: `POST https://gitlab/oauth/token` resulted in a `401 Unauthorized` |
| 66 | + // response: {"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not ma (truncated...) |
| 67 | + |
| 68 | + $user = $request->user(); |
| 69 | + |
| 70 | + // create or update the user's identity |
| 71 | + |
| 72 | + list($user, $identity) = DB::transaction(function () use ($user, $provider, $oauthUser) { |
| 73 | + $identity = $this->createIdentity($user, $provider, $oauthUser); |
| 74 | + |
| 75 | + return [$user, $identity]; |
| 76 | + }); |
| 77 | + |
| 78 | + // todo: event(new Connected($user, $identity)); |
| 79 | + |
| 80 | + return $this->sendConnectionResponse($request, $identity); |
| 81 | + } |
| 82 | + |
| 83 | + protected function createIdentity($user, $provider, $oauthUser) |
| 84 | + { |
| 85 | + return $user->identities()->updateOrCreate( |
| 86 | + [ |
| 87 | + 'provider'=> $provider, |
| 88 | + 'provider_id'=> IdentityCrypt::hash($oauthUser->getId()) |
| 89 | + ], |
| 90 | + [ |
| 91 | + 'token'=> IdentityCrypt::encryptString($oauthUser->token), |
| 92 | + 'refresh_token'=> IdentityCrypt::encryptString($oauthUser->refreshToken), |
| 93 | + 'expires_at'=> $oauthUser->expiresIn ? now()->addSeconds($oauthUser->expiresIn) : null, |
| 94 | + 'registration' => true, |
| 95 | + ] |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + protected function sendConnectionResponse(Request $request, $identity) |
| 100 | + { |
| 101 | + $request->session()->regenerate(); |
| 102 | + |
| 103 | + if ($response = $this->connected($this->guard()->user(), $identity, $this->pullAttributes($request), $request)) { |
| 104 | + return $response; |
| 105 | + } |
| 106 | + |
| 107 | + return redirect()->intended($this->redirectPath()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * The user identity has been connected. |
| 112 | + * |
| 113 | + * @param mixed $user |
| 114 | + * @param mixed $identity |
| 115 | + * @param array $attributes |
| 116 | + * @param \Illuminate\Http\Request $request |
| 117 | + * @return mixed |
| 118 | + */ |
| 119 | + protected function connected($user, $identity, array $attributes, Request $request) |
| 120 | + { |
| 121 | + // |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Get the guard to retrieve currently authenticated user. |
| 126 | + * |
| 127 | + * @return \Illuminate\Contracts\Auth\StatefulGuard |
| 128 | + */ |
| 129 | + protected function guard() |
| 130 | + { |
| 131 | + return Auth::guard(); |
| 132 | + } |
| 133 | +} |
0 commit comments