From 448f23c06cf6ec255212bc04e29bda4d8fe37e2b Mon Sep 17 00:00:00 2001 From: Sam Light Date: Fri, 9 Sep 2022 15:29:01 +0100 Subject: Created auth middleware base class --- src/Middleware/ScormPlayerAuthMiddleware.php | 91 ++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/Middleware/ScormPlayerAuthMiddleware.php diff --git a/src/Middleware/ScormPlayerAuthMiddleware.php b/src/Middleware/ScormPlayerAuthMiddleware.php new file mode 100644 index 0000000..8044821 --- /dev/null +++ b/src/Middleware/ScormPlayerAuthMiddleware.php @@ -0,0 +1,91 @@ +cacheTimeout = config('scorm.cache_timeout'); + } + + public function handle(Request $request, Closure $next) + { + try { + $module = $this->findModule($request); + } + catch(\Exception $e) { + return $this->failedResponse(); + } + + $user = $request->user(); + $authorized = Cache::remember( + 'scorm-player-authorize-{$user->id}_{$module->id}', + $this->cacheTimeout, + fn() => $this->authorize($request, $module) + ); + + if($authorized) return $next($request); + else return $this->failedResponse(); + } + + protected function failedResponse() : Response + { + return response('Not authorized', 503); + } + + protected function findModule(Request $request) : Scorm + { + $route = $request->route(); + $parameters = $route->parameters(); + $routeName = $route->getName(); + + if($routeName === 'scorm-player.serve') { + $uuid = $route->parameter('uuid'); + return Cache::remember( + "scrorm-player-uuid-{$uuid}-scorm", + $this->cacheTimeout, + fn() => Scorm::where('uuid', $uuid)->firstOrFail() + ); + } + else foreach($parameters as $val) { + if(getType($val) !== 'object') continue; + + if(is_a($val, Scorm::class)) + return $val; + elseif(is_a($val, ScormSco::class)) + return Cache::remember( + "scorm-player-sco-{$val->id}-scorm", + $this->cacheTimeout, + fn() => $val->scorm + ); + elseif(is_a($val, ScormScoTracking::class)) + return Cache::remember( + "scorm-player-sco-tracking-{$val->id}-scorm", + $this->cacheTimeout, + fn() => $val->sco->scorm + ); + } + + throw new \Exception('Failed to find module from route'); + } + + protected function authorize(Request $req, Scorm $module) : bool + { + return false; + } +} -- cgit v1.2.3