Skip to content

Commit 524a3bb

Browse files
committed
Refactoring authentication layer.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 9ba8e28 commit 524a3bb

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

src/Authentication.php

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,75 +76,77 @@ public function authenticate()
7676
return null;
7777
}
7878

79-
if ($route = $this->router->getCurrentRoute() and $this->routeIsProtected($route))
79+
if ( ! $route = $this->router->getCurrentRoute() or ! $this->routeIsProtected($route))
8080
{
81-
$exceptionStack = [];
81+
return null;
82+
}
8283

83-
// If authenticating via OAuth2 a route can be protected by defining its scopes.
84-
// We'll grab the scopes for this route and pass them through to the
85-
// authentication providers.
86-
if (isset($this->providers['oauth2']))
87-
{
88-
$scopes = $this->getRouteScopes($route);
84+
$exceptionStack = [];
8985

90-
$this->providers['oauth2']->setScopes($scopes);
91-
}
86+
$this->registerOAuth2Scopes($route);
9287

93-
// Spin through each of the registered authentication providers and attempt to
94-
// authenticate through one of them.
95-
foreach ($this->providers as $provider)
88+
// Spin through each of the registered authentication providers and attempt to
89+
// authenticate through one of them.
90+
foreach ($this->providers as $provider)
91+
{
92+
try
9693
{
97-
try
98-
{
99-
return $this->userId = $provider->authenticate($request);
100-
}
101-
catch (UnauthorizedHttpException $exception)
102-
{
103-
$exceptionStack[] = $exception;
104-
}
105-
catch (Exception $exception)
106-
{
107-
// We won't add this exception to the stack as it's thrown when the provider
108-
// is unable to authenticate due to the correct authorization header not
109-
// being set. We will throw an exception for this below.
110-
}
94+
return $this->userId = $provider->authenticate($request);
11195
}
112-
113-
$exception = array_shift($exceptionStack);
114-
115-
if ($exception === null)
96+
catch (UnauthorizedHttpException $exception)
11697
{
117-
$exception = new UnauthorizedHttpException(null, 'Failed to authenticate because of bad credentials or an invalid authorization header.');
98+
$exceptionStack[] = $exception;
11899
}
100+
catch (Exception $exception)
101+
{
102+
// We won't add this exception to the stack as it's thrown when the provider
103+
// is unable to authenticate due to the correct authorization header not
104+
// being set. We will throw an exception for this below.
105+
}
106+
}
119107

120-
throw $exception;
108+
$exception = array_shift($exceptionStack);
109+
110+
if ($exception === null)
111+
{
112+
$exception = new UnauthorizedHttpException(null, 'Failed to authenticate because of bad credentials or an invalid authorization header.');
121113
}
114+
115+
throw $exception;
122116
}
123117

124118
/**
125-
* Determine if a route is protected.
119+
* Register the OAuth 2.0 scopes on the "oauth2" provider.
126120
*
127121
* @param \Illuminate\Routing\Route $route
128-
* @return bool
122+
* @return void
129123
*/
130-
protected function routeIsProtected(Route $route)
124+
protected function registerOAuth2Scopes(Route $route)
131125
{
132-
$action = $route->getAction();
126+
// If authenticating via OAuth2 a route can be protected by defining its scopes.
127+
// We'll grab the scopes for this route and pass them through to the
128+
// authentication providers.
129+
if (isset($this->providers['oauth2']))
130+
{
131+
$action = $route->getAction();
133132

134-
return in_array('protected', $action, true) or (isset($action['protected']) and $action['protected'] === true);
133+
$scopes = isset($action['scopes']) ? (array) $action['scopes'] : [];
134+
135+
$this->providers['oauth2']->setScopes($scopes);
136+
}
135137
}
136138

137139
/**
138-
* Get the routes scopes.
140+
* Determine if a route is protected.
139141
*
140142
* @param \Illuminate\Routing\Route $route
141-
* @return array
143+
* @return bool
142144
*/
143-
protected function getRouteScopes(Route $route)
145+
protected function routeIsProtected(Route $route)
144146
{
145147
$action = $route->getAction();
146148

147-
return isset($action['scopes']) ? (array) $action['scopes'] : [];
149+
return in_array('protected', $action, true) or (isset($action['protected']) and $action['protected'] === true);
148150
}
149151

150152
/**

0 commit comments

Comments
 (0)