Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
9a23e313
Commit
9a23e313
authored
Mar 03, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support authentication via query parameter.
parent
94a9087b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
10 deletions
+36
-10
Controller.php
framework/rest/Controller.php
+36
-10
No files found.
framework/rest/Controller.php
View file @
9a23e313
...
...
@@ -40,6 +40,10 @@ class Controller extends \yii\web\Controller
* HTTP Bearer authentication (the token obtained through OAuth2)
*/
const
AUTH_TYPE_BEARER
=
'Bearer'
;
/**
* Authentication by an access token passed via a query parameter
*/
const
AUTH_TYPE_QUERY
=
'Query'
;
/**
* @var string|array the configuration for creating the serializer that formats the response data.
...
...
@@ -50,14 +54,19 @@ class Controller extends \yii\web\Controller
*/
public
$enableCsrfValidation
=
false
;
/**
* @var string the authentication type. This should be a valid HTTP authentication method.
* @var string|array the supported authentication type(s). Valid values include [[AUTH_TYPE_BASIC]],
* [[AUTH_TYPE_BEARER]] and [[AUTH_TYPE_QUERY]].
*/
public
$authType
=
self
::
AUTH_TYPE_BASIC
;
public
$authType
=
[
self
::
AUTH_TYPE_BASIC
,
self
::
AUTH_TYPE_BEARER
,
self
::
AUTH_TYPE_QUERY
]
;
/**
* @var string the authentication realm to display in case when authentication fails.
*/
public
$authRealm
=
'api'
;
/**
* @var string the name of the query parameter containing the access token when [[AUTH_TYPE_QUERY]] is used.
*/
public
$authParam
=
'access-token'
;
/**
* @var string the chosen API version number
* @see supportedVersions
*/
...
...
@@ -172,17 +181,34 @@ class Controller extends \yii\web\Controller
protected
function
authenticate
()
{
$request
=
Yii
::
$app
->
getRequest
();
if
(
$this
->
authType
==
self
::
AUTH_TYPE_BASIC
)
{
$accessToken
=
$request
->
getAuthUser
();
}
else
{
$authHeader
=
$request
->
getHeaders
()
->
get
(
'Authorization'
);
if
(
$authHeader
!==
null
&&
preg_match
(
"/^
{
$this
->
authType
}
\\
s+(.*?)$/"
,
$authHeader
,
$matches
))
{
$accessToken
=
$matches
[
1
];
foreach
((
array
)
$this
->
authType
as
$authType
)
{
switch
(
$authType
)
{
case
self
::
AUTH_TYPE_BASIC
:
$accessToken
=
$request
->
getAuthUser
();
break
;
case
self
::
AUTH_TYPE_BEARER
:
$authHeader
=
$request
->
getHeaders
()
->
get
(
'Authorization'
);
if
(
$authHeader
!==
null
&&
preg_match
(
"/^
{
$this
->
authType
}
\\
s+(.*?)$/"
,
$authHeader
,
$matches
))
{
$accessToken
=
$matches
[
1
];
}
break
;
case
self
::
AUTH_TYPE_QUERY
:
$accessToken
=
$request
->
get
(
$this
->
authParam
);
break
;
}
if
(
isset
(
$accessToken
))
{
break
;
}
}
if
(
empty
(
$accessToken
)
||
!
Yii
::
$app
->
getUser
()
->
loginByAccessToken
(
$accessToken
))
{
Yii
::
$app
->
getResponse
()
->
getHeaders
()
->
set
(
"WWW-Authenticate', '
{
$this
->
authType
}
realm=
\"
{
$this
->
authRealm
}
\"
"
);
if
(
!
isset
(
$accessToken
)
||
!
Yii
::
$app
->
getUser
()
->
loginByAccessToken
(
$accessToken
))
{
if
(
!
isset
(
$accessToken
,
$authType
))
{
$authType
=
is_array
(
$this
->
authType
)
?
reset
(
$this
->
authType
)
:
$this
->
authType
;
}
if
(
$authType
!==
self
::
AUTH_TYPE_QUERY
)
{
Yii
::
$app
->
getResponse
()
->
getHeaders
()
->
set
(
'WWW-Authenticate'
,
"
{
$authType
}
realm=
\"
{
$this
->
authRealm
}
\"
"
);
}
throw
new
UnauthorizedHttpException
(
empty
(
$accessToken
)
?
'Access token required.'
:
'You are requesting with an invalid access token.'
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment