From b9e9b53941408947427fda00e038e384a3e3b8ce Mon Sep 17 00:00:00 2001 From: Kiran Date: Fri, 18 Feb 2022 09:37:27 +0530 Subject: [PATCH] Cleanup --- README.md | 9 ++ lib/env.ts | 13 ++- pages/_app.tsx | 6 +- pages/api/apps/metadata/index.ts | 32 ------- pages/api/{apps => saml}/metadata/download.ts | 10 +- pages/apps/[id].tsx | 44 --------- pages/apps/index.tsx | 60 ------------ pages/index.tsx | 92 ++++++------------- .../apps/[appid].tsx => saml/index.tsx} | 18 +--- pages/saml/sso.tsx | 0 types/index.ts | 4 +- utils/index.ts | 5 - 12 files changed, 56 insertions(+), 237 deletions(-) delete mode 100644 pages/api/apps/metadata/index.ts rename pages/api/{apps => saml}/metadata/download.ts (77%) delete mode 100644 pages/apps/[id].tsx delete mode 100644 pages/apps/index.tsx rename pages/{saml2/apps/[appid].tsx => saml/index.tsx} (56%) create mode 100644 pages/saml/sso.tsx diff --git a/README.md b/README.md index 3894886..73221e1 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,12 @@ - Parse the SAML Request - Create the SAML Response - Fix the certificate +- Install prettify + +// Start a session +// Store the RelayState in the session +// Parse the SAMLRequest +// Validate the SAMLRequest +// Create SAMLResponse +// POST the SAMLResponse to ACS URL +// Remove the RelayState from the session \ No newline at end of file diff --git a/lib/env.ts b/lib/env.ts index e13b9ed..512e57c 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -1,6 +1,9 @@ -const config = { - appUrl: process.env.APP_URL || 'http://localhost:4000', - entityId: process.env.ENTITY_ID || 'http://saml.example.com', -} +const appUrl = process.env.APP_URL || 'http://localhost:4000'; +const entityId = process.env.ENTITY_ID || 'http://saml.example.com'; +const ssoUrl = `${appUrl}/saml/sso`; -export default config; \ No newline at end of file +export default { + appUrl, + entityId, + ssoUrl, +}; \ No newline at end of file diff --git a/pages/_app.tsx b/pages/_app.tsx index 55eb596..b5a75b0 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,13 +1,9 @@ import '../styles/globals.css' import type { AppProps } from 'next/app' -import Layout from '../components/Layout' -import 'rsuite/dist/rsuite.min.css'; function MyApp({ Component, pageProps }: AppProps) { return ( - - - + ) } diff --git a/pages/api/apps/metadata/index.ts b/pages/api/apps/metadata/index.ts deleted file mode 100644 index 850534a..0000000 --- a/pages/api/apps/metadata/index.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next'; -import { createCertificate, createIdPSSOUrl } from '../../../../utils'; -import { IdPMetadata } from '../../../../types'; -import config from '../../../../lib/env' - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse -) { - - switch (req.method) { - case 'GET': - return await getMetadata(); - default: - return res.status(405).end(`Method ${req.method} Not Allowed`); - } - - // Get metadata for an app - async function getMetadata() { - //const {id} = req.query; - const appId = '0480c44e-f200-4f72-8af0-a5a57611fd2d'; - - const metadata = { - certificate: await createCertificate(), - fingerprint: '', - sso_url: createIdPSSOUrl(appId), - entity_id: config.entityId, - } - - return res.json(metadata); - } -} \ No newline at end of file diff --git a/pages/api/apps/metadata/download.ts b/pages/api/saml/metadata/download.ts similarity index 77% rename from pages/api/apps/metadata/download.ts rename to pages/api/saml/metadata/download.ts index 984f461..3fe59a9 100644 --- a/pages/api/apps/metadata/download.ts +++ b/pages/api/saml/metadata/download.ts @@ -1,5 +1,5 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { createCertificate, createIdPMetadataXML, createIdPSSOUrl } from '../../../../utils'; +import { createCertificate, createIdPMetadataXML } from '../../../../utils'; import { IdPMetadata } from '../../../../types'; import stream from 'stream'; import { promisify } from 'util'; @@ -19,18 +19,16 @@ export default async function handler( return res.status(405).end(`Method ${req.method} Not Allowed`); } - // Download metadata for an app + // Download metadata async function downloadMetadata() { - const appId = '0480c44e-f200-4f72-8af0-a5a57611fd2d'; - const xml = await createIdPMetadataXML({ idpEntityId: config.entityId, - idpSsoUrl: createIdPSSOUrl(appId), + idpSsoUrl: config.ssoUrl, certificate: await createCertificate(), }); res.setHeader('Content-type', 'text/xml'); - res.setHeader('Content-Disposition', 'attachment; filename=metadata.xml'); + res.setHeader('Content-Disposition', 'attachment; filename=mock-saml-metadata.xml'); await pipeline(xml, res); } diff --git a/pages/apps/[id].tsx b/pages/apps/[id].tsx deleted file mode 100644 index 34cd6a2..0000000 --- a/pages/apps/[id].tsx +++ /dev/null @@ -1,44 +0,0 @@ -import prisma from '../../lib/prisma'; -import { GetServerSideProps } from 'next'; -import { App } from '../../types'; -import axios from 'axios'; -import { IdPMetadata } from '../../types'; -import React, { ChangeEvent, FormEvent, useState } from 'react'; - -// TODO: Remove this -export const getServerSideProps: GetServerSideProps = async ({ params }) => { - const app = await prisma.app.findUnique({ - where: { - id: params?.id, - } - }); - - const metadata = await axios.get('http://localhost:4000/api/apps/metadata'); - - return { - props: { - app, - metadata: metadata.data, - }, - }; -}; - -const ShowApp: React.FC<{app: App, metadata: IdPMetadata}> = ({app, metadata}) => { - return ( -
-

