|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kauth\Auth; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author @code4mk <hiremostafa@gmail.com> |
| 7 | + * @author @0devco <with@0dev.co> |
| 8 | + * @since 2019 |
| 9 | + * @copyright 0dev.co (https://0dev.co) |
| 10 | + */ |
| 11 | + |
| 12 | +use Illuminate\Http\Request; |
| 13 | +use Kauth\Model\KauthModel; |
| 14 | +use Kauth\Token\Token; |
| 15 | +use DateTime; |
| 16 | +use Schema; |
| 17 | +use Config; |
| 18 | +use Hash; |
| 19 | +use DB; |
| 20 | +use Parrotx\Dakapi\App\Http\Controllers\Controller; |
| 21 | +class Auth extends Controller |
| 22 | +{ |
| 23 | + |
| 24 | + protected $guard; |
| 25 | + |
| 26 | + protected $socialite; |
| 27 | + |
| 28 | + protected $user_type = ''; |
| 29 | + |
| 30 | + protected $daktariApp = false; |
| 31 | + |
| 32 | + public function __construct() |
| 33 | + { |
| 34 | + $this->guard(); |
| 35 | + $this->socialite(false); |
| 36 | + } |
| 37 | + |
| 38 | + public function daktariui() |
| 39 | + { |
| 40 | + $this->daktariApp = true; |
| 41 | + return $this; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * |
| 47 | + * set gaurd name |
| 48 | + * |
| 49 | + *@return string guard |
| 50 | + */ |
| 51 | + public function guard($guard ='users') |
| 52 | + { |
| 53 | + $this->guard = $guard; |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * |
| 59 | + * socialite system |
| 60 | + * |
| 61 | + *@return boolean |
| 62 | + */ |
| 63 | + public function socialite($status = true) |
| 64 | + { |
| 65 | + $this->socialite = $status; |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * |
| 72 | + * attempt credentials |
| 73 | + * |
| 74 | + *@return string jwt |
| 75 | + */ |
| 76 | + public function attempt($credentials) |
| 77 | + { |
| 78 | + $credential = []; |
| 79 | + $getTokennames = $credentials['usernames'] ?? []; |
| 80 | + $getTokenname = $credentials['username'] ?? ''; |
| 81 | + |
| 82 | + // store credentials without password & username |
| 83 | + |
| 84 | + foreach ($credentials as $key => $value){ |
| 85 | + if($key === 'password' || !empty($key === 'usernames') || !empty($key === 'username')){ |
| 86 | + // nothing |
| 87 | + } else { |
| 88 | + $credential[$key] = $value; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // fetch user by credentials |
| 93 | + |
| 94 | + $getToken = DB::table(Config::get('kauth.guard.' . $this->guard . '.table')) |
| 95 | + // multiple username accept (id||username||email >> etc) |
| 96 | + ->where(function ($query) use ($getTokennames,$getTokenname){ |
| 97 | + foreach ($getTokennames as $key => $value) { |
| 98 | + $query->orWhere($value,$getTokenname) |
| 99 | + ->orWhere($value,$getTokenname); |
| 100 | + } |
| 101 | + }) |
| 102 | + ->where(function ($query) use ($credential) { |
| 103 | + foreach ($credential as $key => $value) { |
| 104 | + $query->where($key,$value); |
| 105 | + } |
| 106 | + }) |
| 107 | + ->first(); |
| 108 | + |
| 109 | + |
| 110 | + // check has user and socialite |
| 111 | + |
| 112 | + if(!empty($getToken) && !($this->socialite)){ |
| 113 | + $getTokenPassword = Hash::check($credentials['password'],$getToken->password); |
| 114 | + } elseif (!empty($getToken) && $this->socialite) { |
| 115 | + $getTokenPassword = true; |
| 116 | + } else { |
| 117 | + $getTokenPassword = false; |
| 118 | + } |
| 119 | + |
| 120 | + // check user and password then store jwt token |
| 121 | + |
| 122 | + if (!empty($getToken) && $getTokenPassword ) { |
| 123 | + $jwt = new KauthModel; |
| 124 | + if($this->daktariApp && $getToken->type == 'doctor'){ |
| 125 | + $jwt->user_id = $getToken->doctor_id; |
| 126 | + $jwt->user_type = $getToken->type; |
| 127 | + }elseif ($this->daktariApp && $getToken->type == 'patient') { |
| 128 | + $jwt->user_id = $getToken->patient_id; |
| 129 | + $jwt->user_type = $getToken->type; |
| 130 | + }else{ |
| 131 | + $jwt->user_id = $getToken->id; |
| 132 | + } |
| 133 | + |
| 134 | + $jwt->browser = \Request::get('browser'); |
| 135 | + $jwt->os = \Request::get('os'); |
| 136 | + $jwt->device = \Request::get('device'); |
| 137 | + $jwt->ip = \Request::get('ip'); |
| 138 | + $jwt->active = true; |
| 139 | + $jwt->guard = $this->guard; |
| 140 | + $jwt->save(); |
| 141 | + |
| 142 | + $secret = new Token(); |
| 143 | + if (Schema::hasColumn(Config::get('kauth.guard.' . $this->guard . '.table'), 'user_type')) { |
| 144 | + $this->user_type = $getToken->user_type; |
| 145 | + } |
| 146 | + $tokon = $secret->create($jwt->id,$this->user_type); |
| 147 | + $payloader = $secret->payloader($tokon); |
| 148 | + $jwt->tokon = $tokon; |
| 149 | + $jwt->iat = $payloader['iat']; |
| 150 | + $jwt->exp = $payloader['expM']; |
| 151 | + |
| 152 | + |
| 153 | + if (Schema::hasColumn($this->guard, 'user_type')) { |
| 154 | + $jwt->user_type = $getToken->user_type; |
| 155 | + } |
| 156 | + $jwt->save(); |
| 157 | + return [ |
| 158 | + 'token' => $jwt->tokon, |
| 159 | + 'type' => $getToken->type, |
| 160 | + 'did' => $getToken->doctor_id, |
| 161 | + 'pid' => $getToken->patient_id |
| 162 | + ]; |
| 163 | + } |
| 164 | + return "wrong credentials"; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * |
| 169 | + * auth check |
| 170 | + * |
| 171 | + *@return boolean |
| 172 | + */ |
| 173 | + public function check() |
| 174 | + { |
| 175 | + try { |
| 176 | + $token = new Token(); |
| 177 | + $getToken = KauthModel::where('tokon',$token->tokon())->first(); |
| 178 | + $instanceTime = new DateTime(); |
| 179 | + if(!empty($getToken) && ($instanceTime->getTimestamp() <= $getToken->exp)){ |
| 180 | + return true; |
| 181 | + } |
| 182 | + return false; |
| 183 | + } catch (\Exception $e) { |
| 184 | + return "jwt-error"; |
| 185 | + } |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * |
| 191 | + * auth user id |
| 192 | + * |
| 193 | + *@return integer id |
| 194 | + */ |
| 195 | + public function id() |
| 196 | + { |
| 197 | + try { |
| 198 | + $token = new Token(); |
| 199 | + $getToken = KauthModel::where('tokon',$token->tokon())->first(); |
| 200 | + $instanceTime = new DateTime(); |
| 201 | + if(!empty($getToken) && ($instanceTime->getTimestamp() <= $getToken->exp)){ |
| 202 | + if($this->daktariApp){ |
| 203 | + $attribute = [ |
| 204 | + "id" => $getToken->user_id, |
| 205 | + "type" => $getToken->user_type |
| 206 | + ]; |
| 207 | + }else{ |
| 208 | + $attribute = $getToken->user_id; |
| 209 | + } |
| 210 | + |
| 211 | + return $attribute; |
| 212 | + } |
| 213 | + return 0; |
| 214 | + } catch (\Exception $e) { |
| 215 | + return "jwt-error"; |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + |
| 220 | + public function auths() |
| 221 | + { |
| 222 | + $auths = KauthModel::where('user_id',$this->id()) |
| 223 | + ->orderBy('id','desc') |
| 224 | + ->get(); |
| 225 | + return $auths; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * |
| 230 | + * auth logout |
| 231 | + * |
| 232 | + * delete auth credentials |
| 233 | + */ |
| 234 | + public function logout() |
| 235 | + { |
| 236 | + try { |
| 237 | + $token = new Token(); |
| 238 | + $getToken = KauthModel::where('tokon',$token->tokon())->first(); |
| 239 | + $getToken->delete(); |
| 240 | + } catch (\Exception $e) { |
| 241 | + return "jwt-error"; |
| 242 | + } |
| 243 | + |
| 244 | + |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * |
| 249 | + * logout all devices |
| 250 | + * |
| 251 | + * delete auth user's all token |
| 252 | + * without current token id |
| 253 | + */ |
| 254 | + public function logoutOtherDevices() |
| 255 | + { |
| 256 | + try { |
| 257 | + $token = new Token(); |
| 258 | + $getToken = KauthModel::where('tokon',$token->tokon())->first(); |
| 259 | + |
| 260 | + // fetch all token without current token |
| 261 | + |
| 262 | + KauthModel::where('user_id',$getToken->user_id) |
| 263 | + -> where(function ($query) use ($getToken){ |
| 264 | + $query->whereNotIn('id',[$getToken->id]); |
| 265 | + }) |
| 266 | + ->delete(); |
| 267 | + } catch (\Exception $e) { |
| 268 | + return "jwt-error"; |
| 269 | + } |
| 270 | + |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * |
| 275 | + * refresh token |
| 276 | + * |
| 277 | + * edit existing token |
| 278 | + * |
| 279 | + */ |
| 280 | + public function refreshToken() |
| 281 | + { |
| 282 | + try { |
| 283 | + $token = new Token(); |
| 284 | + $getToken = KauthModel::where('tokon',$token->tokon())->first(); |
| 285 | + $getToken->tokon = $token->create($getToken->id); |
| 286 | + $payloader = $token->payloader($token->create($getToken->id)); |
| 287 | + $getToken->iat = $payloader['iat']; |
| 288 | + $getToken->exp = $payloader['expM']; |
| 289 | + $getToken->save(); |
| 290 | + return $getToken->tokon; |
| 291 | + } catch (\Exception $e) { |
| 292 | + return "jwt-error"; |
| 293 | + } |
| 294 | + } |
| 295 | +} |
0 commit comments