Skip to content

Commit d058275

Browse files
committed
moves refresh token into its own request flow
1 parent 9aff2d2 commit d058275

File tree

6 files changed

+33
-18
lines changed

6 files changed

+33
-18
lines changed

src/OAuth2Demo/Client/Controllers/RequestToken.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ public function requestTokenWithAuthCode(Application $app)
2323

2424
$code = $app['request']->get('code');
2525

26+
$redirect_uri_params = array_filter(array(
27+
'show_refresh_token' => $app['request']->get('show_refresh_token'),
28+
));
29+
2630
// exchange authorization code for access token
2731
$query = array(
2832
'grant_type' => 'authorization_code',
2933
'code' => $code,
3034
'client_id' => $config['client_id'],
3135
'client_secret' => $config['client_secret'],
32-
'redirect_uri' => $urlgen->generate('authorize_redirect', array(), true),
36+
'redirect_uri' => $urlgen->generate('authorize_redirect', $redirect_uri_params, true),
3337
);
3438

3539
// determine the token endpoint to call based on our config (do this somewhere else?)
@@ -42,6 +46,10 @@ public function requestTokenWithAuthCode(Application $app)
4246

4347
// if it is succesful, display the token in our app
4448
if (isset($json['access_token'])) {
49+
if ($app['request']->get('show_refresh_token')) {
50+
return $twig->render('client/show_refresh_token.twig', array('response' => $json));
51+
}
52+
4553
return $twig->render('client/show_access_token.twig', array('response' => $json));
4654
}
4755

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>
2+
The <code>Refresh Token</code> grant type is typically used in tandem with the <code>Authorization Code</code> grant type. Click the "Authorize" button to receive an authorization code:
3+
</p>
4+
<a class="button" href="{{ app.parameters.authorize_route|slice(0, 4) == 'http' ? app.parameters.authorize_route : url(app.parameters.authorize_route) }}?response_type=code&client_id={{app.parameters.client_id}}&redirect_uri={{ url('authorize_redirect', {show_refresh_token:1})|url_encode() }}&state={{session_id}}">Authorize</a>

views/client/index.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
<li><a href="#authcode">Authorization Code</a></li>
1212
<li><a href="#implicit">Implicit</a></li>
1313
<li><a href="#usercred">User Credentials</a></li>
14+
<li><a href="#refresh">Refresh Token</a></li>
1415
</ul>
1516
<div class="simpleTabsContent">{% include 'client/grant_types/_authorization_code.twig' %}</div>
1617
<div class="simpleTabsContent">{% include 'client/grant_types/_implicit.twig' %}</div>
1718
<div class="simpleTabsContent">{% include 'client/grant_types/_user_credentials.twig' %}</div>
19+
<div class="simpleTabsContent">{% include 'client/grant_types/_refresh_token.twig' %}</div>
1820
</div>
1921
{% endblock %}

views/client/show_access_token.twig

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
<div class="help"><em>Expires in {{ response.expires_in }} seconds</em></div>
99
{% endif %}
1010

11-
{% if response.refresh_token %}
12-
<pre><code> Refresh Token: {{ response.refresh_token }} </code></pre>
13-
{% endif %}
14-
1511
<p>
1612
Now use this token to make a request to the OAuth2.0 Server's APIs:
1713
</p>
@@ -20,17 +16,5 @@
2016

2117
<div class="help"><em>This token can now be used multiple times to make API requests for this user.</em></div>
2218

23-
<br>
24-
<hr>
25-
<br>
26-
27-
<p>
28-
Or you can use the refresh token to renew your access token when it expires:
29-
</p>
30-
31-
<a class="button" href="{{ path('request_token_with_refresh_token', { 'refresh_token': response.refresh_token }) }}">renew your access token</a>
32-
33-
<div class="help"><em>The refresh token can be used both when the access token has expired and when it hasn't.</em></div>
34-
3519
<a href="{{ path('homepage') }}">back</a>
3620
{% endblock %}

views/client/show_authorization_code.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Now exchange the Authorization Code for an <strong>Access Token</strong>:
1010
<p>
1111

12-
<a class="button" href="{{ path('request_token_with_authcode', { 'code': code }) }}">make a token request</a>
12+
<a class="button" href="{{ path('request_token_with_authcode', { 'code': code, show_refresh_token: app.request.get('show_refresh_token') }) }}">make a token request</a>
1313

1414
<div class="help"><em>usually this is done behind the scenes, but we're going step-by-step so you don't miss anything!</em></div>
1515

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% extends "client/base.twig" %}
2+
3+
{% block content %}
4+
<h3>Token Retrieved!</h3>
5+
6+
<p>
7+
But let's pretend this access token has expired. Luckily, it came with a refresh token!
8+
</p>
9+
10+
<pre><code> Refresh Token: {{ response.refresh_token }} </code></pre>
11+
12+
<a class="button" href="{{ path('request_token_with_refresh_token', { 'refresh_token': response.refresh_token }) }}">renew your access token</a>
13+
14+
<div class="help"><em>The refresh token can be used to get a new access token after the access token has expired.</em></div>
15+
16+
<a href="{{ path('homepage') }}">back</a>
17+
{% endblock %}

0 commit comments

Comments
 (0)