Skip to content

Commit

Permalink
Added debug case for auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Mar 14, 2023
1 parent e450d74 commit 4f1a674
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ulms/api-clients",
"version": "5.14.1",
"version": "5.15.0",
"description": "JavaScript API clients for ULMS platform",
"keywords": [],
"homepage": "https://github.com/foxford/ulms-api-clients-js#readme",
Expand Down
33 changes: 33 additions & 0 deletions src/basic-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BasicClient {
this.httpClient = httpClient
this.tokenProvider = tokenProvider
this.labels = {}
this.trackEvent = undefined
}

url(endpoint, parameters) {
Expand Down Expand Up @@ -76,6 +77,10 @@ class BasicClient {
}
}

setTrackEventFunction(trackEvent) {
this.trackEvent = trackEvent
}

clearLabels() {
this.labels = {}
}
Expand Down Expand Up @@ -110,6 +115,34 @@ class BasicClient {
}
const requestOptions = { ...options, headers }

// [debug section] start
if (this.trackEvent) {
const { exp } = JSON.parse(atob(token.split('.')[1]))
const requestStart = Date.now()
const expiresAtLocal = this.tokenProvider.tokenData
? this.tokenProvider.tokenData.expires_ts
: undefined
const result = this.httpClient.post(url, data, requestOptions)

result.catch((error) => {
const responseEnd = Date.now()

const eventPayload = {
exp: exp * 1000,
expiresAtLocal,
requestStart,
responseEnd,
}

if (error && error.type === 'invalid_authentication') {
this.trackEvent(eventPayload)
}
})

return result
}
// [debug section] end

return this.httpClient.post(url, data, requestOptions)
}

Expand Down
12 changes: 10 additions & 2 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,16 @@ class Dispatcher extends BasicClient {
* @returns {Promise}
*/
updatePosition(kind, classId, position) {
return this.post(`${this.baseUrl}/${kind}/${classId}/timestamps`, {
position,
const controller = new AbortController()
const { signal } = controller
const timeoutId = setTimeout(() => controller.abort(), 10 * 1000)

return this.post(
`${this.baseUrl}/${kind}/${classId}/timestamps`,
{ position },
{ signal }
).finally(() => {
clearTimeout(timeoutId)
})
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TokenProvider {
this.httpClient = httpClient
this.tokenData = undefined
this.tokenP = undefined
this.tokenRequestStart = undefined
}

setContext(context) {
Expand All @@ -27,6 +28,7 @@ class TokenProvider {

if (isTokenDataEmpty || isAccessTokenExpired) {
this.tokenP = makeDeferred()
this.tokenRequestStart = Date.now()

this.fetchTokenData()
.then((response) => {
Expand All @@ -44,10 +46,20 @@ class TokenProvider {
}

fetchTokenData() {
const controller = new AbortController()
const { signal } = controller
const timeoutId = setTimeout(() => controller.abort(), 10 * 1000)
const qs = this.context ? `?context=${this.context}` : ''
const url = `${this.baseUrl}/api/user/ulms_token${qs}`

return this.httpClient.post(url, undefined, { credentials: 'include' })
return this.httpClient
.post(url, undefined, {
credentials: 'include',
signal,
})
.finally(() => {
clearTimeout(timeoutId)
})
}

rejectAndReset(error) {
Expand Down Expand Up @@ -85,17 +97,19 @@ class TokenProvider {
this.tokenP.reject(transformedError)

this.tokenP = undefined
this.tokenRequestStart = undefined
}

resolveAndReset() {
this.tokenP.resolve(this.tokenData.access_token)

this.tokenP = undefined
this.tokenRequestStart = undefined
}

updateTokenData(updates) {
const { expires_in } = updates
const expires_ts = Date.now() + expires_in * 1e3 - 10e3
const expires_ts = this.tokenRequestStart + expires_in * 1e3 - 20e3

this.tokenData = {
...this.tokenData,
Expand Down

0 comments on commit 4f1a674

Please sign in to comment.