Skip to content

Commit b4f3384

Browse files
committed
Adding support for Google "Installed Applications" special redirect URI workflow
Detects the use of "urn:ietf:wg:oauth:2.0:oob" as the redirect URI which signifies the use of an installed application using the Google OAuth 2 implementation. With this redirect URI, a custom workflow is used whereby the authorization code is obtained from the page title as opposed to the URL fragment in the authorization code grant workflow.
1 parent 98d49f0 commit b4f3384

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

src/main/actionscript/com/adobe/protocols/oauth2/OAuth2.as

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -273,35 +273,7 @@ package com.adobe.protocols.oauth2
273273
if (code != null)
274274
{
275275
log.debug("Authorization code: " + code);
276-
277-
// set up URL request
278-
var urlRequest:URLRequest = new URLRequest(tokenEndpoint);
279-
var urlLoader:URLLoader = new URLLoader();
280-
urlRequest.method = URLRequestMethod.POST;
281-
282-
// define POST parameters
283-
var urlVariables : URLVariables = new URLVariables();
284-
urlVariables.grant_type = OAuth2Const.GRANT_TYPE_AUTHORIZATION_CODE;
285-
urlVariables.code = code;
286-
urlVariables.redirect_uri = authorizationCodeGrant.redirectUri;
287-
urlVariables.client_id = authorizationCodeGrant.clientId;
288-
urlVariables.client_secret = authorizationCodeGrant.clientSecret;
289-
urlRequest.data = urlVariables;
290-
291-
// attach event listeners
292-
urlLoader.addEventListener(Event.COMPLETE, onGetAccessTokenResult);
293-
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onGetAccessTokenError);
294-
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onGetAccessTokenError);
295-
296-
// make the call
297-
try
298-
{
299-
urlLoader.load(urlRequest);
300-
} // try statement
301-
catch (error:Error)
302-
{
303-
log.error("Error loading token endpoint \"" + tokenEndpoint + "\"");
304-
} // catch statement
276+
getAccessTokenWithAuthCode(code);
305277
} // if statement
306278
else
307279
{
@@ -311,6 +283,38 @@ package com.adobe.protocols.oauth2
311283
dispatchEvent(getAccessTokenEvent);
312284
} // else statement
313285
} // if statement
286+
} // onLocationChange
287+
288+
function getAccessTokenWithAuthCode(code:String):void
289+
{
290+
// set up URL request
291+
var urlRequest:URLRequest = new URLRequest(tokenEndpoint);
292+
var urlLoader:URLLoader = new URLLoader();
293+
urlRequest.method = URLRequestMethod.POST;
294+
295+
// define POST parameters
296+
var urlVariables : URLVariables = new URLVariables();
297+
urlVariables.grant_type = OAuth2Const.GRANT_TYPE_AUTHORIZATION_CODE;
298+
urlVariables.code = code;
299+
urlVariables.redirect_uri = authorizationCodeGrant.redirectUri;
300+
urlVariables.client_id = authorizationCodeGrant.clientId;
301+
urlVariables.client_secret = authorizationCodeGrant.clientSecret;
302+
urlRequest.data = urlVariables;
303+
304+
// attach event listeners
305+
urlLoader.addEventListener(Event.COMPLETE, onGetAccessTokenResult);
306+
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onGetAccessTokenError);
307+
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onGetAccessTokenError);
308+
309+
// make the call
310+
try
311+
{
312+
urlLoader.load(urlRequest);
313+
} // try statement
314+
catch (error:Error)
315+
{
316+
log.error("Error loading token endpoint \"" + tokenEndpoint + "\"");
317+
} // catch statement
314318

315319
function onGetAccessTokenResult(event:Event):void
316320
{
@@ -347,11 +351,25 @@ package com.adobe.protocols.oauth2
347351

348352
dispatchEvent(getAccessTokenEvent);
349353
} // onGetAccessTokenError
350-
} // onLocationChange
354+
} // getAccessTokenWithAuthCode
351355

352356
function onStageWebViewComplete(event:Event):void
353357
{
354-
log.info("Auth URL loading complete after " + (new Date().time - startTime) + "ms");
358+
// Note: Special provision made particularly for Google OAuth 2 implementation for installed
359+
// applications. Particularly, when we see a certain redirect URI, we must look for the authorization
360+
// code in the page title as opposed to in the URL. See https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi
361+
// for more information.
362+
if (authorizationCodeGrant.redirectUri == OAuth2Const.GOOGLE_INSTALLED_APPLICATION_REDIRECT_URI && event.currentTarget.title.indexOf(OAuth2Const.RESPONSE_TYPE_AUTHORIZATION_CODE) > 0)
363+
{
364+
var codeString:String = event.currentTarget.title.substring(event.currentTarget.title.indexOf(OAuth2Const.RESPONSE_TYPE_AUTHORIZATION_CODE));
365+
var code:String = codeString.split("=")[1];
366+
log.debug("Authorization code extracted from page title: " + code);
367+
getAccessTokenWithAuthCode(code);
368+
}
369+
else
370+
{
371+
log.info("Auth URL loading complete after " + (new Date().time - startTime) + "ms");
372+
}
355373
} // onStageWebViewComplete
356374

357375
function onStageWebViewError(errorEvent:ErrorEvent):void

src/main/actionscript/com/adobe/protocols/oauth2/OAuth2Const.as

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ package com.adobe.protocols.oauth2
1111

1212
public static const RESPONSE_PROPERTY_AUTHORIZATION_CODE:String = "code";
1313
public static const RESPONSE_PROPERTY_ACCESS_TOKEN:String = "access_token";
14+
15+
public static const GOOGLE_INSTALLED_APPLICATION_REDIRECT_URI:String = "urn:ietf:wg:oauth:2.0:oob";
1416
}
1517
}

0 commit comments

Comments
 (0)