1
|
|
|
<?php |
2
|
|
|
namespace App\Services\Auth; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use Illuminate\Auth\AuthenticationException; |
5
|
|
|
use Illuminate\Support\Facades\Cookie; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
//use Lcobucci\JWT\Token; |
8
|
|
|
//use Lcobucci\JWT\Token\Parser; |
9
|
|
|
use Lcobucci\JWT\Token; |
10
|
|
|
use Lcobucci\JWT\Parser; |
11
|
|
|
use Barryvdh\Debugbar\Facade as Debugbar; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Adapted from the OpenIDConnect Laravel package at |
15
|
|
|
* https://github.com/furdarius/oidconnect-laravel |
16
|
|
|
*/ |
|
|
|
|
17
|
|
|
class RequestTokenParser |
18
|
|
|
{ |
19
|
|
|
const AUTH_HEADER = "Authorization"; |
20
|
|
|
const COOKIE_KEY = "id_token"; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @var Parser |
24
|
|
|
*/ |
25
|
|
|
private $parser; |
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* TokenMiddleware constructor. |
28
|
|
|
* |
29
|
|
|
* @param Parser $parser |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
Parser $parser |
33
|
|
|
) { |
34
|
|
|
$this->parser = $parser; |
35
|
|
|
} |
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @param Request $request |
|
|
|
|
38
|
|
|
* |
39
|
|
|
* @return Token |
40
|
|
|
*/ |
41
|
|
|
public function parse(Request $request): Token |
42
|
|
|
{ |
43
|
|
|
// if ($request->session()->has(static::COOKIE_KEY)) { |
44
|
|
|
// $token = $request->session()->get(static::COOKIE_KEY); |
45
|
|
|
// } else { |
46
|
|
|
// throw new AuthenticationException("Request doesn't contain id token"); |
47
|
|
|
// } |
48
|
|
|
$token = $request->cookie(static::COOKIE_KEY); |
49
|
|
|
Debugbar::info("Retrieving token:"); |
50
|
|
|
Debugbar::info($token); |
51
|
|
|
if (empty($token)) { |
52
|
|
|
throw new AuthenticationException("Request doesn't contain id token"); |
53
|
|
|
} |
54
|
|
|
return $this->parser->parse($token); |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
$bearer = $request->headers->get(static::AUTH_HEADER); |
58
|
|
|
if (empty($bearer)) { |
59
|
|
|
throw new AuthenticationException("Request doesn't contain auth token"); |
60
|
|
|
} |
61
|
|
|
$parts = explode(" ", $bearer); |
62
|
|
|
if (count($parts) < 2) { |
63
|
|
|
throw new AuthenticationException("Invalid format of auth header"); |
64
|
|
|
} |
65
|
|
|
$jwt = $parts[1]; |
66
|
|
|
return $this->parser->parse($jwt); |
67
|
|
|
* |
68
|
|
|
*/ |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @param string $tokenString * |
|
|
|
|
73
|
|
|
* @return Token |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public function parseFromString(string $tokenString): Token { |
76
|
|
|
return $this->parser->parse($tokenString); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function save(Token $token) { |
|
|
|
|
80
|
|
|
//session([static::COOKIE_KEY => (string)$token]); |
81
|
|
|
//Cookie::queue(static::COOKIE_KEY, (string)$token); |
82
|
|
|
Debugbar::info("Saving token:"); |
83
|
|
|
Debugbar::info($token); |
84
|
|
|
cookie()->queue(cookie()->forever(static::COOKIE_KEY, (string)$token)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function forget() { |
|
|
|
|
88
|
|
|
//session()->forget(static::COOKIE_KEY); |
89
|
|
|
// if (Cookie::hasQueued(static::COOKIE_KEY)) { |
90
|
|
|
// Cookie::unqueue(static::COOKIE_KEY); |
91
|
|
|
// } |
92
|
|
|
// Cookie::queue(Cookie::forget(static::COOKIE_KEY)); |
93
|
|
|
cookie()->queue(cookie()->forget(static::COOKIE_KEY)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|