-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oauth): support Fenix WebChannels
- Loading branch information
Showing
17 changed files
with
617 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/fxa-content-server/app/scripts/models/auth_brokers/oauth-webchannel-v1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/** | ||
* WebChannel OAuth broker that speaks 'v1' of the protocol. | ||
*/ | ||
|
||
import _ from 'underscore'; | ||
import ChannelMixin from './mixins/channel'; | ||
import Cocktail from 'cocktail'; | ||
import Constants from '../../lib/constants'; | ||
import HaltBehavior from '../../views/behaviors/halt'; | ||
import OAuthRedirectAuthenticationBroker from './oauth-redirect'; | ||
import ScopedKeys from 'lib/crypto/scoped-keys'; | ||
import WebChannel from '../../lib/channels/web'; | ||
import SyncEngines from '../sync-engines'; | ||
|
||
const proto = OAuthRedirectAuthenticationBroker.prototype; | ||
|
||
const OAuthWebChannelBroker = OAuthRedirectAuthenticationBroker.extend({ | ||
defaultBehaviors: _.extend({}, proto.defaultBehaviors, { | ||
afterForceAuth: new HaltBehavior(), | ||
afterSignIn: new HaltBehavior(), | ||
}), | ||
|
||
defaultCapabilities: _.extend({}, proto.defaultCapabilities, { | ||
chooseWhatToSyncWebV1: true, | ||
fxaStatus: true, | ||
openWebmailButtonVisible: false, | ||
}), | ||
|
||
commands: _.pick(WebChannel, 'FXA_STATUS', 'OAUTH_LOGIN'), | ||
|
||
type: 'oauth-webchannel-v1', | ||
|
||
initialize(options = {}) { | ||
this.session = options.session; | ||
this._channel = options.channel; | ||
this._scopedKeys = ScopedKeys; | ||
this._metrics = options.metrics; | ||
|
||
proto.initialize.call(this, options); | ||
|
||
this.request( | ||
this.getCommand('FXA_STATUS', { | ||
service: this.relier.get('service'), | ||
}) | ||
).then(response => this.onFxaStatus(response)); | ||
}, | ||
|
||
/** | ||
* Handle a response to the `fxa_status` message. | ||
* | ||
* @param {any} [response={}] | ||
* @private | ||
*/ | ||
onFxaStatus(response = {}) { | ||
const supportedEngines = | ||
response.capabilities && response.capabilities.engines; | ||
if (supportedEngines) { | ||
// supportedEngines override the defaults | ||
const syncEngines = new SyncEngines(null, { | ||
engines: supportedEngines, | ||
window: this.window, | ||
}); | ||
return this.set('chooseWhatToSyncWebV1Engines', syncEngines); | ||
} | ||
}, | ||
|
||
/** | ||
* Get a reference to a channel. If a channel has already been created, | ||
* the cached channel will be returned. Used by the ChannelMixin. | ||
* | ||
* @method getChannel | ||
* @returns {Object} channel | ||
*/ | ||
getChannel() { | ||
if (!this._channel) { | ||
this._channel = this.createChannel(); | ||
} | ||
|
||
return this._channel; | ||
}, | ||
|
||
createChannel() { | ||
const channel = new WebChannel(Constants.ACCOUNT_UPDATES_WEBCHANNEL_ID); | ||
channel.initialize({ | ||
window: this.window, | ||
}); | ||
|
||
return channel; | ||
}, | ||
|
||
DELAY_BROKER_RESPONSE_MS: 100, | ||
|
||
sendOAuthResultToRelier(result, account) { | ||
return this._metrics.flush().then(() => { | ||
const extraParams = {}; | ||
if (result.error) { | ||
extraParams.error = result.error; | ||
} | ||
if (result.action) { | ||
extraParams.action = result.action; | ||
} | ||
|
||
result.redirect = Constants.OAUTH_WEBCHANNEL_REDIRECT; | ||
if (account) { | ||
// pairing flow inherits from the broker, but at this time it doesn't offer CWTS | ||
result.declinedSyncEngines = account.get('declinedSyncEngines'); | ||
result.offeredSyncEngines = account.get('offeredSyncEngines'); | ||
} | ||
|
||
return this.send(this.getCommand('OAUTH_LOGIN'), result); | ||
}); | ||
}, | ||
|
||
getCommand(commandName) { | ||
if (!this.commands) { | ||
throw new Error('this.commands must be specified'); | ||
} | ||
|
||
const command = this.commands[commandName]; | ||
if (!command) { | ||
throw new Error('command not found for: ' + commandName); | ||
} | ||
|
||
return command; | ||
}, | ||
}); | ||
|
||
Cocktail.mixin(OAuthWebChannelBroker, ChannelMixin); | ||
|
||
export default OAuthWebChannelBroker; |
75 changes: 75 additions & 0 deletions
75
packages/fxa-content-server/app/scripts/models/auth_brokers/pairing/supplicant-webchannel.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import OAuthErrors from '../../../lib/oauth-errors'; | ||
import OAuthWebChannelBroker from '../oauth-webchannel-v1'; | ||
import PairingChannelClient from '../../../lib/pairing-channel-client'; | ||
import setRemoteMetaData from './remote-metadata'; | ||
import SupplicantStateMachine from '../../pairing/supplicant-state-machine'; | ||
import Url from '../../../lib/url'; | ||
|
||
export default class SupplicantWebChannelBroker extends OAuthWebChannelBroker { | ||
type = 'supplicant'; | ||
|
||
initialize(options = {}) { | ||
super.initialize(options); | ||
const { config, notifier, relier } = options; | ||
|
||
if (!config.pairingClients.includes(relier.get('clientId'))) { | ||
// only approved clients may pair | ||
throw OAuthErrors.toError('INVALID_PAIRING_CLIENT'); | ||
} | ||
|
||
const channelServerUri = config.pairingChannelServerUri; | ||
const { channelId, channelKey } = relier.toJSON(); | ||
if (channelId && channelKey && channelServerUri) { | ||
this.pairingChannelClient = new PairingChannelClient( | ||
{ | ||
channelId, | ||
channelKey, | ||
channelServerUri, | ||
}, | ||
{ | ||
importPairingChannel: options.importPairingChannel, | ||
notifier, | ||
} | ||
); | ||
|
||
this.suppStateMachine = new SupplicantStateMachine( | ||
{}, | ||
{ | ||
broker: this, | ||
notifier, | ||
pairingChannelClient: this.pairingChannelClient, | ||
relier, | ||
} | ||
); | ||
|
||
this.pairingChannelClient.open(); | ||
} else { | ||
throw new Error('Failed to initialize supplicant'); | ||
} | ||
} | ||
|
||
afterSupplicantApprove() { | ||
return Promise.resolve().then(() => { | ||
this.notifier.trigger('pair:supp:authorize'); | ||
}); | ||
} | ||
|
||
sendCodeToRelier() { | ||
return Promise.resolve().then(() => { | ||
const relier = this.relier; | ||
const result = { | ||
redirect: relier.get('redirectUri'), | ||
code: relier.get('code'), | ||
state: relier.get('state'), | ||
}; | ||
|
||
this.sendOAuthResultToRelier(result); | ||
}); | ||
} | ||
|
||
setRemoteMetaData = setRemoteMetaData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.