Skip to content

Commit

Permalink
Expose login url building to amp access service
Browse files Browse the repository at this point in the history
  • Loading branch information
trodrigues committed May 12, 2017
1 parent 904764c commit 9d34bef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
5 changes: 3 additions & 2 deletions extensions/amp-access-laterpay/0.1/laterpay-impl.js
Expand Up @@ -219,12 +219,13 @@ export class LaterpayVendor {
const urlPromise = this.accessService_.buildUrl(
url, /* useAuthData */ false);
return urlPromise.then(url => {
dev().fine(TAG, 'Authorization URL: ', url);
return this.accessService_.buildLoginUrl(url);
}).then(url => {
dev().info(TAG, 'Authorization URL: ', url);
return this.timer_.timeoutPromise(
AUTHORIZATION_TIMEOUT,
this.xhr_.fetchJson(url, {
credentials: 'include',
requireAmpResponseSourceOrigin: true,
}));
});
}
Expand Down
14 changes: 12 additions & 2 deletions extensions/amp-access/0.1/amp-access.js
Expand Up @@ -34,7 +34,7 @@ import {isExperimentOn} from '../../../src/experiments';
import {isObject} from '../../../src/types';
import {listenOnce} from '../../../src/event-helper';
import {dev, user} from '../../../src/log';
import {openLoginDialog} from './login-dialog';
import {getLoginUrl, openLoginDialog} from './login-dialog';
import {parseQueryString} from '../../../src/url';
import {performanceForOrNull} from '../../../src/services';
import {resourcesForDoc} from '../../../src/services';
Expand Down Expand Up @@ -157,6 +157,8 @@ export class AccessService {
/** @private @const {function(string):Promise<string>} */
this.openLoginDialog_ = openLoginDialog.bind(null, ampdoc);

this.getLoginUrl_ = getLoginUrl.bind(null, ampdoc);

/** @private {?Promise<string>} */
this.readerIdPromise_ = null;

Expand Down Expand Up @@ -919,8 +921,16 @@ export class AccessService {
}
return Promise.all(promises);
}
}

/**
* @param {string} url
* @return {string}
*/
buildLoginUrl(url) {
return this.getLoginUrl_(url);
}

}

/**
* @typedef {{
Expand Down
38 changes: 29 additions & 9 deletions extensions/amp-access/0.1/login-dialog.js
Expand Up @@ -46,6 +46,15 @@ export function openLoginDialog(ampdoc, urlOrPromise) {
return new WebLoginDialog(ampdoc.win, viewer, urlOrPromise).open();
}

export function getLoginUrl(ampdoc, urlOrPromise) {
const viewer = viewerForDoc(ampdoc);
const overrideDialog = parseInt(viewer.getParam('dialog'), 10);
if (overrideDialog) {
return new ViewerLoginDialog(viewer, urlOrPromise).getLoginUrl();
}
return new WebLoginDialog(ampdoc.win, viewer, urlOrPromise).getLoginUrl();
}


/**
* The implementation of the Login Dialog delegated via Viewer.
Expand All @@ -63,27 +72,34 @@ class ViewerLoginDialog {
this.urlOrPromise = urlOrPromise;
}

/**
* Opens the dialog. Returns the promise that will yield with the dialog's
* result or will be rejected if dialog fails. The dialog's result is
* typically a hash string from the return URL.
* @return {!Promise<string>}
*/
open() {
getLoginUrl() {
let urlPromise;
if (typeof this.urlOrPromise == 'string') {
urlPromise = Promise.resolve(this.urlOrPromise);
} else {
urlPromise = this.urlOrPromise;
}
return urlPromise.then(url => {
const loginUrl = buildLoginUrl(url, 'RETURN_URL');
return buildLoginUrl(url, 'RETURN_URL');
});
}


/**
* Opens the dialog. Returns the promise that will yield with the dialog's
* result or will be rejected if dialog fails. The dialog's result is
* typically a hash string from the return URL.
* @return {!Promise<string>}
*/
open() {
return this.getLoginUrl().then(loginUrl => {
dev().fine(TAG, 'Open viewer dialog: ', loginUrl);
return this.viewer.sendMessageAwaitResponse('openDialog', {
'url': loginUrl,
});
});
}

}


Expand Down Expand Up @@ -172,6 +188,11 @@ export class WebLoginDialog {
}
}

getLoginUrl() {
const returnUrl = this.getReturnUrl_();
return buildLoginUrl(this.urlOrPromise, returnUrl);
}

/** @private */
openInternal_() {
const screen = this.win.screen;
Expand Down Expand Up @@ -297,7 +318,6 @@ export class WebLoginDialog {
* @param {string} url
* @param {string} returnUrl
* @return {string}
* @private
*/
function buildLoginUrl(url, returnUrl) {
// RETURN_URL has to arrive here unreplaced by UrlReplacements for two
Expand Down

0 comments on commit 9d34bef

Please sign in to comment.