Skip to content

Commit

Permalink
Review#1: extract authenticated session check logic into a dedicated …
Browse files Browse the repository at this point in the history
…function.
  • Loading branch information
azasypkin committed Oct 5, 2020
1 parent 743dea6 commit b3057e3
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions x-pack/plugins/security/server/authentication/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ function isLoginAttemptWithProviderType(
);
}

function isSessionAuthenticated(sessionValue?: Readonly<SessionValue> | null) {
return !!sessionValue?.username;
}

/**
* Instantiates authentication provider based on the provider key from config.
* @param providerType Provider type key.
Expand Down Expand Up @@ -558,7 +562,7 @@ export class Authenticator {
return ownsSession ? { value: existingSessionValue, overwritten: false } : null;
}

const isExistingSessionAuthenticated = !!existingSessionValue?.username;
const isExistingSessionAuthenticated = isSessionAuthenticated(existingSessionValue);
const isNewSessionAuthenticated = !!authenticationResult.user;

const providerHasChanged = !!existingSessionValue && !ownsSession;
Expand Down Expand Up @@ -637,7 +641,7 @@ export class Authenticator {
// 4. Request isn't attributed with HTTP Authorization header
return (
canRedirectRequest(request) &&
(!sessionValue || !sessionValue.username) &&
!isSessionAuthenticated(sessionValue) &&
this.options.config.authc.selector.enabled &&
HTTPAuthorizationHeader.parseFromRequest(request) == null
);
Expand Down Expand Up @@ -688,14 +692,14 @@ export class Authenticator {
return authenticationResult;
}

const isSessionAuthenticated = !!sessionUpdateResult?.value?.username;
const isUpdatedSessionAuthenticated = isSessionAuthenticated(sessionUpdateResult?.value);

let preAccessRedirectURL;
if (isSessionAuthenticated && sessionUpdateResult?.overwritten) {
if (isUpdatedSessionAuthenticated && sessionUpdateResult?.overwritten) {
this.logger.debug('Redirecting user to the overwritten session UI.');
preAccessRedirectURL = `${this.options.basePath.serverBasePath}${OVERWRITTEN_SESSION_ROUTE}`;
} else if (
isSessionAuthenticated &&
isUpdatedSessionAuthenticated &&
this.shouldRedirectToAccessAgreement(sessionUpdateResult?.value ?? null)
) {
this.logger.debug('Redirecting user to the access agreement UI.');
Expand Down

0 comments on commit b3057e3

Please sign in to comment.