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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
6171287f
Commit
6171287f
authored
Dec 11, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Classes "\yii\authclient\provider\*" created as draft.
parent
41eaa2df
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
344 additions
and
14 deletions
+344
-14
OAuth1.php
extensions/yii/authclient/provider/OAuth1.php
+52
-0
OAuth2.php
extensions/yii/authclient/provider/OAuth2.php
+59
-0
OpenId.php
extensions/yii/authclient/provider/OpenId.php
+50
-5
ProviderInterface.php
extensions/yii/authclient/provider/ProviderInterface.php
+2
-1
ProviderTrait.php
extensions/yii/authclient/provider/ProviderTrait.php
+46
-0
redirect.php
extensions/yii/authclient/provider/views/redirect.php
+39
-0
TestCase.php
tests/unit/extensions/authclient/TestCase.php
+5
-8
ProviderTraitTest.php
...unit/extensions/authclient/provider/ProviderTraitTest.php
+91
-0
No files found.
extensions/yii/authclient/provider/OAuth1.php
0 → 100644
View file @
6171287f
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\authclient\provider
;
use
Yii
;
/**
* Class OAuth1
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
OAuth1
extends
\yii\authclient\OAuth1
implements
ProviderInterface
{
use
ProviderTrait
;
/**
* @inheritdoc
*/
public
function
authenticate
()
{
// user denied error
if
(
isset
(
$_GET
[
'denied'
]))
{
return
$this
->
redirectCancel
();
}
if
(
isset
(
$_REQUEST
[
'oauth_token'
]))
{
$oauthToken
=
$_REQUEST
[
'oauth_token'
];
}
if
(
!
isset
(
$oauthToken
))
{
// Get request token.
$requestToken
=
$this
->
fetchRequestToken
();
// Get authorization URL.
$url
=
$this
->
buildAuthUrl
(
$requestToken
);
// Redirect to authorization URL.
return
Yii
::
$app
->
getResponse
()
->
redirect
(
$url
);
}
else
{
// Upgrade to access token.
$accessToken
=
$this
->
fetchAccessToken
();
$this
->
isAuthenticated
=
true
;
}
return
$this
->
isAuthenticated
;
}
}
\ No newline at end of file
extensions/yii/authclient/provider/OAuth2.php
0 → 100644
View file @
6171287f
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\authclient\provider
;
use
Yii
;
use
yii\base\Exception
;
/**
* Class OAuth2
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
OAuth2
extends
\yii\authclient\OAuth2
implements
ProviderInterface
{
use
ProviderTrait
;
/**
* @inheritdoc
*/
public
function
authenticate
()
{
if
(
isset
(
$_GET
[
'error'
]))
{
if
(
$_GET
[
'error'
]
==
'access_denied'
)
{
// user denied error
return
$this
->
redirectCancel
();
}
else
{
// request error
if
(
isset
(
$_GET
[
'error_description'
]))
{
$errorMessage
=
$_GET
[
'error_description'
];
}
elseif
(
isset
(
$_GET
[
'error_message'
]))
{
$errorMessage
=
$_GET
[
'error_message'
];
}
else
{
$errorMessage
=
http_build_query
(
$_GET
);
}
throw
new
Exception
(
'Auth error: '
.
$errorMessage
);
}
}
// Get the access_token and save them to the session.
if
(
isset
(
$_GET
[
'code'
]))
{
$code
=
$_GET
[
'code'
];
$token
=
$this
->
fetchAccessToken
(
$code
);
if
(
!
empty
(
$token
))
{
$this
->
isAuthenticated
=
true
;
}
}
else
{
$url
=
$this
->
buildAuthUrl
();
return
Yii
::
$app
->
getResponse
()
->
redirect
(
$url
);
}
return
$this
->
isAuthenticated
;
}
}
\ No newline at end of file
extensions/yii/authclient/provider/OpenId.php
View file @
6171287f
...
...
@@ -7,7 +7,9 @@
namespace
yii\authclient\provider
;
use
yii\authclient\openid\Client
;
use
Yii
;
use
yii\base\Exception
;
use
yii\web\HttpException
;
/**
* Class OpenId
...
...
@@ -15,16 +17,58 @@ use yii\authclient\openid\Client;
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
OpenId
extends
Client
implements
ProviderInterface
class
OpenId
extends
\yii\authclient\OpenId
implements
ProviderInterface
{
use
ProviderTrait
;
/**
* Authenticate the user.
* @return boolean whether user was successfully authenticated.
* @inheritdoc
*/
public
function
authenticate
()
{
// TODO: Implement authenticate() method.
if
(
!
empty
(
$_REQUEST
[
'openid_mode'
]))
{
switch
(
$_REQUEST
[
'openid_mode'
])
{
case
'id_res'
:
if
(
$this
->
validate
())
{
$attributes
=
array
(
'id'
=>
$this
->
identity
);
$rawAttributes
=
$this
->
getAttributes
();
foreach
(
$this
->
getRequiredAttributes
()
as
$openIdAttributeName
)
{
if
(
isset
(
$rawAttributes
[
$openIdAttributeName
]))
{
$attributes
[
$openIdAttributeName
]
=
$rawAttributes
[
$openIdAttributeName
];
}
else
{
throw
new
Exception
(
'Unable to complete the authentication because the required data was not received.'
);
}
}
$this
->
setAttributes
(
$attributes
);
$this
->
isAuthenticated
=
true
;
return
true
;
}
else
{
throw
new
Exception
(
'Unable to complete the authentication because the required data was not received.'
);
}
break
;
case
'cancel'
:
$this
->
redirectCancel
();
break
;
default
:
throw
new
HttpException
(
400
);
break
;
}
}
else
{
$this
->
identity
=
$this
->
authUrl
;
// Setting identifier
$this
->
required
=
[];
// Try to get info from openid provider
foreach
(
$this
->
getRequiredAttributes
()
as
$openIdAttributeName
)
{
$this
->
required
[]
=
$openIdAttributeName
;
}
$request
=
Yii
::
$app
->
getRequest
();
$this
->
realm
=
$request
->
getHostInfo
();
$this
->
returnUrl
=
$this
->
realm
.
$request
->
getUrl
();
// getting return URL
$url
=
$this
->
authUrl
();
return
Yii
::
$app
->
getResponse
()
->
redirect
(
$url
);
}
return
false
;
}
}
\ No newline at end of file
extensions/yii/authclient/provider/ProviderInterface.php
View file @
6171287f
...
...
@@ -67,7 +67,7 @@ interface ProviderInterface
/**
* Authenticate the user.
* @return
boolean
whether user was successfully authenticated.
* @return
\yii\web\Response|boolean response instance or
whether user was successfully authenticated.
*/
public
function
authenticate
();
}
\ No newline at end of file
extensions/yii/authclient/provider/ProviderTrait.php
View file @
6171287f
...
...
@@ -173,4 +173,49 @@ trait ProviderTrait
{
return
Yii
::
$app
->
getRequest
()
->
getAbsoluteUrl
();
}
/**
* Redirect to the given URL or simply close the popup window.
* @param mixed $url URL to redirect, could be a string or array config to generate a valid URL.
* @param boolean $enforceRedirect indicates if redirect should be performed even in case of popup window.
* @return \yii\web\Response response instance.
*/
public
function
redirect
(
$url
,
$enforceRedirect
=
true
)
{
$viewData
=
[
'url'
=>
$url
,
'enforceRedirect'
=>
$enforceRedirect
,
];
$viewFile
=
__DIR__
.
DIRECTORY_SEPARATOR
.
'views'
.
DIRECTORY_SEPARATOR
.
'redirect.php'
;
$response
=
Yii
::
$app
->
getResponse
();
$response
->
content
=
Yii
::
$app
->
getView
()
->
renderFile
(
$viewFile
,
$viewData
);
return
$response
;
}
/**
* Redirect to the URL. If URL is null, {@link successUrl} will be used.
* @param string $url URL to redirect.
* @return \yii\web\Response response instance.
*/
public
function
redirectSuccess
(
$url
=
null
)
{
if
(
$url
===
null
)
{
$url
=
$this
->
getSuccessUrl
();
}
return
$this
->
redirect
(
$url
);
}
/**
* Redirect to the {@link cancelUrl} or simply close the popup window.
* @param string $url URL to redirect.
* @return \yii\web\Response response instance.
*/
public
function
redirectCancel
(
$url
=
null
)
{
if
(
$url
===
null
)
{
$url
=
$this
->
getCancelUrl
();
}
return
$this
->
redirect
(
$url
,
false
);
}
}
\ No newline at end of file
extensions/yii/authclient/provider/views/redirect.php
0 → 100644
View file @
6171287f
<?php
use
yii\helpers\Html
;
use
yii\helpers\Json
;
/* @var $this \yii\base\View */
/* @var $url string */
/* @var $enforceRedirect boolean */
$redirectJavaScript
=
<<<EOL
function popupWindowRedirect(url, enforceRedirect = true) {
if (window.opener) {
window.close();
if (enforceRedirect) {
window.opener.location = url;
}
} else {
window.location = url;
}
}
EOL;
$redirectJavaScript
.=
'popupWindowRedirect('
.
Json
::
encode
(
$url
)
.
', '
.
Json
::
encode
(
$enforceRedirect
)
.
');'
;
?>
<!DOCTYPE html>
<html>
<head>
<?=
Html
::
script
(
$redirectJavaScript
);
?>
</head>
<body>
<h2
id=
"title"
style=
"display:none;"
>
Redirecting back to the
"
<?=
Yii
::
$app
->
name
;
?>
"
...
</h2>
<h3
id=
"link"
><a
href=
"
<?=
$url
;
?>
"
>
Click here to return to the
"
<?=
Yii
::
$app
->
name
;
?>
"
.
</a></h3>
<script
type=
"text/javascript"
>
document
.
getElementById
(
'title'
).
style
.
display
=
''
;
document
.
getElementById
(
'link'
).
style
.
display
=
'none'
;
</script>
</body>
</html>
\ No newline at end of file
tests/unit/extensions/authclient/TestCase.php
View file @
6171287f
...
...
@@ -10,16 +10,11 @@ use Yii;
*/
class
TestCase
extends
\yiiunit\TestCase
{
public
static
function
setUpBeforeClass
()
{
static
::
loadClassMap
();
}
/**
* Adds sphinx extension files to [[Yii::$classPath]],
* avoiding the necessity of usage Composer autoloader.
*/
p
rotected
static
function
loadClassMap
()
p
ublic
static
function
loadClassMap
()
{
$baseNameSpace
=
'yii/authclient'
;
$basePath
=
realpath
(
__DIR__
.
'/../../../../extensions/yii/authclient'
);
...
...
@@ -30,4 +25,6 @@ class TestCase extends \yiiunit\TestCase
Yii
::
$classMap
[
$classFullName
]
=
$file
;
}
}
}
\ No newline at end of file
}
TestCase
::
loadClassMap
();
\ No newline at end of file
tests/unit/extensions/authclient/provider/ProviderTraitTest.php
0 → 100644
View file @
6171287f
<?php
namespace
yiiunit\extensions\authclient\provider
;
use
yii\authclient\provider\ProviderInterface
;
use
yii\authclient\provider\ProviderTrait
;
use
yii\base\Object
;
use
yiiunit\extensions\authclient\TestCase
;
class
ProviderTraitTest
extends
TestCase
{
protected
function
setUp
()
{
$config
=
[
'components'
=>
[
'user'
=>
[
'identityClass'
=>
'\yii\web\IdentityInterface'
],
'request'
=>
[
'hostInfo'
=>
'http://testdomain.com'
,
'scriptUrl'
=>
'/index.php'
,
],
]
];
$this
->
mockApplication
(
$config
,
'\yii\web\Application'
);
}
public
function
testSetGet
()
{
$provider
=
new
Provider
();
$id
=
'test_service_id'
;
$provider
->
setId
(
$id
);
$this
->
assertEquals
(
$id
,
$provider
->
getId
(),
'Unable to setup id!'
);
$successUrl
=
'http://test.success.url'
;
$provider
->
setSuccessUrl
(
$successUrl
);
$this
->
assertEquals
(
$successUrl
,
$provider
->
getSuccessUrl
(),
'Unable to setup success URL!'
);
$cancelUrl
=
'http://test.cancel.url'
;
$provider
->
setCancelUrl
(
$cancelUrl
);
$this
->
assertEquals
(
$cancelUrl
,
$provider
->
getCancelUrl
(),
'Unable to setup cancel URL!'
);
}
public
function
testGetDescriptiveData
()
{
$provider
=
new
Provider
();
$this
->
assertNotEmpty
(
$provider
->
getName
(),
'Unable to get name!'
);
$this
->
assertNotEmpty
(
$provider
->
getTitle
(),
'Unable to get title!'
);
}
/**
* @depends testSetGet
*/
public
function
testGetDefaultSuccessUrl
()
{
$provider
=
new
Provider
();
$this
->
assertNotEmpty
(
$provider
->
getSuccessUrl
(),
'Unable to get default success URL!'
);
}
/**
* @depends testSetGet
*/
public
function
testGetDefaultCancelUrl
()
{
$provider
=
new
Provider
();
$this
->
assertNotEmpty
(
$provider
->
getSuccessUrl
(),
'Unable to get default cancel URL!'
);
}
public
function
testRedirect
()
{
$provider
=
new
Provider
();
$url
=
'http://test.url'
;
$response
=
$provider
->
redirect
(
$url
,
true
);
$this
->
assertContains
(
$url
,
$response
->
content
);
}
}
class
Provider
extends
Object
implements
ProviderInterface
{
use
ProviderTrait
;
public
function
authenticate
()
{}
}
\ No newline at end of file
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