Skip to content

Commit

Permalink
use des-ecb algo for encrypting uniqueSessionId
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Sep 23, 2022
1 parent 71846da commit 85d2d73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 49 deletions.
52 changes: 9 additions & 43 deletions backend/src/services/authentication-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AuthType } from "@common/enums"
import { SessionMeta } from "@common/types"
import { AppDataSource } from "data-source"
import { ApiTrace, AuthenticationConfig } from "models"
import { encrypt, generate_iv } from "utils/encryption"
import { encryptEcb } from "utils/encryption"

export class AuthenticationConfigService {
static async setSessionMetadata(apiTrace: ApiTrace) {
Expand All @@ -14,8 +14,6 @@ export class AuthenticationConfigService {
return
}
const key = process.env.ENCRYPTION_KEY
const encryptionKey = Buffer.from(key, "base64")
const keypairIv = generate_iv()

const requestHeaders = apiTrace.requestHeaders
const successfulAuth =
Expand All @@ -35,20 +33,12 @@ export class AuthenticationConfigService {
const decodedUser = Buffer.from(encodedValue, "base64")
?.toString()
?.split(":")[0]
const { encrypted, tag } = encrypt(
encodedValue,
encryptionKey,
keypairIv,
)
const encrypted = encryptEcb(encodedValue, key)
sessionMeta = {
authenticationProvided: true,
authenticationSuccessful: successfulAuth,
authType: authConfig.authType,
uniqueSession: {
key: encrypted,
iv: keypairIv.toString("base64"),
tag: tag.toString("base64"),
},
uniqueSessionKey: encrypted,
user: decodedUser,
}
}
Expand All @@ -57,61 +47,37 @@ export class AuthenticationConfigService {
const authHeader = authConfig.headerKey ?? ""
if (header.name.toLowerCase() === authHeader.toLowerCase()) {
const headerValue = header.value
const { encrypted, tag } = encrypt(
headerValue,
encryptionKey,
keypairIv,
)
const encrypted = encryptEcb(headerValue, key)
sessionMeta = {
authenticationProvided: true,
authenticationSuccessful: successfulAuth,
authType: authConfig.authType,
uniqueSession: {
key: encrypted,
iv: keypairIv.toString("base64"),
tag: tag.toString("base64"),
},
uniqueSessionKey: encrypted,
}
}
break
case AuthType.SESSION_COOKIE:
const cookieName = authConfig?.cookieName ?? ""
if (header.name.toLowerCase() === cookieName.toLowerCase()) {
const cookieValue = header.value
const { encrypted, tag } = encrypt(
cookieValue,
encryptionKey,
keypairIv,
)
const encrypted = encryptEcb(cookieValue, key)
sessionMeta = {
authenticationProvided: true,
authenticationSuccessful: successfulAuth,
authType: authConfig.authType,
uniqueSession: {
key: encrypted,
iv: keypairIv.toString("base64"),
tag: tag.toString("base64"),
},
uniqueSessionKey: encrypted,
}
}
break
case AuthType.JWT:
const jwtHeader = authConfig.headerKey ?? ""
if (header.name.toLowerCase() === jwtHeader.toLowerCase()) {
const { encrypted, tag } = encrypt(
header.value,
encryptionKey,
keypairIv,
)
const encrypted = encryptEcb(header.value, key)
sessionMeta = {
authenticationProvided: true,
authenticationSuccessful: successfulAuth,
authType: authConfig.authType,
uniqueSession: {
key: encrypted,
iv: keypairIv.toString("base64"),
tag: tag.toString("base64"),
},
uniqueSessionKey: encrypted,
}
const decodedPayload = JSON.parse(
Buffer.from(
Expand Down
9 changes: 9 additions & 0 deletions backend/src/utils/encryption.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from "crypto"

const algorithm = "aes-256-gcm"
const ecbAlgorithm = "des-ecb"

/** encrypts ascii/utf-8 text into a base64-encoded string */
const encrypt = (
Expand All @@ -14,6 +15,14 @@ const encrypt = (
return { encrypted: enc, tag: cipher.getAuthTag() }
}

export const encryptEcb = (text: string, key: string): string => {
const keyBuffer = Buffer.from(key, "base64").subarray(0, 8)
const cipher = crypto.createCipheriv(ecbAlgorithm, keyBuffer, null)
let enc = cipher.update(text, "utf8", "base64")
enc += cipher.final("base64")
return enc
}

/** decrypt decodes base64-encoded ciphertext into a utf8-encoded string */
const decrypt = (
encrypted: string,
Expand Down
8 changes: 2 additions & 6 deletions common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ export interface Meta {

export interface SessionMeta {
authenticationProvided: boolean
authenticationSuccessful?: boolean
authenticationSuccessful: boolean
authType: AuthType
uniqueSession?: {
key: string
iv: string
tag: string
}
uniqueSessionKey?: string
user?: string
}

Expand Down

0 comments on commit 85d2d73

Please sign in to comment.