|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace nguyenanhung\Laravel\BasicAuth\Middleware; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Http\JsonResponse; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Symfony\Component\HttpFoundation\Response; |
| 9 | + |
| 10 | +class BasicAuthWithCors |
| 11 | +{ |
| 12 | + protected $addDomainPassCors = []; |
| 13 | + protected $defaultDomainPassCors = [ |
| 14 | + 'localhost.test' |
| 15 | + ]; |
| 16 | + protected $defaultDomain = 'localhost.test'; |
| 17 | + protected $overrideErrorResponse = false; |
| 18 | + |
| 19 | + protected function patternDomain($domain = ''): string |
| 20 | + { |
| 21 | + if (empty($domain)) { |
| 22 | + $domain = $this->defaultDomain; |
| 23 | + } |
| 24 | + $domain = str_replace('.', '\.', $domain); |
| 25 | + $domain = trim($domain); |
| 26 | + return '/^(https?:\/\/)?([a-zA-Z0-9-]+\.)*' . $domain . '\/?/'; |
| 27 | + } |
| 28 | + |
| 29 | + protected function acceptRequestRestful($origin, Request $request, Closure $next) |
| 30 | + { |
| 31 | + return $next($request) |
| 32 | + ->header('Access - Control - Allow - Origin', $origin) |
| 33 | + ->header('Access - Control - Allow - Methods', 'GET, POST, PUT, DELETE, OPTIONS') |
| 34 | + ->header('Access - Control - Allow - Headers', 'Content - Type, Authorization'); |
| 35 | + } |
| 36 | + |
| 37 | + protected function errorResponse(): JsonResponse |
| 38 | + { |
| 39 | + $response = [ |
| 40 | + 'error' => 'Cors Error' |
| 41 | + ]; |
| 42 | + return response()->json($response, Response::HTTP_INTERNAL_SERVER_ERROR); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param Request $request |
| 47 | + * @param Closure $next |
| 48 | + * @return mixed |
| 49 | + */ |
| 50 | + public function handle(Request $request, Closure $next) |
| 51 | + { |
| 52 | + $origin = $request->header('Origin'); |
| 53 | + $defaultListDomainCors = $this->defaultDomainPassCors; |
| 54 | + |
| 55 | + // Add from Config |
| 56 | + $acceptCorsUrl = config('laravel-basic-cors.accept_from_url'); |
| 57 | + if (!empty($acceptCorsUrl)) { |
| 58 | + $acceptListDomainCors = array_merge_recursive($defaultListDomainCors, explode(',', $acceptCorsUrl)); |
| 59 | + $acceptListDomainCors = array_unique($acceptListDomainCors); |
| 60 | + } else { |
| 61 | + $acceptListDomainCors = $defaultListDomainCors; |
| 62 | + } |
| 63 | + |
| 64 | + if (!empty($this->addDomainPassCors) && is_array($this->addDomainPassCors)) { |
| 65 | + $listDomainCors = array_merge_recursive($acceptListDomainCors, $this->addDomainPassCors); |
| 66 | + } else { |
| 67 | + $listDomainCors = $acceptListDomainCors; |
| 68 | + } |
| 69 | + $listDomainCors = array_unique($listDomainCors); |
| 70 | + |
| 71 | + foreach ($listDomainCors as $domain) { |
| 72 | + if ($origin && preg_match($this->patternDomain($domain), $origin)) { |
| 73 | + return $this->acceptRequestRestful($origin, $request, $next); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return $this->errorResponse(); |
| 78 | + } |
| 79 | +} |
0 commit comments