Skip to content

Commit

Permalink
Merge pull request #1698 from uProxy/dborkan-oauth
Browse files Browse the repository at this point in the history
Use interactive param from launchAuthFlow
  • Loading branch information
dborkan committed Aug 3, 2015
2 parents 0f22172 + 9b84c27 commit 403458d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"compare-version": "^0.1.2",
"crypto": "^0.0.3",
"es6-promise": "^2.0.0",
"freedom-for-chrome": "^0.4.11",
"freedom-for-firefox": "^0.6.13",
"freedom-for-chrome": "^0.4.20",
"freedom-for-firefox": "^0.6.15",
"freedom-port-control": "^0.9.0",
"freedom-social-firebase": "^1.0.1",
"freedom-social-firebase": "^1.0.4",
"freedom-social-xmpp": "^0.4.0",
"freedomjs-anonymized-metrics": "~0.1.0",
"fs-extra": "^0.12.0",
Expand Down
3 changes: 2 additions & 1 deletion src/chrome/app/scripts/chrome_oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class Chrome_oauth {
public launchAuthFlow(
authUrl:string,
stateObj:{redirect:string},
interactive:boolean,
continuation:(credentials:Object)=> void) {
this.options_.connector.sendToUI(
uproxy_core_api.Update.GET_CREDENTIALS,
{url :authUrl, redirect :stateObj.redirect});
{url: authUrl, redirect: stateObj.redirect, interactive: interactive});
this.options_.connector.setOnCredentials(continuation);
}

Expand Down
7 changes: 4 additions & 3 deletions src/chrome/app/scripts/chrome_ui_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var installedFreedomHooks :number[] = [];
class ChromeUIConnector {

private extPort_:chrome.runtime.Port; // The port that the extension connects to.
private onCredentials_ :(credentials:Object) => void;
private onCredentials_ :(credentials?:Object, error?:Object) => void;
private INSTALL_INCOMPLETE_PAGE_ :string = '../install-incomplete.html';

constructor(private uProxyAppChannel_ :freedom_types.OnAndEmit<any,any>) {
Expand Down Expand Up @@ -95,8 +95,9 @@ class ChromeUIConnector {
if ('emit' == msg.cmd) {
if (msg.type == uproxy_core_api.Command.SEND_CREDENTIALS) {
this.onCredentials_(msg.data);
}
if (msg.type == uproxy_core_api.Command.RESTART) {
} else if (msg.type == uproxy_core_api.Command.CREDENTIALS_ERROR) {
this.onCredentials_(undefined, msg.data);
} else if (msg.type == uproxy_core_api.Command.RESTART) {
chrome.runtime.reload();
}
this.sendToCore_(msg.type, msg.data, msg.promiseId);
Expand Down
28 changes: 20 additions & 8 deletions src/chrome/extension/scripts/chrome_tab_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import core_connector = require('../../../generic_ui/scripts/core_connector');
import uproxy_core_api = require('../../../interfaces/uproxy_core_api');
import CoreConnector = require('../../../generic_ui/scripts/core_connector');
import user_interface = require('../../../generic_ui/scripts/ui');
import chromeInterface = require('../../../interfaces/chrome');
import background = require('./background');

Expand All @@ -21,35 +20,48 @@ class ChromeTabAuth {
}

public login = (oauthInfo :chromeInterface.OAuthInfo) : void => {
this.launchAuthTab_(oauthInfo.url, oauthInfo.redirect);
this.launchAuthTab_(
oauthInfo.url, oauthInfo.redirect, oauthInfo.interactive);
}


private launchAuthTab_ = (url :string, redirectUrl :string) : void => {
private launchAuthTab_ = (url :string, redirectUrl :string, interactive :boolean) : void => {
var gotCredentials = false;
var onTabChange = (tabId :number, changeInfo :chrome.tabs.TabChangeInfo, tab :chrome.tabs.Tab) => {
if (tab.url.indexOf(redirectUrl) === 0) {
chrome.tabs.onUpdated.removeListener(onTabChange);
chrome.tabs.remove(tabId);
gotCredentials = true;
this.sendCredentials_(tab.url);
}
};

var isActive = true; //TODO use actual value
chrome.tabs.create({url: url, active: isActive},
chrome.tabs.create({url: url, active: interactive},
function(tab: chrome.tabs.Tab) {
if (isActive) {
if (interactive) {
chrome.windows.update(tab.windowId, {focused: true});
} else {
// For non-interactive login, close tab and reject if we don't have
// credentials within 5 seconds.
setTimeout(() => {
if (!gotCredentials) {
chrome.tabs.remove(tab.id);
this.onError_('Error reconnecting');
}
}, 5000);
}
chrome.tabs.onUpdated.addListener(onTabChange);
}.bind(this));
}

private onError_ = (errorText :string) : void => {
background.core.sendCommand(uproxy_core_api.Command.SEND_CREDENTIALS, errorText);
background.core.sendCommand(
uproxy_core_api.Command.CREDENTIALS_ERROR, errorText);
}

private sendCredentials_ = (url :string) : void => {
background.core.sendCommand(uproxy_core_api.Command.SEND_CREDENTIALS, url);
background.core.sendCommand(
uproxy_core_api.Command.SEND_CREDENTIALS, url);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

export interface OAuthInfo {
url :string;
redirect :string
redirect :string;
interactive :boolean;
}

// Enums for Chrome App-Extension communication.
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/uproxy_core_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export enum Command {
GET_VERSION = 1020,
HANDLE_CORE_UPDATE = 1021,
REFRESH_PORT_CONTROL = 1022,
CREDENTIALS_ERROR = 1023
}

// Updates are sent from the Core to the UI, to update state that the UI must
Expand Down

0 comments on commit 403458d

Please sign in to comment.