mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Withdrew the sessionId from the JWT.
This commit is contained in:
parent
6ab381a6b1
commit
e76646b327
@ -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')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user