diff --git a/ui/admin/app/routes/scopes/scope/authenticate/method.js b/ui/admin/app/routes/scopes/scope/authenticate/method.js index a7f6be207..9c42ca274 100644 --- a/ui/admin/app/routes/scopes/scope/authenticate/method.js +++ b/ui/admin/app/routes/scopes/scope/authenticate/method.js @@ -11,7 +11,7 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { @service session; @service notify; @service intl; - @service('browser/window') window; + @service windowManager; // =methods @@ -26,22 +26,16 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { } /** - * + * Kicks off the OIDC auth flow by opening a new window to the provider. + * @param {string} authenticatorName + * @param {object} options */ async startOIDCAuthentication(authenticatorName, options) { const oidc = getOwner(this).lookup(authenticatorName); // TODO: delegate this call from the session service so that we don't have // to look up the authenticator directly const json = await oidc.startAuthentication(options); - await this.openExternalOIDCFlow(json.attributes.auth_url); - } - - /** - * Opens the specified URL in a new tab or window. - * @param {string} url - */ - openExternalOIDCFlow(url) { - this.window.open(url); + await this.windowManager.open(json.attributes.auth_url); } // =actions diff --git a/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js b/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js index b6cb354c4..db24f3bbd 100644 --- a/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js +++ b/ui/admin/app/routes/scopes/scope/authenticate/method/oidc.js @@ -13,6 +13,7 @@ export default class ScopesScopeAuthenticateMethodOidcRoute extends Route { // =services @service session; + @service windowManager; // =attributes @@ -61,10 +62,12 @@ export default class ScopesScopeAuthenticateMethodOidcRoute extends Route { } /** - * When this route is deactivated (exited), stop polling for changes. + * When this route is deactivated (exited), stop polling for changes and close + * any windows opened via the window manager service. */ deactivate() { this.poller.cancelAll(); + this.windowManager.closeAll(); } /** diff --git a/ui/admin/app/services/window-manager.js b/ui/admin/app/services/window-manager.js index 8c54be361..b9cdacf04 100644 --- a/ui/admin/app/services/window-manager.js +++ b/ui/admin/app/services/window-manager.js @@ -28,7 +28,7 @@ export default class WindowManagerService extends Service { */ open(url) { const newWindow = this.window.open(url); - this.add(newWindow); + if (newWindow) this.add(newWindow); } /** diff --git a/ui/admin/app/styles/app.scss b/ui/admin/app/styles/app.scss index 4647973af..420258319 100644 --- a/ui/admin/app/styles/app.scss +++ b/ui/admin/app/styles/app.scss @@ -152,6 +152,12 @@ } } +.rose-dialog-cover { + svg { + max-width: 20rem; + } +} + .copyable { white-space: nowrap; diff --git a/ui/admin/app/templates/scopes/scope/authenticate/method/oidc.hbs b/ui/admin/app/templates/scopes/scope/authenticate/method/oidc.hbs index dedca3ab3..651b0a350 100644 --- a/ui/admin/app/templates/scopes/scope/authenticate/method/oidc.hbs +++ b/ui/admin/app/templates/scopes/scope/authenticate/method/oidc.hbs @@ -8,9 +8,9 @@
{{t 'resources.auth-method.questions.no-see-window'}} {{t 'actions.retry'}} diff --git a/ui/admin/config/environment.js b/ui/admin/config/environment.js index a0350eae9..14982afa1 100644 --- a/ui/admin/config/environment.js +++ b/ui/admin/config/environment.js @@ -149,8 +149,9 @@ module.exports = function (environment) { // Notification timeout should be 0 for fast tests ENV.notifyTimeout = 0; - // Session polling timeout should be short + // Polling timeouts should be short ENV.sessionPollingTimeoutSeconds = 0.25; + ENV.oidcPollingTimeoutSeconds = 0; ENV.enableConfirmService = false; } diff --git a/ui/admin/tests/acceptance/authentication-test.js b/ui/admin/tests/acceptance/authentication-test.js index 3578ae935..f42540f4f 100644 --- a/ui/admin/tests/acceptance/authentication-test.js +++ b/ui/admin/tests/acceptance/authentication-test.js @@ -12,12 +12,14 @@ import { import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import { Response } from 'miragejs'; +//import { run, later } from '@ember/runloop'; import a11yAudit from 'ember-a11y-testing/test-support/audit'; import { currentSession, authenticateSession, invalidateSession, } from 'ember-simple-auth/test-support'; +import Service from '@ember/service'; module('Acceptance | authentication', function (hooks) { setupApplicationTest(hooks); @@ -31,12 +33,15 @@ module('Acceptance | authentication', function (hooks) { let orgScopeURL; let scope; let globalAuthMethod; - let authMethod; let globalAuthMethodID; + let authMethod; let authMethodID; + let authMethodOIDC; + let authMethodOIDCID; let globalAuthenticateURL; let authenticateURL; let authMethodAuthenticateURL; + let authMethodOIDCAuthenticateURL; let authMethodGlobalAuthenticateURL; let changePasswordURL; let orgsURL; @@ -72,15 +77,21 @@ module('Acceptance | authentication', function (hooks) { scope: orgScope, type: 'password', }); + authMethodOIDC = this.server.create('auth-method', { + scope: orgScope, + type: 'oidc', + }); orgScopeID = orgScope.id; globalAuthMethodID = globalAuthMethod.id; authMethodID = authMethod.id; + authMethodOIDCID = authMethodOIDC.id; scopesURL = `/scopes`; orgScopeURL = `/scopes/${orgScopeID}`; globalAuthenticateURL = `/scopes/global/authenticate`; authenticateURL = `/scopes/${orgScopeID}/authenticate`; authMethodGlobalAuthenticateURL = `/scopes/global/authenticate/${globalAuthMethodID}`; authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`; + authMethodOIDCAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodOIDCID}`; changePasswordURL = `/account/change-password`; orgsURL = `/scopes/global/scopes`; orgEditURL = `/scopes/${orgScopeID}/edit`; @@ -105,6 +116,7 @@ module('Acceptance | authentication', function (hooks) { test('visiting auth methods authenticate route when there no methods shows a message', async function (assert) { assert.expect(2); authMethod.destroy(); + authMethodOIDC.destroy(); await visit(authenticateURL); await a11yAudit(); assert.equal(currentURL(), authenticateURL); @@ -350,4 +362,28 @@ module('Acceptance | authentication', function (hooks) { assert.notOk(getRootElement().classList.contains('rose-theme-light')); assert.notOk(getRootElement().classList.contains('rose-theme-dark')); }); + + // TODO: figure out a sustainable test strategy for polling routes + test('OIDC authentication opens a new window and closes it upon completion', async function (assert) { + assert.expect(3); + // Register mock window service + this.owner.register( + 'service:browser/window', + class extends Service { + open(/* url */) { + assert.ok(true, 'New window was opened.'); + return { + close() { + assert.ok(true, 'New window was closed.'); + assert.ok(currentSession().isAuthenticated); + }, + }; + } + } + ); + await visit(authMethodOIDCAuthenticateURL); + await click('form [type="submit"]'); + }); + + // TODO: test OIDC retry and cancel }); diff --git a/ui/desktop/app/services/ipc.js b/ui/desktop/app/services/ipc.js index 8be63359f..e7766c302 100644 --- a/ui/desktop/app/services/ipc.js +++ b/ui/desktop/app/services/ipc.js @@ -1,5 +1,5 @@ import Service from '@ember/service'; -import { getOwner } from '@ember/application'; +import { inject as service } from '@ember/service'; import { Promise } from 'rsvp'; /** @@ -58,7 +58,7 @@ export class IPCRequest { this.method = method; this.payload = payload; this.origin = window?.location?.origin; - this.#channel = new MessageChannel(); + this.#channel = new window.MessageChannel(); this.#promise = new Promise((resolve, reject) => { this.#channel.port1.onmessage = (event) => { this.close(); @@ -124,18 +124,9 @@ export class IPCRequest { * } */ export default class IpcService extends Service { - // =attributes + // =services - /** - * Looks up the window object indirectly. - * @type {Window} - */ - get window() { - // The Ember way of accessing globals... - const document = getOwner(this).lookup('service:-document').documentElement; - // defaultView === window, but without using globals directly - return document.parentNode.defaultView; - } + @service('browser/window') window; // =methods diff --git a/ui/desktop/tests/acceptance/origin-test.js b/ui/desktop/tests/acceptance/origin-test.js index 7181d33de..eddad56bb 100644 --- a/ui/desktop/tests/acceptance/origin-test.js +++ b/ui/desktop/tests/acceptance/origin-test.js @@ -19,15 +19,17 @@ import { // authenticateSession, invalidateSession, } from 'ember-simple-auth/test-support'; +import { setupBrowserFakes } from 'ember-browser-services/test-support'; +import WindowMockIPC from '../helpers/window-mock-ipc'; import config from '../../config/environment'; module('Acceptance | origin', function (hooks) { setupApplicationTest(hooks); + setupBrowserFakes(hooks, { window: true }); setupMirage(hooks); const currentOrigin = window.location.origin; let mockIPC; - let messageHandler; const instances = { scopes: { @@ -104,40 +106,12 @@ module('Acceptance | origin', function (hooks) { urls.projects = `${urls.scopes.global}/projects`; urls.targets = `${urls.projects}/targets`; - class MockIPC { - origin = null; - - invoke(method, payload) { - return this[method](payload); - } - - getOrigin() { - return this.origin; - } - - setOrigin(origin) { - this.origin = origin; - return this.origin; - } - - resetOrigin() {} - } - - mockIPC = new MockIPC(); - messageHandler = async function (event) { - if (event.origin !== currentOrigin) return; - const { method, payload } = event.data; - if (method) { - const response = await mockIPC.invoke(method, payload); - event.ports[0].postMessage(response); - } - }; - - window.addEventListener('message', messageHandler); + // Mock the postMessage interface used by IPC. + this.owner.register('service:browser/window', WindowMockIPC); + mockIPC = this.owner.lookup('service:browser/window').mockIPC; }); hooks.afterEach(function () { - window.removeEventListener('message', messageHandler); sinon.restore(); }); diff --git a/ui/desktop/tests/helpers/window-mock-ipc.js b/ui/desktop/tests/helpers/window-mock-ipc.js new file mode 100644 index 000000000..9d191d87a --- /dev/null +++ b/ui/desktop/tests/helpers/window-mock-ipc.js @@ -0,0 +1,66 @@ +import Service from '@ember/service'; + +class MockIPC { + origin = null; + + invoke(method, payload) { + return this[method](payload); + } + + getOrigin() { + return this.origin; + } + + setOrigin(origin) { + this.origin = origin; + return this.origin; + } + + resetOrigin() {} +} + +/** + * Mock window service with support for simplified postMessage and + * MessageChannel APIs. This mock implements just enough functionality + * to support the IPC mechanism. + * + * This mock is necessary to guarantee synchronous and deterministic + * operation. The window's own API is evented and asychronous, which + * cannot be relied on for testing purposes. + * + * @todo refactor mock postMessage and MessageChannel for reusability + * outside of tests + */ +export default class extends Service { + // =attributes + + mockIPC = new MockIPC(); + + MessageChannel = class MessageChannel { + constructor() { + this.port1.postMessage = this.port1.postMessage.bind(this); + this.port2.postMessage = this.port2.postMessage.bind(this); + } + port1 = { + postMessage() {}, + close() {}, + }; + port2 = { + postMessage(data) { + this.port1.onmessage({ data }); + }, + close() {}, + }; + }; + + // =methods + + postMessage(data, origin, ports) { + if (origin !== window.location.origin) return; + const { method, payload } = data; + if (method) { + const response = this.mockIPC.invoke(method, payload); + ports[0].postMessage(response); + } + } +} diff --git a/ui/desktop/tests/unit/services/ipc-test.js b/ui/desktop/tests/unit/services/ipc-test.js index a892ccccc..d193074c5 100644 --- a/ui/desktop/tests/unit/services/ipc-test.js +++ b/ui/desktop/tests/unit/services/ipc-test.js @@ -6,6 +6,23 @@ import sinon from 'sinon'; module('Unit | Service | ipc', function (hooks) { setupTest(hooks); + class MessageChannelMock { + constructor() { + this.port1.postMessage = this.port1.postMessage.bind(this); + this.port2.postMessage = this.port2.postMessage.bind(this); + } + port1 = { + postMessage() {}, + close() {}, + }; + port2 = { + postMessage(data) { + this.port1.onmessage({ data }); + }, + close() {}, + }; + } + hooks.beforeEach(() => { // IPC requests talk to the main process indirectly via window.postMessage // proxy layer (mediated by preload.js). @@ -31,6 +48,7 @@ module('Unit | Service | ipc', function (hooks) { assert.equal(request.method, 'hello'); assert.equal(request.payload, 'world'); }, + MessageChannel: MessageChannelMock, }; new IPCRequest('hello', 'world', myWindow); }); @@ -45,6 +63,7 @@ module('Unit | Service | ipc', function (hooks) { test: 42, }); }, + MessageChannel: MessageChannelMock, }; const request = new IPCRequest('hello', 'world', myWindow); const response = await request; @@ -65,6 +84,7 @@ module('Unit | Service | ipc', function (hooks) { // Post back an error on the response port port.postMessage(new Error(stringifiedPayload)); }, + MessageChannel: MessageChannelMock, }; try { await new IPCRequest('hello', 'world', myWindow);