1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Authentication Handler |
4
|
|
|
* |
5
|
|
|
* PHP version 7 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2019 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Trait |
13
|
|
|
* @package Vperyod\AuthHandler |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2019 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2019 MIT License |
17
|
|
|
* @link https://github.com/vperyod/vperyod.auth-handler |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Vperyod\AuthHandler; |
21
|
|
|
|
22
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
23
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
24
|
|
|
|
25
|
|
|
use Aura\Auth\Auth; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Auth Request aware trait |
29
|
|
|
* |
30
|
|
|
* Trait for objects which need to know where the auth attribute is stored in |
31
|
|
|
* the request. |
32
|
|
|
* |
33
|
|
|
* @category Trait |
34
|
|
|
* @package Vperyod\AuthHandler |
35
|
|
|
* @author Jake Johns <[email protected]> |
36
|
|
|
* @license http://jnj.mit-license.org/2019 MIT License |
37
|
|
|
* @link https://github.com/vperyod/vperyod.auth-handler |
38
|
|
|
*/ |
39
|
|
|
trait AuthRequestAwareTrait |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Attribute on request where auth is stored |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
* |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected $authAttribute = Auth::class; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set auth attribute |
52
|
|
|
* |
53
|
|
|
* @param string $attr attribute on request where auth is stored |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
*/ |
59
|
2 |
|
public function setAuthAttribute($attr) |
60
|
|
|
{ |
61
|
2 |
|
$this->authAttribute = $attr; |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get auth from request |
66
|
|
|
* |
67
|
|
|
* @param Request $request PSR7 Request |
68
|
|
|
* |
69
|
|
|
* @return Auth |
70
|
|
|
* @throws \InvalidArgumentException if auth attribute is not an `Auth` |
71
|
|
|
* |
72
|
|
|
* @access protected |
73
|
|
|
*/ |
74
|
|
|
protected function getAuth(Request $request) : Auth |
75
|
2 |
|
{ |
76
|
|
|
$auth = $request->getAttribute($this->authAttribute); |
77
|
2 |
|
if (! $auth instanceof Auth) { |
78
|
2 |
|
throw new \InvalidArgumentException( |
79
|
1 |
|
'Auth attribute not available in request' |
80
|
|
|
); |
81
|
1 |
|
} |
82
|
|
|
return $auth; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get auth status from request |
87
|
|
|
* |
88
|
|
|
* @param Request $request PSR7 Request |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
* |
92
|
|
|
* @access protected |
93
|
|
|
*/ |
94
|
|
|
protected function getAuthStatus(Request $request) : string |
95
|
1 |
|
{ |
96
|
|
|
return $this->getAuth($request)->getStatus(); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Is auth status valid? |
101
|
|
|
* |
102
|
|
|
* @param Request $request PSR7 Request |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
* |
106
|
|
|
* @access protected |
107
|
|
|
*/ |
108
|
|
|
protected function isAuthValid(Request $request) : bool |
109
|
1 |
|
{ |
110
|
|
|
return $this->getAuth($request)->isValid(); |
111
|
1 |
|
} |
112
|
|
|
} |
113
|
|
|
|