-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
johannbm
committed
Sep 4, 2023
1 parent
63ea2dc
commit fc6d16a
Showing
7 changed files
with
115 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as crypto from "node:crypto"; | ||
import { IncomingMessage } from "node:http"; | ||
|
||
import NodeCache from "node-cache"; | ||
|
||
export const sessionCache = new NodeCache({ | ||
stdTTL: 60 * 60, // 1 hour | ||
}); | ||
|
||
type SessionCacheValue = { | ||
[scope: string]: OboToken; | ||
}; | ||
|
||
type OboToken = { | ||
expiresAt: number; | ||
accessToken: string; | ||
refreshToken: string; | ||
}; | ||
|
||
export function setOboTokenForRequest( | ||
request: IncomingMessage, | ||
oboToken: OboToken, | ||
scope: string, | ||
) { | ||
const hashedAuthHeader = getHashedAuthHeader(request); | ||
|
||
if (!hashedAuthHeader) { | ||
return; | ||
} | ||
|
||
const cachedValue = sessionCache.get<SessionCacheValue>(hashedAuthHeader); | ||
|
||
if (!cachedValue) { | ||
return; | ||
} | ||
|
||
cachedValue[scope] = oboToken; | ||
|
||
sessionCache.set<SessionCacheValue>(hashedAuthHeader, cachedValue); | ||
} | ||
|
||
export function getOboTokenForRequest(request: IncomingMessage, scope: string) { | ||
const hashedAuthHeader = getHashedAuthHeader(request); | ||
|
||
if (!hashedAuthHeader) { | ||
return; | ||
} | ||
|
||
const cachedValue = sessionCache.get<SessionCacheValue>(hashedAuthHeader); | ||
|
||
if (!cachedValue) { | ||
return; | ||
} | ||
|
||
return cachedValue[scope]; | ||
} | ||
|
||
function getHashedAuthHeader(request: IncomingMessage) { | ||
const authToken = request.headers["authorization"]; | ||
|
||
if (!authToken) { | ||
return; | ||
} | ||
|
||
return crypto.createHash("md5").update(authToken).digest("hex"); | ||
} |