1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Examples\Controllers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ExampleController |
7
|
|
|
* @package Examples\Controllers |
8
|
|
|
* |
9
|
|
|
* An example controller as how to handle the Lifeboat Auth process |
10
|
|
|
* |
11
|
|
|
* BEFORE YOU START |
12
|
|
|
* You will need to register your app with Lifeboat team. |
13
|
|
|
* Contact [email protected] to get your app credentials. |
14
|
|
|
* |
15
|
|
|
* This controller is designed to show how your app will authenticate with Lifeboat APIs |
16
|
|
|
* and allow your app to interact with Lifeboat APIs where the logged in user permissions |
17
|
|
|
* are automatically checked at an API level. |
18
|
|
|
* |
19
|
|
|
* Base url: /auth |
20
|
|
|
* |
21
|
|
|
* /auth/process |
22
|
|
|
* @see ExampleController::process() |
23
|
|
|
* This controller action shows how to handle the response from the Lifeboat Auth, |
24
|
|
|
* using the Lifeboat SDK |
25
|
|
|
* |
26
|
|
|
* /auth/error |
27
|
|
|
* @see ExampleController::error() |
28
|
|
|
* This controller action shows how to handle Lifeboat Auth errors |
29
|
|
|
* |
30
|
|
|
* /auth/change |
31
|
|
|
* @see ExampleController::change() |
32
|
|
|
* This controller action shows how to handle user requests to change between sites he/she |
33
|
|
|
* has access to |
34
|
|
|
* |
35
|
|
|
* /auth/select |
36
|
|
|
* @see ExampleController::select() |
37
|
|
|
* This controller action shows what how to present a site selection screen the user |
38
|
|
|
* has access to |
39
|
|
|
*/ |
40
|
|
|
class ExampleController extends Controller { |
|
|
|
|
41
|
|
|
|
42
|
|
|
const LIFEBOAT_APP_ID = '[[Lifeboat App ID]]'; |
43
|
|
|
const LIFEBOAT_APP_SECRET = '[[Lifeboat App Secret]]'; |
44
|
|
|
|
45
|
|
|
private static $url_segment = 'auth'; |
|
|
|
|
46
|
|
|
private static $allowed_actions = ['process', 'error', 'select', 'change']; |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** @var \Lifeboat\App $app */ |
49
|
|
|
private $app; |
50
|
|
|
|
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
parent::__construct(); |
54
|
|
|
$this->app = new \Lifeboat\App(self::LIFEBOAT_APP_ID, self::LIFEBOAT_APP_SECRET); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Process the code returns by the Lifeboat Auth process |
59
|
|
|
* and ensure the user has selected an active site |
60
|
|
|
* |
61
|
|
|
* @return HTTPResponse |
62
|
|
|
*/ |
63
|
|
|
public function process(): HTTPResponse |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// This function will automatically create an access token |
66
|
|
|
// and save it into $_SESSIONS |
67
|
|
|
$this->app->fetchAccessToken($_GET['code'] ?? ''); |
68
|
|
|
|
69
|
|
|
// Check if the user has selected a Lifeboat site to use. |
70
|
|
|
// If not redirect them so they can select |
71
|
|
|
if (!$this->app->getActiveSite()) { |
72
|
|
|
return $this->redirect($this->Link('select')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->redirect('/'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* This function handles the switching between Lifeboat sites |
80
|
|
|
* the user has access to |
81
|
|
|
* |
82
|
|
|
* Example: https://app.com/auth/change?site_key=[lifeboat_site_key] |
83
|
|
|
* |
84
|
|
|
* @return HTTPResponse |
85
|
|
|
*/ |
86
|
|
|
public function change(): HTTPResponse |
87
|
|
|
{ |
88
|
|
|
// The site of the Lifeboat site we want to be set as active |
89
|
|
|
$key = $_GET['site_key']; |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
foreach ($this->app->getSites() as $site) { |
93
|
|
|
if ($site['site_key'] === $key) { |
94
|
|
|
// This function automatically saves the user preference to $_SESSION |
95
|
|
|
$this->app->setActiveSite($site['domain'], $site['site_key']); |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} catch (\Lifeboat\Exceptions\OAuthException $e) { |
100
|
|
|
// If we encounter any auth issues, restart the auth process |
101
|
|
|
return $this->reloadAuth(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// If everything went well redirect to the app's index page |
105
|
|
|
return $this->redirect('/'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @see ExampleController::select() |
110
|
|
|
* @return HTTPResponse |
111
|
|
|
*/ |
112
|
|
|
public function select() |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
$sites = $this->app->getSites(); |
116
|
|
|
|
117
|
|
|
// If the user doesn't have access to any Lifeboat site force a re-auth |
118
|
|
|
if (count($sites) < 1) return $this->reloadAuth(); |
119
|
|
|
|
120
|
|
|
// If the user only has access to one Lifeboat site then automatically use that |
121
|
|
|
if (count($sites) === 1) { |
122
|
|
|
$this->app->setActiveSite($sites[0]['domain'], $sites[0]['site_key']); |
123
|
|
|
return $this->redirect('/'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
// Create a list of available Lifeboat sites with links |
128
|
|
|
// to switch the active site |
129
|
|
|
$options = []; |
130
|
|
|
foreach ($sites as $site) { |
131
|
|
|
$options[] = [ |
132
|
|
|
'Label' => $site['domain'], |
133
|
|
|
'URL' => $this->Link('select') . '?site_key=' . $site['site_key'] |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Render this page showing a list of all available Lifeboat sites |
138
|
|
|
// the current user has access to so that he/she can switch |
139
|
|
|
// between them |
140
|
|
|
return $this->renderWith('[[Your template]]', [ |
141
|
|
|
'Options' => $options |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
return $this; |
|
|
|
|
146
|
|
|
} catch (\Lifeboat\Exceptions\OAuthException $e) { |
147
|
|
|
return $this->reloadAuth(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return HTTPResponse |
153
|
|
|
*/ |
154
|
|
|
public function reloadAuth(): HTTPResponse |
155
|
|
|
{ |
156
|
|
|
// URL to process the auth response |
157
|
|
|
$process = $this->Link('process'); |
158
|
|
|
|
159
|
|
|
// URL to handle auth errors |
160
|
|
|
$error = $this->Link('error'); |
161
|
|
|
|
162
|
|
|
// A one-time use challenge code to prevent man in the middle attacks |
163
|
|
|
$challenge = $this->app->getAPIChallenge(); |
164
|
|
|
|
165
|
|
|
// Redirect to the auth URL |
166
|
|
|
return $this->redirect($this->app->getAuthURL($process, $error, $challenge)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getSiteName(): string |
173
|
|
|
{ |
174
|
|
|
$site = $this->app->getActiveSite(); |
175
|
|
|
return ($site) ? $site['host'] : ''; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths