feat(SW-68): refactor digital signature
This commit is contained in:
@@ -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`} />
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user