Skip to content

Commit

Permalink
Poll auth expiration when using Okta as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreda committed Apr 22, 2023
1 parent c74de02 commit 23635e6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
6 changes: 2 additions & 4 deletions web/app/routes/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export default class AuthenticatedRoute extends Route {
await this.authenticatedUser.loadInfo.perform();

/**
* If using Google auth, kick off the task to poll for expired auth.
* Kick off the task to poll for expired auth.
*/
if (!this.configSvc.config.skip_google_auth) {
void this.session.pollForExpiredAuth.perform();
}
void this.session.pollForExpiredAuth.perform();
}
}
67 changes: 47 additions & 20 deletions web/app/services/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FlashMessageService from "ember-cli-flash/services/flash-messages";
import Ember from "ember";
import { tracked } from "@glimmer/tracking";
import simpleTimeout from "hermes/utils/simple-timeout";
import ConfigService from "hermes/services/config";
import FetchService from "./fetch";

export const REDIRECT_STORAGE_KEY = "hermes.redirectTarget";
Expand All @@ -21,6 +22,7 @@ export function isJSON(str: string) {
}

export default class SessionService extends EmberSimpleAuthSessionService {
@service("config") declare configSvc: ConfigService;
@service declare router: RouterService;
@service declare fetch: FetchService;
@service declare session: SessionService;
Expand Down Expand Up @@ -61,32 +63,57 @@ export default class SessionService extends EmberSimpleAuthSessionService {
true
);

let isLoggedIn = await this.requireAuthentication(null, () => {});
if (!this.configSvc.config.skip_google_auth) {
let isLoggedIn = await this.requireAuthentication(null, () => {});

if (this.pollResponseIs401 || !isLoggedIn) {
this.tokenIsValid = false;
if (this.pollResponseIs401 || !isLoggedIn) {
this.tokenIsValid = false;
}
} else {
this.tokenIsValid = !this.pollResponseIs401;
}

if (this.tokenIsValid) {
this.preventReauthenticationMessage = false;
} else if (!this.preventReauthenticationMessage) {
this.flashMessages.add({
title: "Login token expired",
message: "Please reauthenticate to keep using Hermes.",
type: "warning",
sticky: true,
destroyOnClick: false,
preventDuplicates: true,
buttonText: "Authenticate with Google",
buttonIcon: "google",
buttonAction: () => {
this.authenticate("authenticator:torii", "google-oauth2-bearer");
this.flashMessages.clearMessages();
},
onDestroy: () => {
this.preventReauthenticationMessage = true;
},
});
if (!this.configSvc.config.skip_google_auth) {
this.flashMessages.add({
title: "Login token expired",
message: "Please reauthenticate to keep using Hermes.",
type: "warning",
sticky: true,
destroyOnClick: false,
preventDuplicates: true,
buttonText: "Authenticate with Google",
buttonIcon: "google",
buttonAction: () => {
this.authenticate("authenticator:torii", "google-oauth2-bearer");
this.flashMessages.clearMessages();
},
onDestroy: () => {
this.preventReauthenticationMessage = true;
},
});
} else {
this.flashMessages.add({
title: "Okta session expired",
message: "Please reauthenticate to keep using Hermes.",
type: "warning",
sticky: true,
destroyOnClick: false,
preventDuplicates: true,
buttonText: "Authenticate with Okta",
buttonIcon: "okta",
buttonAction: () => {
// Reload to redirect to Okta login.
window.location.reload();
this.flashMessages.clearMessages();
},
onDestroy: () => {
this.preventReauthenticationMessage = true;
},
});
}
}

this.pollForExpiredAuth.perform();
Expand Down

0 comments on commit 23635e6

Please sign in to comment.