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; 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 appUrl = process.env.APP_URL || 'http://localhost:4000';
const entityId = process.env.ENTITY_ID || 'https://saml.example.com/entityid'; const entityId = process.env.ENTITY_ID || 'https://saml.example.com/entityid';
const ssoUrl = `${appUrl}/api/saml/sso`;
const privateKey = fetchPrivateKey(); const privateKey = fetchPrivateKey();
const publicKey = fetchPublicKey(); const publicKey = fetchPublicKey();
const config = { const config = {
appUrl, appUrl,
entityId, entityId,
ssoUrl,
privateKey, privateKey,
publicKey, 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 { createIdPMetadataXML } from 'utils';
import stream from 'stream'; import stream from 'stream';
import { promisify } from 'util'; import { promisify } from 'util';
import { getEntityId } from 'lib/entity-id'; import { getEntityId, getSSOUrl } from 'lib/entity-id';
const pipeline = promisify(stream.pipeline); const pipeline = promisify(stream.pipeline);
@ -26,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
const xml = await createIdPMetadataXML({ const xml = await createIdPMetadataXML({
idpEntityId: getEntityId(config.entityId, req.query.namespace as any), 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), 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 }); 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) { } catch (err) {
console.error(err); console.error(err);

View File

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