generateBearerToken(); if (Hash::needsRehash($service->bearer_token)) { $service->bearer_token = Hash::make($service->bearer_token); } if (Hash::needsRehash($service->bearer_pwd)) { $service->bearer_pwd = Hash::make($service->bearer_pwd); } if (Hash::needsRehash($service->api_token)) { $service->api_token = Hash::make($service->api_token); } }); static::updating(function (self $service) { if (Hash::needsRehash($service->bearer_token)) { $service->bearer_token = Hash::make($service->bearer_token); } if (Hash::needsRehash($service->bearer_pwd)) { $service->bearer_pwd = Hash::make($service->bearer_pwd); } if (Hash::needsRehash($service->api_token)) { $service->api_token = Hash::make($service->api_token); } }); } /** * Generates a new Api Token * * @return string */ public function generateApiToken(): string { $this->api_token = hash('sha256', Str::uuid()); $this->api_token_date = now(); return $this->api_token; } /** * Generates new bearer credentials * * @return string */ public function generateBearerToken(): string { $this->bearer_token = hash('sha256', Str::uuid()); $this->bearer_token_date = now(); $this->bearer_pwd = hash('ripemd160', Str::uuid()); return $this->bearer_token; } /** * Increment API CRM Stat * * @return void */ public function incrementStat(): void { $serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]); if (!$serviceStat->exists) { $serviceStat->service()->associate($this); $serviceStat->brand()->associate($this->brand); } $serviceStat->nb_calls += 1; $serviceStat->saveOrFail(); } /** * Get Service from apiToken with its brand * * @param string $apiToken * @return \App\Models\Service * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public static function getFromApiToken(string $apiToken): self { $services = static::all(); foreach ($services as $service) { if (Hash::check($apiToken, $service->api_token)) { return $service; } } throw new ModelNotFoundException('Could not find service from Api Token'); } /** * Get Service from bearerToken with its brand * * @param string $bearerToken * @return \App\Models\Service * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public static function getFromBearerToken(string $bearerToken): self { $services = static::all(); foreach ($services as $service) { if (Hash::check($bearerToken, $service->bearer_token)) { return $service; } } throw new ModelNotFoundException('Could not find service from Service Token'); } /** * Authenticate from the api token string * * @param array $guards * @param string $apiToken * @return bool */ public function authenticateApi(string $apiToken): bool { return Hash::check($apiToken, $this->api_token) and ((abs(strtotime($this->api_token_date) - time()) * 60) < (config('api.tokenTimeout') * 60)); } /** * Brand BelongsTo relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function brand() { return $this->belongsTo(Brand::class); } /** * ServiceStat hasMany relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function servicesStats() { return $this->hasMany(ServiceStat::class); } }