Skip to content

Commit

Permalink
Fix the lint
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 committed Aug 28, 2023
1 parent 99afb6e commit c4e91c4
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions server/auth/types/saml/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export class SamlAuthRoutes {
let requestId: string = '';
let nextUrl: string = '/';
let redirectHash: boolean = false;

try {
const cookie = await this.sessionStorageFactory.asScoped(request).get();
if (cookie) {
requestId = cookie.saml?.requestId || '';
nextUrl = cookie.saml?.nextUrl || '/';
redirectHash = cookie.saml?.redirectHash || false;
}

if (!requestId) {
return response.badRequest({
body: 'Invalid requestId',
Expand All @@ -69,33 +69,33 @@ export class SamlAuthRoutes {
context.security_plugin.logger.error(`Failed to parse cookie: ${error}`);
return response.badRequest();
}

try {
const credentials = await this.securityClient.authToken(
requestId,
request.body.SAMLResponse,
undefined
);

const user = await this.securityClient.authenticateWithHeader(
request,
'authorization',
credentials.authorization
);

let expiryTime = Date.now() + this.config.session.ttl;
const [headerEncoded, payloadEncoded] = credentials.authorization.split('.');

if (!payloadEncoded) {
context.security_plugin.logger.error('JWT token payload not found');
}

const tokenPayload = JSON.parse(Buffer.from(payloadEncoded, 'base64').toString());

if (tokenPayload.exp) {
expiryTime = parseInt(tokenPayload.exp, 10) * 1000;
}

const cookie: SecuritySessionCookie = {
username: user.username,
credentials: {
Expand All @@ -104,19 +104,21 @@ export class SamlAuthRoutes {
authType: AuthType.SAML,
expiryTime,
};

setExtraAuthStorage(
request,
credentials.authorization,
this.getExtraAuthStorageOptions(context.security_plugin.logger)
);

this.sessionStorageFactory.asScoped(request).set(cookie);

if (redirectHash) {
return response.redirected({
headers: {
location: `${this.coreSetup.http.basePath.serverBasePath}/auth/saml/redirectUrlFragment?nextUrl=${escape(nextUrl)}`,
location: `${
this.coreSetup.http.basePath.serverBasePath
}/auth/saml/redirectUrlFragment?nextUrl=${escape(nextUrl)}`,
},
});
} else {
Expand All @@ -127,12 +129,19 @@ export class SamlAuthRoutes {
});
}
} catch (error) {
context.security_plugin.logger.error(`SAML SP initiated authentication workflow failed: ${error}`);
context.security_plugin.logger.error(
`SAML SP initiated authentication workflow failed: ${error}`
);
return response.internalError();
}
}

private async handleIdpInitiatedAcs(context: any, request: any, response: any, acsEndpoint: string) {
private async handleIdpInitiatedAcs(
context: any,
request: any,
response: any,
acsEndpoint: string
) {
try {
const credentials = await this.securityClient.authToken(
undefined,
Expand All @@ -144,7 +153,7 @@ export class SamlAuthRoutes {
'authorization',
credentials.authorization
);

let expiryTime = Date.now() + this.config.session.ttl;
const [headerEncoded, payloadEncoded, signature] = credentials.authorization.split('.');
if (!payloadEncoded) {
Expand All @@ -154,7 +163,7 @@ export class SamlAuthRoutes {
if (tokenPayload.exp) {
expiryTime = parseInt(tokenPayload.exp, 10) * 1000;
}

const cookie: SecuritySessionCookie = {
username: user.username,
credentials: {
Expand All @@ -163,15 +172,15 @@ export class SamlAuthRoutes {
authType: AuthType.SAML,
expiryTime,
};

setExtraAuthStorage(
request,
credentials.authorization,
this.getExtraAuthStorageOptions(context.security_plugin.logger)
);

this.sessionStorageFactory.asScoped(request).set(cookie);

return response.redirected({
headers: {
location: `${this.coreSetup.http.basePath.serverBasePath}/app/opensearch-dashboards`,
Expand All @@ -182,10 +191,10 @@ export class SamlAuthRoutes {
`SAML IDP initiated authentication workflow failed: ${error}`
);
}

return response.internalError();
}

public setupRoutes() {
this.router.get(
{
Expand Down Expand Up @@ -247,7 +256,7 @@ export class SamlAuthRoutes {
},
},
async (context, request, response) => {
return this.handleSamlAcs(context, request, response)
return this.handleSamlAcs(context, request, response);
}
);

Expand All @@ -262,7 +271,7 @@ export class SamlAuthRoutes {
},
},
async (context, request, response) => {
return this.handleSamlAcs(context, request, response)
return this.handleSamlAcs(context, request, response);
}
);

Expand All @@ -278,7 +287,7 @@ export class SamlAuthRoutes {
},
async (context, request, response) => {
const acsEndpoint = `${this.coreSetup.http.basePath.serverBasePath}/_opendistro/_security/saml/acs/idpinitiated`;
return await this.handleIdpInitiatedAcs(context, request, response, acsEndpoint)
return await this.handleIdpInitiatedAcs(context, request, response, acsEndpoint);
}
);

Expand All @@ -294,7 +303,7 @@ export class SamlAuthRoutes {
},
async (context, request, response) => {
const acsEndpoint = `${this.coreSetup.http.basePath.serverBasePath}/_opendistro/_security/saml/acs/idpinitiated`;
return await this.handleIdpInitiatedAcs(context, request, response, acsEndpoint)
return await this.handleIdpInitiatedAcs(context, request, response, acsEndpoint);
}
);

Expand Down

0 comments on commit c4e91c4

Please sign in to comment.