feat(SW-68): refactor digital signature

This commit is contained in:
Fredrik Thorsson
2024-08-12 17:13:56 +02:00
parent 5c39a1dcbd
commit e7af67ca73
4 changed files with 19 additions and 23 deletions

View File

@@ -44,4 +44,4 @@ AUTH_URL="$PUBLIC_URL/api/web/auth"
NEXTAUTH_URL="$PUBLIC_URL/api/web/auth"
GOOGLE_STATIC_MAP_KEY=""
GOOGLE_STATIC_MAP_SECRET=""
GOOGLE_STATIC_MAP_SIGNATURE_SECRETT=""

View File

@@ -14,7 +14,7 @@ import { LangParams, PageArgs } from "@/types/params"
export default async function SelectHotelPage({
params,
}: PageArgs<LangParams>) {
const { formatMessage } = await getIntl()
const intl = await getIntl()
const { attributes } = await serverClient().hotel.getHotel({
hotelId: "d98c7ab1-ebaa-4102-b351-758daf1ddf55",
@@ -44,7 +44,7 @@ export default async function SelectHotelPage({
variant="underscored"
href="#"
>
{formatMessage({ id: "Show map" })}
{intl.formatMessage({ id: "Show map" })}
<ChevronRightIcon color="burgundy" className={styles.icon} />
</Link>
<HotelFilter filters={hotelFilters} />

View File

@@ -1,6 +1,5 @@
/* eslint-disable @next/next/no-img-element */
import crypto from "crypto"
import url from "url"
import crypto from "node:crypto"
import { env } from "@/env/server"
@@ -22,15 +21,6 @@ function encodeBase64Hash(key: Buffer, data: string) {
return crypto.createHmac("sha1", key).update(data).digest("base64")
}
function createRequestUrl(path: string, secret: string) {
const uri = new URL(path)
const safeSecret = decodeBase64Hash(removeWebSafe(secret))
const hashedSignature = makeWebSafe(
encodeBase64Hash(safeSecret, uri.pathname + uri.search)
)
return url.format(uri) + "&signature=" + hashedSignature
}
export default function StaticMap({
city,
width,
@@ -39,13 +29,18 @@ export default function StaticMap({
mapType,
}: StaticMapProps) {
const key = env.GOOGLE_STATIC_MAP_KEY
const secret = env.GOOGLE_STATIC_MAP_SECRET
const url = `https://maps.googleapis.com/maps/api/staticmap?center=${city}&zoom=${zoomLevel}&size=${width}x${height}&maptype=${mapType}&key=${key}`
const secret = env.GOOGLE_STATIC_MAP_SIGNATURE_SECRET
const safeSecret = decodeBase64Hash(removeWebSafe(secret))
return (
<img
src={createRequestUrl(url, secret)}
alt={`Map of ${city} city center`}
/>
const url = new URL(
`https://maps.googleapis.com/maps/api/staticmap?center=${city}&zoom=${zoomLevel}&size=${width}x${height}&maptype=${mapType}&key=${key}`
)
const hashedSignature = makeWebSafe(
encodeBase64Hash(safeSecret, url.pathname + url.search)
)
const src = url.toString() + "&signature=" + hashedSignature
return <img src={src} alt={`Map of ${city} city center`} />
}

5
env/server.ts vendored
View File

@@ -59,7 +59,7 @@ export const env = createEnv({
WEBVIEW_ENCRYPTION_KEY: z.string(),
BOOKING_ENCRYPTION_KEY: z.string(),
GOOGLE_STATIC_MAP_KEY: z.string(),
GOOGLE_STATIC_MAP_SECRET: z.string(),
GOOGLE_STATIC_MAP_SIGNATURE_SECRET: z.string(),
},
emptyStringAsUndefined: true,
runtimeEnv: {
@@ -105,6 +105,7 @@ export const env = createEnv({
WEBVIEW_ENCRYPTION_KEY: process.env.WEBVIEW_ENCRYPTION_KEY,
BOOKING_ENCRYPTION_KEY: process.env.BOOKING_ENCRYPTION_KEY,
GOOGLE_STATIC_MAP_KEY: process.env.GOOGLE_STATIC_MAP_KEY,
GOOGLE_STATIC_MAP_SECRET: process.env.GOOGLE_STATIC_MAP_SECRET,
GOOGLE_STATIC_MAP_SIGNATURE_SECRET:
process.env.GOOGLE_STATIC_MAP_SIGNATURE_SECRET,
},
})