Withdrew the sessionId from the JWT.

This commit is contained in:
elweyn 2021-11-10 06:13:29 +01:00
parent 6ab381a6b1
commit e76646b327
3 changed files with 5 additions and 17 deletions

View File

@ -13,15 +13,9 @@ const isAuthorized: AuthChecker<any> = async (
) => { ) => {
if (context.token) { if (context.token) {
const decoded = decode(context.token) const decoded = decode(context.token)
if (decoded.sessionId && decoded.sessionId !== 0) {
const result = await apiGet(
`${CONFIG.LOGIN_API_URL}checkSessionState?session_id=${decoded.sessionId}`,
)
context.sessionId = decoded.sessionId
context.pubKey = decoded.pubKey context.pubKey = decoded.pubKey
context.setHeaders.push({ key: 'token', value: encode(decoded.sessionId, decoded.pubKey) }) context.setHeaders.push({ key: 'token', value: encode(decoded.pubKey) })
return result.success return true
}
} }
throw new Error('401 Unauthorized') throw new Error('401 Unauthorized')
} }

View File

@ -2,27 +2,22 @@ import jwt, { JwtPayload } from 'jsonwebtoken'
import CONFIG from '../config/' import CONFIG from '../config/'
interface CustomJwtPayload extends JwtPayload { interface CustomJwtPayload extends JwtPayload {
sessionId: number
pubKey: Buffer pubKey: Buffer
} }
type DecodedJwt = { type DecodedJwt = {
token: string token: string
sessionId: number
pubKey: Buffer pubKey: Buffer
} }
export default (token: string): DecodedJwt => { export default (token: string): DecodedJwt => {
if (!token) throw new Error('401 Unauthorized') if (!token) throw new Error('401 Unauthorized')
let sessionId = null
let pubKey = null let pubKey = null
try { try {
const decoded = <CustomJwtPayload>jwt.verify(token, CONFIG.JWT_SECRET) const decoded = <CustomJwtPayload>jwt.verify(token, CONFIG.JWT_SECRET)
sessionId = decoded.sessionId
pubKey = decoded.pubKey pubKey = decoded.pubKey
return { return {
token, token,
sessionId,
pubKey, pubKey,
} }
} catch (err) { } catch (err) {

View File

@ -5,10 +5,9 @@ import jwt from 'jsonwebtoken'
import CONFIG from '../config/' import CONFIG from '../config/'
// Generate an Access Token // Generate an Access Token
export default function encode(sessionId: number, pubKey: Buffer): string { export default function encode(pubKey: Buffer): string {
const token = jwt.sign({ sessionId, pubKey }, CONFIG.JWT_SECRET, { const token = jwt.sign({ pubKey }, CONFIG.JWT_SECRET, {
expiresIn: CONFIG.JWT_EXPIRES_IN, expiresIn: CONFIG.JWT_EXPIRES_IN,
subject: sessionId.toString(),
}) })
return token return token
} }