fixed namespace login (#473)

This commit is contained in:
Deepak Prabhakara 2024-01-21 01:01:09 +00:00 committed by GitHub
parent 8f22962349
commit 7ad7ec0186
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 19 additions and 10 deletions

View File

@ -2,4 +2,8 @@ const getEntityId = (entityId: string, namespace: string | undefined) => {
return namespace ? `${entityId}/${namespace}` : entityId;
};
export { getEntityId };
const getSSOUrl = (appUrl: string, namespace: string | undefined) => {
return `${appUrl}/api` + (namespace ? `/namespace/${namespace}` : '') + '/saml/sso';
};
export { getEntityId, getSSOUrl };

View File

@ -2,14 +2,12 @@ import { fetchPrivateKey, fetchPublicKey } from 'utils';
const appUrl = process.env.APP_URL || 'http://localhost:4000';
const entityId = process.env.ENTITY_ID || 'https://saml.example.com/entityid';
const ssoUrl = `${appUrl}/api/saml/sso`;
const privateKey = fetchPrivateKey();
const publicKey = fetchPublicKey();
const config = {
appUrl,
entityId,
ssoUrl,
privateKey,
publicKey,
};

View File

@ -0,0 +1,3 @@
import handler from 'pages/api/saml/sso';
export default handler;

View File

@ -6,7 +6,7 @@ import type { IdPMetadata } from 'types';
import { createIdPMetadataXML } from 'utils';
import stream from 'stream';
import { promisify } from 'util';
import { getEntityId } from 'lib/entity-id';
import { getEntityId, getSSOUrl } from 'lib/entity-id';
const pipeline = promisify(stream.pipeline);
@ -26,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
const xml = await createIdPMetadataXML({
idpEntityId: getEntityId(config.entityId, req.query.namespace as any),
idpSsoUrl: config.ssoUrl,
idpSsoUrl: getSSOUrl(config.appUrl, req.query.namespace as any),
certificate: saml.stripCertHeaderAndFooter(config.publicKey),
});

View File

@ -46,7 +46,9 @@ async function processSAMLRequest(req: NextApiRequest, res: NextApiResponse, isP
const params = new URLSearchParams({ id, audience, acsUrl, providerName, relayState });
res.redirect(302, `/saml/login?${params.toString()}`);
const loginUrl = (req.query.namespace ? `/namespace/${req.query.namespace}` : '') + '/saml/login';
res.redirect(302, `${loginUrl}?${params.toString()}`);
} catch (err) {
console.error(err);

View File

@ -3,16 +3,18 @@ import Link from 'next/link';
import React from 'react';
import config from '../lib/env';
import { IdPMetadata } from '../types';
import { getEntityId } from 'lib/entity-id';
import { getEntityId, getSSOUrl } from 'lib/entity-id';
const Home: React.FC<{ metadata: IdPMetadata; params: any }> = ({ metadata, params }) => {
const namespace = params.namespace;
const { ssoUrl, entityId, certificate } = metadata;
const { ssoUrl: appUrl, entityId, certificate } = metadata;
const namespaceEntityId = getEntityId(entityId, namespace);
const metadataDownloadUrl =
'/api' + (namespace ? `/namespace/${namespace}` : '') + '/saml/metadata?download=true';
const metadataUrl = '/api' + (namespace ? `/namespace/${namespace}` : '') + '/saml/metadata';
const loginUrl = (namespace ? `/namespace/${namespace}` : '') + '/saml/login';
const ssoUrl = getSSOUrl(appUrl, namespace);
return (
<div className='flex items-center justify-center'>
<div className='flex w-full max-w-4xl flex-col space-y-5 px-2'>
@ -41,7 +43,7 @@ const Home: React.FC<{ metadata: IdPMetadata; params: any }> = ({ metadata, para
Metadata URL
</Link>
</div>
<Link href='/saml/login' className='btn-outline btn-primary btn'>
<Link href={loginUrl} className='btn-outline btn-primary btn'>
Test IdP Login
</Link>
</div>
@ -83,7 +85,7 @@ const Home: React.FC<{ metadata: IdPMetadata; params: any }> = ({ metadata, para
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const metadata: IdPMetadata = {
ssoUrl: config.ssoUrl,
ssoUrl: config.appUrl,
entityId: config.entityId,
certificate: config.publicKey,
};