Skip to content

Commit

Permalink
Metadata secret check and remove fs in saml server, tried to add the …
Browse files Browse the repository at this point in the history
…relayState stuff in the logic inside of the saml server
  • Loading branch information
stefanaz2 committed Feb 23, 2023
1 parent 85a3416 commit e322ff3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
6 changes: 2 additions & 4 deletions app/routes/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ export const action = async ({ request }: ActionArgs) => {

// If not then create a login request to the IDP's redirect binding
if (!user) {
const context = await createLoginRequest();
const url = new URL(request.url);
const returnTo = url.searchParams.get('redirectTo') || '/';
return redirect(context + '&RelayState=' + returnTo);
const samlRedirectURL = await createLoginRequest(new URL(request.url));
return redirect(samlRedirectURL);
}

return redirect('/');
Expand Down
1 change: 0 additions & 1 deletion app/routes/login/callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const action = async ({ request }: ActionArgs) => {
request: request,
username: username,
remember: false,
// redirectTo: '/',
redirectTo: returnTo,
});
};
13 changes: 9 additions & 4 deletions app/saml.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
// https://github.com/remix-run/examples/pull/130/files/ec66b3060fac83eec2389eb0c96aad6d8ea4aed1#diff-02d2b71e481b2495b8a72af14f09fc28238298c7f1d19a540e37c9228985b0da
import * as samlify from 'samlify';
import * as validator from '@authenio/samlify-node-xmllint';
import { readFileSync } from 'fs';
import secrets from './lib/secrets.server';

samlify.setSchemaValidator(validator);

const { SAML_IDP_METADATA } = secrets;
if (!SAML_IDP_METADATA) {
throw new Error('Missing SAML_IDP_METADATA secret');
}

// Here we configure the service provider: https://samlify.js.org/#/sp-configuration

const sp = samlify.ServiceProvider({
Expand All @@ -29,16 +33,17 @@ const sp = samlify.ServiceProvider({

// Take the metadata stood up by the IDP and use it as the metadata for our IDP object
const idp = samlify.IdentityProvider({
metadata: readFileSync(secrets.SAML_IDP_METADATA_FILE),
metadata: SAML_IDP_METADATA,
});

export function metadata() {
return sp.getMetadata();
}

export async function createLoginRequest() {
export async function createLoginRequest(url: URL) {
const { context } = sp.createLoginRequest(idp, 'redirect');
return context;
const returnTo = url.searchParams.get('redirectTo') || '/';
return context + '&RelayState=' + returnTo;
}

export async function createLogoutRequest(user: string) {
Expand Down
File renamed without changes.

0 comments on commit e322ff3

Please sign in to comment.