open-consul/ui-v2/app/routes/application.js
John Cowen 412eec7f5d UI: Improved Login/Logout flow inc SSO support (#7790)
* 6 new components for new login/logout flow, plus SSO support

UI Components:

1. AuthDialog: Wraps/orchestrates AuthForm and AuthProfile
2. AuthForm: Authorization form shown when logged out.
3. AuthProfile: Simple presentational component to show the users
'Profile'
4. OidcSelect: A 'select' component for selecting an OIDC provider,
dynamically uses either a single select menu or multiple buttons
depending on the amount of providers

Data Components:

1. JwtSource: Given an OIDC provider URL this component will request a
token from the provider and fire an donchange event when it has been
retrieved. Used by TokenSource.
2. TokenSource: Given a oidc provider name or a Consul SecretID,
TokenSource will use whichever method/API requests required to retrieve
Consul ACL Token, which is emitted to the onchange event handler.

Very basic README documentation included here, which is likely to be
refined somewhat.

* CSS required for new auth/SSO UI components

* Remaining app code required to tie the new auth/SSO work together

* CSS code required to help tie the auth/SSO work together

* Test code in order to get current tests passing with new auth/SSO flow

..plus extremely basics/skipped rendering tests for the new components

* Treat the secret received from the server as the truth

Previously we've always treated what the user typed as the truth, this
breaks down when using SSO as the user doesn't type anything to retrieve
a token. Therefore we change this so that we use the secret in the API
response as the truth.

* Make sure removing an dom tree from a buffer only removes its own tree
2020-05-12 17:14:51 +00:00

128 lines
4 KiB
JavaScript

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { hash } from 'rsvp';
import { next } from '@ember/runloop';
import WithBlockingActions from 'consul-ui/mixins/with-blocking-actions';
const removeLoading = function($from) {
return $from.classList.remove('ember-loading');
};
export default Route.extend(WithBlockingActions, {
dom: service('dom'),
router: service('router'),
nspacesRepo: service('repository/nspace/disabled'),
repo: service('repository/dc'),
settings: service('settings'),
model: function() {
return hash({
router: this.router,
dcs: this.repo.findAll(),
nspaces: this.nspacesRepo.findAll().catch(function() {
return [];
}),
// these properties are added to the controller from route/dc
// as we don't have access to the dc and nspace params in the URL
// until we get to the route/dc route
// permissions also requires the dc param
// dc: null,
// nspace: null
// token: null
// permissions: null
});
},
setupController: function(controller, model) {
controller.setProperties(model);
},
actions: {
loading: function(transition, originRoute) {
const $root = this.dom.root();
let dc = null;
if (originRoute.routeName !== 'dc' && originRoute.routeName !== 'application') {
const app = this.modelFor('application');
const model = this.modelFor('dc') || { dc: { Name: null } };
dc = this.repo.getActive(model.dc.Name, app.dcs);
}
hash({
loading: !$root.classList.contains('ember-loading'),
dc: dc,
nspace: this.nspacesRepo.getActive(),
}).then(model => {
next(() => {
const controller = this.controllerFor('application');
controller.setProperties(model);
transition.promise.finally(function() {
removeLoading($root);
controller.setProperties({
loading: false,
dc: model.dc,
});
});
});
});
return true;
},
error: function(e, transition) {
// TODO: Normalize all this better
let error = {
status: e.code || '',
message: e.message || e.detail || 'Error',
};
if (e.errors && e.errors[0]) {
error = e.errors[0];
error.message = error.title || error.detail || 'Error';
}
if (error.status === '') {
error.message = 'Error';
}
// Try and get the currently attempted dc, whereever that may be
let model = this.modelFor('dc') || this.modelFor('nspace.dc');
if (!model) {
const path = new URL(location.href).pathname
.substr(this.router.rootURL.length - 1)
.split('/')
.slice(1, 3);
model = {
nspace: { Name: 'default' },
};
if (path[0].startsWith('~')) {
model.nspace = {
Name: path.shift(),
};
}
model.dc = {
Name: path[0],
};
}
const app = this.modelFor('application') || {};
const dcs = app.dcs || [model.dc];
const nspaces = app.nspaces || [model.nspace];
const $root = this.dom.root();
hash({
dc:
error.status.toString().indexOf('5') !== 0
? this.repo.getActive(model.dc.Name, dcs)
: { Name: 'Error' },
dcs: dcs,
nspace: model.nspace,
nspaces: nspaces,
})
.then(model => Promise.all([model, this.repo.clearActive()]))
.then(([model]) => {
removeLoading($root);
// we can't use setupController as we received an error
// so we do it manually instead
this.controllerFor('application').setProperties(model);
this.controllerFor('error').setProperties({ error: error });
})
.catch(e => {
removeLoading($root);
this.controllerFor('error').setProperties({ error: error });
});
return true;
},
},
});