Skip to content

Commit

Permalink
Add redirectUrl setter w7corp/easywechat#202.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jan 4, 2016
1 parent b7a05f0 commit 1a38da1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
58 changes: 51 additions & 7 deletions src/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ abstract class AbstractProvider implements ProviderInterface
/**
* Create a new provider instance.
*
* @param Request $request
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUrl
* @param Request $request
* @param string $clientId
* @param string $clientSecret
* @param string|null $redirectUrl
*/
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl)
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl = null)
{
$this->request = $request;
$this->clientId = $clientId;
$this->redirectUrl = $redirectUrl;
$this->clientSecret = $clientSecret;
$this->redirectUrl = $redirectUrl;
}

/**
Expand Down Expand Up @@ -141,12 +141,18 @@ abstract protected function mapUserToObject(array $user);
/**
* Redirect the user of the application to the provider's authentication screen.
*
* @param string $redirectUrl
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirect()
public function redirect($redirectUrl = null)
{
$state = null;

if (!is_null($redirectUrl)) {
$this->redirectUrl = $redirectUrl;
}

if ($this->usesState()) {
$this->request->getSession()->set('state', $state = md5(time()));
}
Expand Down Expand Up @@ -221,6 +227,44 @@ public function user()
return $user->setToken($token);
}

/**
* Set redirect url.
*
* @param string $redirectUrl
*
* @return $this
*/
public function setRedirectUrl($redirectUrl)
{
$this->redirectUrl = $redirectUrl;

return $this;
}

/**
* Set redirect url.
*
* @param string $redirectUrl
*
* @return $this
*/
public function withRedirectUrl($redirectUrl)
{
$this->redirectUrl = $redirectUrl;

return $this;
}

/**
* Return the redirect url.
*
* @return string
*/
public function getRedirectUrl()
{
return $this->redirectUrl;
}

/**
* Determine if the current request / session has a mismatching "state".
*
Expand Down
17 changes: 17 additions & 0 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ public function testRedirectGeneratesTheProperSymfonyRedirectResponse()
$this->assertEquals('http://auth.url', $response->getTargetUrl());
}

public function testRedirectUrl()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));

$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret');
$this->assertNull($provider->getRedirectUrl());

$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
$this->assertEquals('redirect_uri', $provider->getRedirectUrl());
$provider->setRedirectUrl('overtrue.me');
$this->assertEquals('overtrue.me', $provider->getRedirectUrl());

$provider->withRedirectUrl('http://overtrue.me');
$this->assertEquals('http://overtrue.me', $provider->getRedirectUrl());
}

public function testUserReturnsAUserInstanceForTheAuthenticatedRequest()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
Expand Down

0 comments on commit 1a38da1

Please sign in to comment.