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