Skip to content

Commit

Permalink
centraldashboard: Logout with AuthService
Browse files Browse the repository at this point in the history
The AuthService logout is implemented as follows:
1. Send POST request to logout endpoint with credentials in `Authorization:
Bearer <token>` header. The token is found in the cookie set by the authservice,
called `authservice_session`.
2. A successful response will have a status code of 201 and will return a JSON
response. The JSON response includes the URL to redirect to after logout.

This commit is a rebase of this original GH PR: kubeflow#50

Signed-off-by: Stefano Fioravanzo <stefano@arrikto.com>
  • Loading branch information
StefanoFioravanzo authored and kimwnasptd committed Apr 19, 2021
1 parent cd942ff commit 335cb91
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
9 changes: 7 additions & 2 deletions components/centraldashboard/public/components/main-page.css
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,20 @@ neon-animatable {
height: 100%;
}

#User-Badge {
#LogoutButton {
right: 16px;
position: absolute;
}

#User-Badge .icon {
#LogoutButton .icon {
color: var(--primary-background-color);
}

#LogoutButton .icon:hover {
color: var(--accent-color);
cursor: pointer;
}

#Menu {
--paper-icon-button: {
width: 44px;
Expand Down
40 changes: 37 additions & 3 deletions components/centraldashboard/public/components/main-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class MainPage extends utilitiesMixin(PolymerElement) {
},
namespaces: Array,
namespace: String,
logoutUrl: {type: String, value: '/authservice/logout'},
user: String,
isClusterAdmin: {type: Boolean, value: false},
isolationMode: {type: String, value: 'undecided', readOnly: true},
Expand Down Expand Up @@ -232,9 +233,6 @@ export class MainPage extends utilitiesMixin(PolymerElement) {
let hideSidebar = false;

switch (newPage) {
case 'logout':
window.top.location.href = '/logout';
break;
case 'activity':
this.page = 'activity';
hideTabs = false;
Expand Down Expand Up @@ -427,6 +425,42 @@ export class MainPage extends utilitiesMixin(PolymerElement) {
// trigger template render
this.menuLinks = JSON.parse(JSON.stringify(this.menuLinks));
}

async logout() {
this.$.Logout.headers['Authorization'] = 'Bearer ' +
this._getAuthorizationHeader();
const req = await this.$.Logout.generateRequest();
await req.completes;
// eslint-disable-next-line no-console
console.log(req);
if (!req.succeeded) {
// eslint-disable-next-line no-console
console.log('Logout request failed');
}
if (req.response && req.response.afterLogoutURL) {
window.location.replace(req.response.afterLogoutURL);
}
}

_getAuthorizationHeader() {
const session = this._getCookie('authservice_session');
return session;
}

_getCookie(cname) {
const name = cname + '=';
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
}

window.customElements.define('main-page', MainPage);
5 changes: 3 additions & 2 deletions components/centraldashboard/public/components/main-page.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ iron-ajax(auto, url='/api/dashboard-links', handle-as='json',
on-response='_onHasDashboardLinksResponse', on-error='_onHasDashboardLinksError', loading='{{pageLoading}}')
iron-ajax#envInfo(auto='[[_shouldFetchEnv]]', url='/api/workgroup/env-info', handle-as='json',
on-response='_onEnvInfoResponse')
iron-ajax#Logout(method="POST", url='[[logoutUrl]]', handle-as='json')
aside#PageLoader(hidden='{{!pageLoading}}')
app-drawer-layout.flex(narrow='{{narrowMode}}',
force-narrow='[[or(thinView, hideSidebar)]]',
Expand Down Expand Up @@ -79,8 +80,8 @@ app-drawer-layout.flex(narrow='{{narrowMode}}',
query-params='{{queryParams}}', namespaces='[[namespaces]]'
selected='{{namespace}}', hides, hidden$='[[hideNamespaces]]'
all-namespaces='[[allNamespaces]]')
footer#User-Badge
a(target="_top", href="/logout")
footer#LogoutButton
a(target="_top", on-click='logout')
iron-icon.icon(icon='kubeflow:logout' title="Logout")
main#Content
section#ViewTabs(hidden$='[[hideTabs]]')
Expand Down

0 comments on commit 335cb91

Please sign in to comment.