Skip to content

Commit

Permalink
refactor(oidc): null coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
jafin committed Jul 10, 2024
1 parent e07f849 commit 2610212
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions packages/oidc-client-service-worker/src/OidcServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ const keepAliveAsync = async (event: FetchEvent) => {

async function generateDpopAsync(originalRequest: Request, currentDatabase:OidcConfig|null, url: string, extrasClaims={} ) {
const headersExtras = serializeHeaders(originalRequest.headers);
if (currentDatabase &&
currentDatabase.demonstratingProofOfPossessionConfiguration &&
if (currentDatabase?.demonstratingProofOfPossessionConfiguration &&
currentDatabase.demonstratingProofOfPossessionJwkJson &&
(!currentDatabase.demonstratingProofOfPossessionOnlyWhenDpopHeaderPresent || currentDatabase.demonstratingProofOfPossessionOnlyWhenDpopHeaderPresent && headersExtras['dpop'])
) {
Expand All @@ -125,11 +124,7 @@ const handleFetch = async (event: FetchEvent) => {
url,
trustedDomains,
);
if (
currentDatabaseForRequestAccessToken &&
currentDatabaseForRequestAccessToken.tokens &&
currentDatabaseForRequestAccessToken.tokens.access_token
) {
if (currentDatabaseForRequestAccessToken?.tokens?.access_token) {
while (
currentDatabaseForRequestAccessToken.tokens &&
!isTokensValid(currentDatabaseForRequestAccessToken.tokens)
Expand Down Expand Up @@ -205,7 +200,7 @@ const handleFetch = async (event: FetchEvent) => {
let newBody = actualBody;
for (let i = 0; i < numberDatabase; i++) {
const currentDb = currentDatabases[i];
if (currentDb && currentDb.tokens != null) {
if (currentDb?.tokens != null) {
const claimsExtras = {ath: await base64urlOfHashOfASCIIEncodingAsync(currentDb.tokens.access_token),};
headers = await generateDpopAsync(originalRequest, currentDb, url, claimsExtras);
const keyRefreshToken =
Expand Down Expand Up @@ -246,10 +241,7 @@ const handleFetch = async (event: FetchEvent) => {
integrity: clonedRequest.integrity,
});

if (
currentDatabase &&
currentDatabase.oidcServerConfiguration != null &&
currentDatabase.oidcServerConfiguration.revocationEndpoint &&
if (currentDatabase?.oidcServerConfiguration?.revocationEndpoint &&
url.startsWith(
normalizeUrl(
currentDatabase.oidcServerConfiguration.revocationEndpoint,
Expand All @@ -272,7 +264,7 @@ const handleFetch = async (event: FetchEvent) => {
// @ts-ignore
currentDatabase = database[currentLoginCallbackConfigurationName];
let newBody = actualBody;
if (currentDatabase && currentDatabase.codeVerifier != null) {
if (currentDatabase?.codeVerifier != null) {
newBody = replaceCodeVerifier(
newBody,
currentDatabase.codeVerifier,
Expand Down Expand Up @@ -435,9 +427,7 @@ const handleMessage = async (event: ExtendableMessageEvent) => {
if (tokens.refresh_token) {
tokens.refresh_token = TOKEN.REFRESH_TOKEN + '_' + configurationName;
}
if (
tokens.idTokenPayload &&
tokens.idTokenPayload.nonce &&
if (tokens?.idTokenPayload?.nonce &&
currentDatabase.nonce != null
) {
tokens.idTokenPayload.nonce =
Expand Down

0 comments on commit 2610212

Please sign in to comment.