Skip to content

Commit

Permalink
Finish things up
Browse files Browse the repository at this point in the history
  • Loading branch information
cb1kenobi committed Mar 1, 2024
1 parent 0183702 commit aa14af9
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/app/api/cla/sign/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CLA_VERISON } from '@/lib/cla-constants';
import { claDir, createPDF, CreatePDFData } from '@/lib/cla';
import { join } from 'node:path';
import { existsSync } from 'node:fs';
import { unlink, writeFile } from 'node:fs/promises';
import { rename, unlink, writeFile } from 'node:fs/promises';
import { getServerSession } from 'next-auth/next';
import type { ExtendedProfile } from '@/lib/auth';
import { tmpdir } from 'node:os';
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function POST(req: Request) {
}

const pdfFile = await createPDF(data);
await writeFile(destPdf, pdfFile);
await rename(pdfFile, destPdf);

const claInfo = {
username: data.githubUsername,
Expand Down
19 changes: 2 additions & 17 deletions src/app/contribute/page.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import ContributeCode from '@/components/cla/contribute-code';
import InterestedInContributing from '@/components/cla/interested';
import CLAForm from '@/components/cla/form';
import DownloadCLA from '@/components/cla/download';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { checkCLA } from '@/lib/cla';
import { SIMULATE_NOT_AUTHENTICATED } from '@/lib/cla-constants';
import CLASession from '@/components/cla/session';
import type { Metadata } from 'next';
import type { ExtendedProfile } from '@/lib/auth';

export const metadata: Metadata = {
title: 'Contribute'
};

export default async function ContributePage() {
const session = await getServerSession(authOptions);
const user = !SIMULATE_NOT_AUTHENTICATED && session?.user ? session.user as ExtendedProfile : null;
const signed = user?.username && await checkCLA(user.username+'a') || false;

return (
<div className='md:w-3/4 w-full mx-auto'>
{signed && <DownloadCLA/>}
<InterestedInContributing/>
<ContributeCode/>
{!signed && <CLAForm user={user}/>}
<CLASession />
</div>
);
}
Expand Down
38 changes: 38 additions & 0 deletions src/components/cla/container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';

import ContributeCode from '@/components/cla/contribute-code';
import InterestedInContributing from '@/components/cla/interested';
import CLAForm from '@/components/cla/form';
import DownloadCLA from '@/components/cla/download';
import { SIMULATE_NOT_AUTHENTICATED, SIMULATE_NOT_SIGNED } from '@/lib/cla-constants';
import { useSession } from 'next-auth/react';
import { useEffect, useState } from 'react';
import type { CLASignedInfo } from '@/lib/cla-types';
import type { ExtendedProfile } from '@/lib/auth';

export default function CLAContainer() {
let { data: session, status } = useSession();
let [claInfo, setCLAInfo] = useState<CLASignedInfo | null>(null);
const user = !SIMULATE_NOT_AUTHENTICATED && status === 'authenticated' && session?.user ? session.user as ExtendedProfile : null;

useEffect(() => {
fetch('/api/cla')
.then(res => res.json())
.then((data: CLASignedInfo) => {
if (!SIMULATE_NOT_SIGNED) {
return setCLAInfo(data);
}
})
.catch(console.error);
}, []);

return (
<>
{claInfo?.signed && <DownloadCLA/>}
<InterestedInContributing/>
<ContributeCode/>
{!claInfo?.signed && <CLAForm onSign={setCLAInfo} user={user}/>}
</>
);
}

6 changes: 3 additions & 3 deletions src/components/cla/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { ExtendedProfile } from '@/lib/auth';
import PDFPage from '@/components/cla/pdf-page';
import PDFSignaturePage from '@/components/cla/pdf-signature-page';
import type { PDFPageProxy } from 'pdfjs-dist';
import type { OnSignCallback } from '@/lib/cla-types';

export default function CLAForm({ user }: { user: ExtendedProfile | null }) {
export default function CLAForm({ onSign, user }: { onSign: OnSignCallback, user: ExtendedProfile | null }) {
const [pdfDoc, setPDFDoc] = useState<PDFDocument | null>(null);
const [pdfPages, setPDFPages] = useState<PDFPageProxy[] | null>(null);

Expand Down Expand Up @@ -40,8 +41,7 @@ export default function CLAForm({ user }: { user: ExtendedProfile | null }) {
<p className='mb-10'>Please carefully read the agreement. On the last page, login into GitHub, fill out your information, sign, and submit.</p>
{pdfDoc && pdfPages?.map((page, i, arr) => {
if (i + 1 === arr.length) {
// onSign={onSign}
return <PDFSignaturePage key={`page_${i}`} pageIdx={i} page={page} pdfDoc={pdfDoc} user={user} />;
return <PDFSignaturePage onSign={onSign} key={`page_${i}`} pageIdx={i} page={page} pdfDoc={pdfDoc} user={user} />;
}
return <PDFPage key={`page_${i}`} pageIdx={i} page={page}/>;
})}
Expand Down
10 changes: 6 additions & 4 deletions src/components/cla/pdf-signature-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import type { ExtendedProfile } from '@/lib/auth';
import type { PDFPageProxy, RenderTask } from 'pdfjs-dist';
import type { MouseEvent } from 'react';
import type { Signature } from '@/lib/cla-types';
import type { OnSignCallback } from '@/lib/cla-types';

interface PDFPageParams {
page: PDFPageProxy;
pageIdx: number;
}

interface PDFSignaturePageParams extends PDFPageParams {
// onSign: OnSignCallback;
onSign: OnSignCallback;
pdfDoc: PDFDocument;
user?: ExtendedProfile | null;
}
Expand All @@ -26,7 +27,7 @@ interface AlertMessage {
type: 'info' | 'error';
}

export default function PDFSignaturePage({ /*onSign,*/ page, pageIdx, pdfDoc, user }: PDFSignaturePageParams) {
export default function PDFSignaturePage({ onSign, page, pageIdx, pdfDoc, user }: PDFSignaturePageParams) {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
const renderTaskRef = useRef<RenderTask | null>(null);
const formRef = useRef<HTMLFormElement | null>(null);
Expand Down Expand Up @@ -154,8 +155,9 @@ export default function PDFSignaturePage({ /*onSign,*/ page, pageIdx, pdfDoc, us
if (!res.ok) {
throw new Error((await res.json()).message);
}
console.log(await res.json());
// onSign(await res.json());
const claInfo = await res.json();
// console.log(claInfo);
onSign(claInfo);
window.scrollTo({
behavior: 'smooth',
top: 0
Expand Down
13 changes: 13 additions & 0 deletions src/components/cla/session.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use client';

import CLAContainer from '@/components/cla/container';
import { SessionProvider } from 'next-auth/react';

export default function CLASession() {
return (
<SessionProvider>
<CLAContainer/>
</SessionProvider>
);
}

1 change: 0 additions & 1 deletion src/components/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Link from 'next/link';
import Header from './header';
import Footer from './footer';

Expand Down
3 changes: 0 additions & 3 deletions src/lib/cla-constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// set to `true` to show the CLA form
export const CLA_ENABLED = true;

// set to `true` to show `sign into github`
export const SIMULATE_NOT_AUTHENTICATED = false;

Expand Down
16 changes: 16 additions & 0 deletions src/lib/cla-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@ export interface Signature {
trimmed?: string;
text?: string;
}

export interface CLAInfo {
username: string;
name: string;
title: string;
company: string,
email: string;
date: string;
claVersion: string;
}

export interface CLASignedInfo extends Partial<CLAInfo> {
signed: boolean;
}

export type OnSignCallback = (claInfo: CLASignedInfo | null) => void;
13 changes: 2 additions & 11 deletions src/lib/cla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@ import { dirname, join, resolve } from 'node:path';
import { tmpdir } from 'node:os';
import { CONTRIBUTOR_PDF } from './cla-constants';
import signPDF from './cla-sign-pdf';
import type { CLAInfo } from './cla-types';

const __dirname = dirname(fileURLToPath(import.meta.url));
export const claDir = resolve(__dirname, '../../cla');
export const certsDir = resolve(__dirname, '../../certs');

interface CLAInfo {
username: string;
name: string;
title: string;
company: string,
email: string;
date: string;
claVersion: string;
}

interface CLACache {
[username: string]: {
info: CLAInfo;
Expand Down Expand Up @@ -172,7 +163,7 @@ export async function createPDF({
await signPDF({
unsignedFile,
signedFile,
keyFile: join(certsDir, 'tidev.io.key'),
keyFile: join(certsDir, 'tidev.io.rsa.pkcs1.key'),
certFile: join(certsDir, 'tidev.io.crt'),
signatureFile,
page: '-1',
Expand Down
4 changes: 4 additions & 0 deletions vendor/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
`open-pdf-sign.jar`

This file copied from https://github.com/open-pdf-sign/open-pdf-sign/releases/tag/v0.1.7 which is licensed under the Apache 2.0 license.

NOTE: This library needs an PKCS1 formatted RSA private key. To convert a PKCS8 private key to PKCS1, run:

openssl rsa -in tidev.io.key -out tidev.io.pkcs1.key

0 comments on commit aa14af9

Please sign in to comment.