|
1 | 1 | import middy from "@middy/core"; |
2 | 2 | import jsonBodyParser from "@middy/http-json-body-parser"; |
3 | | -import { ETH_SIGNATURE_REGEX } from "src/consts"; |
| 3 | +import { ETH_SIGNATURE_REGEX } from "consts/index"; |
| 4 | +import { DEFAULT_CHAIN } from "consts/chains"; |
4 | 5 | import { SiweMessage } from "siwe"; |
5 | 6 | import * as jwt from "jose"; |
6 | 7 | import { createClient } from "@supabase/supabase-js"; |
7 | | -import { Database } from "../../src/types/supabase-notification"; |
| 8 | +import { netlifyUri } from "src/generatedNetlifyInfo.json"; |
| 9 | +import { Database } from "src/types/supabase-notification"; |
8 | 10 |
|
9 | 11 | const authUser = async (event) => { |
10 | 12 | try { |
@@ -32,15 +34,30 @@ const authUser = async (event) => { |
32 | 34 | } |
33 | 35 |
|
34 | 36 | const siweMessage = new SiweMessage(message); |
35 | | - const lowerCaseAddress = siweMessage.address.toLowerCase(); |
36 | 37 |
|
37 | | - if (siweMessage.address.toLowerCase() !== address.toLowerCase()) { |
| 38 | + if (netlifyUri && netlifyUri !== siweMessage.uri) { |
| 39 | + console.debug(`Invalid URI: expected ${netlifyUri} but got ${siweMessage.uri}`); |
| 40 | + throw new Error(`Invalid URI`); |
| 41 | + } |
| 42 | + |
| 43 | + if (siweMessage.chainId !== DEFAULT_CHAIN) { |
| 44 | + console.debug(`Invalid chain ID: expected ${DEFAULT_CHAIN} but got ${siweMessage.chainId}`); |
| 45 | + throw new Error(`Invalid chain ID`); |
| 46 | + } |
| 47 | + |
| 48 | + if (!siweMessage.expirationTime || Date.parse(siweMessage.expirationTime) < Date.now()) { |
| 49 | + console.debug(`Message expired: ${siweMessage.expirationTime} < ${new Date().toISOString()}`); |
| 50 | + throw new Error("Message expired"); |
| 51 | + } |
| 52 | + |
| 53 | + const lowerCaseAddress = siweMessage.address.toLowerCase(); |
| 54 | + if (lowerCaseAddress !== address.toLowerCase()) { |
38 | 55 | throw new Error("Address mismatch in provided address and message"); |
39 | 56 | } |
40 | 57 |
|
41 | 58 | const supabase = createClient<Database>(process.env.SUPABASE_URL!, process.env.SUPABASE_CLIENT_API_KEY!); |
42 | 59 |
|
43 | | - // get nonce from db, if its null that means it was alrd used |
| 60 | + // get nonce from db, if its null that means it was already used |
44 | 61 | const { error: nonceError, data: nonceData } = await supabase |
45 | 62 | .from("user-nonce") |
46 | 63 | .select("nonce") |
|
0 commit comments