Merged in feat/sw-2862-move-booking-router-to-trpc-package (pull request #2421)
feat(SW-2861): Move booking router to trpc package * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Move partners router to trpc package * Move autocomplete router to trpc package * Move booking router to trpc package * Merge branch 'master' into feat/sw-2862-move-booking-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
52
packages/trpc/lib/utils/encryption.ts
Normal file
52
packages/trpc/lib/utils/encryption.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import "server-only"
|
||||
|
||||
import crypto from "crypto"
|
||||
|
||||
import { env } from "../../env/server"
|
||||
|
||||
const algorithm = "DES-ECB"
|
||||
const encryptionKey = env.BOOKING_ENCRYPTION_KEY
|
||||
const bufferKey = Buffer.from(encryptionKey, "utf8")
|
||||
|
||||
export function encrypt(originalString: string) {
|
||||
try {
|
||||
const cipher = crypto.createCipheriv(algorithm, bufferKey, null)
|
||||
cipher.setAutoPadding(false)
|
||||
const bufferString = Buffer.from(originalString, "utf8")
|
||||
const paddingSize =
|
||||
bufferKey.length - (bufferString.length % bufferKey.length)
|
||||
const paddedStr = Buffer.concat([
|
||||
bufferString,
|
||||
Buffer.alloc(paddingSize, 0),
|
||||
])
|
||||
const buffers: Buffer[] = []
|
||||
buffers.push(cipher.update(paddedStr))
|
||||
buffers.push(cipher.final())
|
||||
const result = Buffer.concat(buffers).toString("base64")
|
||||
return result
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
export function decrypt(encryptedString: string) {
|
||||
try {
|
||||
const decipher = crypto.createDecipheriv(algorithm, bufferKey, null)
|
||||
decipher.setAutoPadding(false)
|
||||
const buffers: Buffer[] = []
|
||||
buffers.push(decipher.update(encryptedString, "base64"))
|
||||
buffers.push(decipher.final())
|
||||
const result = Buffer.concat(buffers)
|
||||
.toString("utf8")
|
||||
/*
|
||||
* Hexadecimal byte (null byte) replace. These occur when decrypting because
|
||||
* we're disabling the auto padding for historical/compatibility reasons.
|
||||
*/
|
||||
.replace(/(\x00)*/g, "")
|
||||
return result
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
34
packages/trpc/lib/utils/refId.ts
Normal file
34
packages/trpc/lib/utils/refId.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import "server-only"
|
||||
|
||||
import { decrypt, encrypt } from "./encryption"
|
||||
|
||||
export function calculateRefId(confirmationNumber: string, lastName: string) {
|
||||
const encryptedRefId = encrypt(`${confirmationNumber},${lastName}`)
|
||||
|
||||
return encryptedRefId
|
||||
}
|
||||
|
||||
export function parseRefId(refId: string) {
|
||||
// RefId is DES-ECB encryption + Base64 encoding. For legacy reasons we have
|
||||
// to do some manual handling here to get a proper Base64 string.
|
||||
//
|
||||
// - Use case: Current web replaced plus sign with hyphens when generating RefIds.
|
||||
// Handling: We replace hyphens with plus signs.
|
||||
//
|
||||
// - Use case: Incoming links in the wild do not encode the RefId properly.
|
||||
// Handling: We replace spaces with plus signs. Effectively, reversing the
|
||||
// decoding of plus signs into spaces that Next.js does for us for incoming
|
||||
// search params.
|
||||
// Slash and equal sign are not decoded into anything, so no action needed.
|
||||
// We only need to cater for those three (plus, slash, equals) as RefId is
|
||||
// Base64 encoded which only has these three special characters.
|
||||
const data = decrypt(refId.replace(/ |-/g, "+"))
|
||||
const parts = data.split(",")
|
||||
if (parts.length !== 2) {
|
||||
throw new Error("Invalid refId format")
|
||||
}
|
||||
return {
|
||||
confirmationNumber: parts[0],
|
||||
lastName: parts[1],
|
||||
}
|
||||
}
|
||||
26
packages/trpc/lib/utils/session.ts
Normal file
26
packages/trpc/lib/utils/session.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import "server-only"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
export function isValidSession(session: Session | null): session is Session {
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
if (session.error) {
|
||||
console.log(`Session error: ${session.error}`)
|
||||
return false
|
||||
}
|
||||
|
||||
const token = session.token
|
||||
|
||||
if (token?.error) {
|
||||
console.log(`Session token error: ${token.error}`)
|
||||
return false
|
||||
}
|
||||
if (token?.expires_at && token.expires_at < Date.now()) {
|
||||
console.log(`Session expired: ${session.token.expires_at}`)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user