Id: {app.id}

-

name: {app.name}

-

acs_url: {app.acs_url}

-

entity_id: {app.entity_id}

- - Metadata -

sso_url: {metadata.sso_url}

-

entity_id: {metadata.entity_id}

-

certificate: {metadata.certificate}

- - Download Metadata -
- ); -}; - -export default ShowApp; \ No newline at end of file diff --git a/pages/apps/index.tsx b/pages/apps/index.tsx deleted file mode 100644 index 7d5f913..0000000 --- a/pages/apps/index.tsx +++ /dev/null @@ -1,60 +0,0 @@ -import axios from 'axios'; -import type { NextPage } from 'next'; -import { ChangeEvent, FormEvent, useState } from 'react'; -import Router from 'next/router'; - -const Apps: NextPage = () => { - const [formData, setFormData] = useState({ - name: null, - acs_url: null, - entity_id: null, - }); - - const handleInputChange = (e: ChangeEvent) => { - setFormData({ - ...formData, - [e.target.name]: e.target.value.trim() - }); - } - - const createApp = async (e: FormEvent) => { - e.preventDefault(); - - const { data: app } = await axios.post('/api/apps', { - ...formData - }); - - await Router.push(`/apps/${app.id}`); - }; - - return ( -
-
-
- -
- -
- -
- -
- -
- - -
-
- ); -}; - -export default Apps; diff --git a/pages/index.tsx b/pages/index.tsx index 72a4a59..b640b90 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,70 +1,34 @@ import type { NextPage } from 'next' -import Head from 'next/head' -import Image from 'next/image' -import styles from '../styles/Home.module.css' +import { GetServerSideProps } from 'next'; +import { IdPMetadata } from '../types' +import config from '../lib/env'; +import {createCertificate} from '../utils' +import React from 'react'; +import Link from 'next/link' -const Home: NextPage = () => { +export const getServerSideProps: GetServerSideProps = async () => { + const metadata: IdPMetadata = { + ssoUrl: config.ssoUrl, + entityId: config.entityId, + certificate: await createCertificate(), + } + + return { + props: { + metadata + }, + }; +}; + +const Home: React.FC<{metadata: IdPMetadata}> = ({ metadata }) => { return ( -
- - Create Next App - - - - -
-

- Welcome to Next.js! -

- -

- Get started by editing{' '} - pages/index.tsx -

- - -
- - +
+ Mock IdP Metadata +

SSO URL: {metadata.ssoUrl}

+

Entity ID: {metadata.entityId}

+

Certificate: {metadata.certificate}

+

+

Download Metadata

) } diff --git a/pages/saml2/apps/[appid].tsx b/pages/saml/index.tsx similarity index 56% rename from pages/saml2/apps/[appid].tsx rename to pages/saml/index.tsx index 011406c..39d263c 100644 --- a/pages/saml2/apps/[appid].tsx +++ b/pages/saml/index.tsx @@ -1,11 +1,11 @@ import type { GetServerSideProps } from 'next'; import React from "react"; -import { AuthNRequest } from '../../../types' -import {extractSAMLRequestAttributes} from '../../../utils' +import { AuthNRequest } from '../../types' +import { extractSAMLRequestAttributes } from '../../utils' export const getServerSideProps: GetServerSideProps = async ({query, params}) => { const relayState = query.RelayState as string; - const samlRequest = query.SAMLRequest as string; + const samlRequest = query.SAMLRequest as string; const attributes = await extractSAMLRequestAttributes(samlRequest); @@ -20,19 +20,9 @@ export const getServerSideProps: GetServerSideProps = async ({query, params}) => } const ProcessRequest: React.FC = ({relayState, samlRequest}) => { - - return (
Process Request
); } -export default ProcessRequest; - -// Start a session -// Store the RelayState in the session -// Parse the SAMLRequest -// Validate the SAMLRequest -// Create SAMLResponse -// POST the SAMLResponse to ACS URL -// Remove the RelayState from the session \ No newline at end of file +export default ProcessRequest; \ No newline at end of file diff --git a/pages/saml/sso.tsx b/pages/saml/sso.tsx new file mode 100644 index 0000000..e69de29 diff --git a/types/index.ts b/types/index.ts index 98ddc3d..9a7acd8 100644 --- a/types/index.ts +++ b/types/index.ts @@ -4,8 +4,8 @@ export type ServiceProvider = { }; export type IdentityProvider = { - sso_url: string; - entity_id: string; + ssoUrl: string; + entityId: string; }; export type App = { diff --git a/utils/index.ts b/utils/index.ts index bb8d93b..26dc92c 100644 --- a/utils/index.ts +++ b/utils/index.ts @@ -76,10 +76,6 @@ const createSAMLResponseXML = async (user: User): Promise => { .replace('user_lastName', 'K'); }; -const createIdPSSOUrl = (appId: string) => { - return `${config.appUrl}/saml2/apps/${appId}`; -} - export { parseXML, extractSAMLRequestAttributes, @@ -87,5 +83,4 @@ export { createSAMLResponseXML, createCertificate, extractCert, - createIdPSSOUrl, }; \ No newline at end of file