Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bfx auth token refreshing #336

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions workers/loc.api/sync/authenticator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,19 +1082,25 @@ class Authenticator {
} = user ?? {}
const tokenKey = this._getTokenKeyByEmailField(user)

const userSession = this.userSessions.get(token) ?? {}
const authTokenRefreshInterval = authToken
? this.setupAuthTokenRefreshInterval(user)
: null

this.userSessions.set(
token, {
...user,
authTokenFn: () => {
return this.userSessions.get(token)?.authToken
},
authTokenRefreshInterval,
authTokenInvalidateIntervals: new Map()
}
token,
Object.assign(
userSession,
user,
{
authTokenFn: () => {
return this.userSessions.get(token)?.authToken
},
authTokenRefreshInterval,
authTokenInvalidateIntervals: userSession
?.authTokenInvalidateIntervals ?? new Map()
}
)
)
this.userTokenMapByEmail.set(tokenKey, token)
}
Expand Down Expand Up @@ -1133,9 +1139,8 @@ class Authenticator {
? token
: this.userTokenMapByEmail.get(tokenKey)

this.userTokenMapByEmail.delete(tokenKey)

const session = this.userSessions.get(_token) ?? {}
this.userTokenMapByEmail.delete(this._getTokenKeyByEmailField(session))
const {
authTokenRefreshInterval,
authTokenInvalidateIntervals = new Map()
Expand Down Expand Up @@ -1272,8 +1277,12 @@ class Authenticator {
const { authTokenInvalidateIntervals } = userSession
let count = 0

if (authTokenInvalidateIntervals.has(authToken)) {
return
}

const authTokenInvalidateInterval = setInterval(async () => {
const session = this.userSessions.get(token)
const session = this.userSessions.get(token) ?? userSession

try {
count += 1
Expand All @@ -1288,7 +1297,7 @@ class Authenticator {
} catch (err) {
if (count >= 3) {
clearInterval(authTokenInvalidateInterval)
session.authTokenInvalidateIntervals?.delete(authToken)
session?.authTokenInvalidateIntervals?.delete(authToken)
}

this.logger.debug(err)
Expand Down