-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Web3 Authentication Integration (#53)
- Loading branch information
Showing
40 changed files
with
672 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
NEXT_PUBLIC_COMETH_API_KEY=your-public-cometh-api-key | ||
|
||
|
||
NEXT_PUBLIC_GOOGLE_API_KEY=your-google-api-key | ||
|
||
NEXTAUTH_URL=http://localhost:3000 | ||
NEXTAUTH_SECRET=your-nextauth-secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import NextAuth from 'next-auth/next'; | ||
import authOptions from '@/lib/next-auth/web3-provider/auth-options'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
// Below duplicated interface from next-auth/next because it's not exported | ||
interface RouteHandlerContext { | ||
params: { nextauth: string[] } | ||
} | ||
|
||
const handler = async function auth(req: NextRequest, context: RouteHandlerContext) { | ||
const isDefaultSigninPage = | ||
req.method === "GET" && req?.nextUrl.search.includes('signin'); | ||
|
||
// Below to skip showing the default signin page | ||
// because it is handled via web3 provider | ||
if (isDefaultSigninPage) { | ||
authOptions.providers.pop(); | ||
} | ||
|
||
return NextAuth(req, context, authOptions); | ||
} | ||
|
||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import crypto from 'crypto'; | ||
import { z } from 'zod'; | ||
import { isAddress } from 'ethers'; | ||
import { validateBody } from '@/lib/util/auth'; | ||
import { upsertUserNonce, getUserWithNonce, createUserWithNonce } from '@muqa/db'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
const WalletNonceRequestSchema = z.object({ | ||
address: z.string().refine(isAddress, { message: 'Invalid address' }) | ||
}); | ||
|
||
export type WalletNonceRequestDTO = z.infer<typeof WalletNonceRequestSchema>; | ||
export type WalletNonceResponse = { | ||
nonce: string; | ||
} | ||
|
||
const NONCE_EXPIRATION = 60 * 60 * 1000; // 1 hour | ||
|
||
function generateNonceData() { | ||
const nonce = crypto.randomBytes(32).toString('hex'); | ||
const expiresAt = new Date(Date.now() + NONCE_EXPIRATION); | ||
return { nonce, expiresAt }; | ||
} | ||
|
||
export async function POST(req: NextRequest) { | ||
const body = await req.json(); | ||
const result = validateBody(body, WalletNonceRequestSchema); | ||
if (result.error) { | ||
return NextResponse.json({ error: result.error.message }, { status: 400 }); | ||
} | ||
|
||
const { address } = result.data as WalletNonceRequestDTO; | ||
|
||
const user = await getUserWithNonce(address); | ||
const nonceData = generateNonceData(); | ||
|
||
if (user) { | ||
await upsertUserNonce(user, nonceData); | ||
} else { | ||
await createUserWithNonce(address, nonceData); | ||
} | ||
|
||
return NextResponse.json({ nonce: nonceData.nonce }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import axios from 'axios'; | ||
|
||
const BASE_URL = 'https://api.connect.cometh.io'; | ||
const API_KEY = process.env.COMETH_API_KEY; | ||
|
||
type verifySignatureResponse = { | ||
success: boolean, | ||
result: boolean | ||
} | ||
|
||
const api = axios.create({ | ||
baseURL: BASE_URL, | ||
headers: { common: { | ||
apikey: API_KEY | ||
}} | ||
}); | ||
|
||
export async function verifySignature( | ||
address: string, | ||
message: string, | ||
signature: string | ||
): Promise<verifySignatureResponse> { | ||
const body = { message, signature }; | ||
const response = await api.post(`/wallets/${address}/is-valid-signature`, body); | ||
return response.data; | ||
} |
24 changes: 24 additions & 0 deletions
24
apps/platform/src/lib/next-auth/web3-provider/auth-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { prisma } from '@muqa/db'; | ||
import { AuthOptions } from 'next-auth';; | ||
import { PrismaAdapter } from '@next-auth/prisma-adapter'; | ||
import Web3CredentialsProvider from './provider'; | ||
|
||
const authOptions: AuthOptions = { | ||
providers: [ | ||
Web3CredentialsProvider | ||
], | ||
adapter: PrismaAdapter(prisma), | ||
session: { | ||
strategy: 'jwt', | ||
}, | ||
secret: process.env.NEXTAUTH_SECRET, | ||
callbacks: { | ||
async session({ session, token }: { session: any; token: any }) { | ||
session.address = token.sub | ||
session.user.name = token.sub | ||
return session | ||
}, | ||
}, | ||
}; | ||
|
||
export default authOptions; |
34 changes: 34 additions & 0 deletions
34
apps/platform/src/lib/next-auth/web3-provider/authorize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { deleteUserNonce, getUserWithNonce } from '@muqa/db'; | ||
import { RequestInternal } from 'next-auth';; | ||
import { verifySignature } from '@/lib/cometh/api'; | ||
|
||
export default async function authorize( | ||
credentials: Record<'address' | 'signedNonce', string> | undefined, | ||
req: Pick<RequestInternal, 'body' | 'headers' | 'method' | 'query'> | ||
) { | ||
if (!credentials) return null; | ||
|
||
const { address, signedNonce } = credentials; | ||
|
||
// Get user from database with their generated nonce | ||
const user = await getUserWithNonce(address); | ||
|
||
if (!user?.authNonce) return null; | ||
|
||
// Check nonce signature against Cometh's api | ||
const verification = await verifySignature( | ||
address, user.authNonce.nonce, signedNonce); | ||
|
||
if (!verification.result) return null; | ||
|
||
// Check that the nonce is not expired | ||
if (user.authNonce.expiresAt < new Date()) return null; | ||
|
||
// Everything is fine, clear the nonce and return the user | ||
await deleteUserNonce(user); | ||
|
||
return { | ||
id: user.id, | ||
address: user.address, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import CredentialsProvider from 'next-auth/providers/credentials'; | ||
import authorize from './authorize'; | ||
|
||
const Web3CredentialsProvider = CredentialsProvider({ | ||
name: 'Web3 Credentials Auth', | ||
credentials: { | ||
address: { label: 'Public Address', type: 'text' }, | ||
signedNonce: { label: 'Signed Nonce', type: 'text' }, | ||
}, | ||
authorize, | ||
}); | ||
|
||
export default Web3CredentialsProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ZodSchema } from 'zod'; | ||
|
||
export function validateBody(body: any, schema: ZodSchema) { | ||
return schema.safeParse(body); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.