Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
chore: improve oAuth related debug logging
Browse files Browse the repository at this point in the history
* Remove duplicate [OAuth] prefix
* Log successful token fetches
  • Loading branch information
nikku committed Jun 8, 2023
1 parent eb95a56 commit dbadd5c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/lib/OAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ export class OAuthProvider {

public async getToken(): Promise<string> {
if (this.tokenCache[this.clientId]) {
debug(`[OAuth] Using cached token from memory...`)
debug(`Using cached token from memory...`)
return this.tokenCache[this.clientId].access_token
}
if (this.useFileCache) {
const cachedToken = this.fromFileCache(this.clientId)
if (cachedToken) {
debug(`[OAuth] Using cached token from file...`)
debug(`Using cached token from file...`)
return cachedToken.access_token
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ export class OAuthProvider {
grant_type: 'client_credentials',
}

debug(`[OAuth] Requesting token from token endpoint...`)
debug(`Requesting token from token endpoint...`)
return got
.post(this.url, {
form,
Expand All @@ -169,6 +169,8 @@ export class OAuthProvider {
})
.then(res => {
return this.safeJSONParse(res.body).then(token => {
debug(`Received token from token endpoint.`)

const d = new Date()
token.expiry = d.setSeconds(d.getSeconds()) + (token.expires_in * 1000)
if (this.useFileCache) {
Expand All @@ -194,9 +196,9 @@ export class OAuthProvider {
private fromFileCache(clientId: string) {
let token: Token
const tokenCachedInFile = fs.existsSync(this.cachedTokenFile(clientId))
debug(`[OAuth] Checking token cache file...`)
debug(`Checking token cache file...`)
if (!tokenCachedInFile) {
debug(`[OAuth] No token cache file found...`)
debug(`No token cache file found...`)
return null
}
try {
Expand All @@ -206,14 +208,14 @@ export class OAuthProvider {
)

if (this.isExpired(token)) {
debug(`[OAuth] Cached token is expired...`)
debug(`Cached token is expired...`)
return null
}
this.tokenCache[this.clientId] = token
this.startExpiryTimer(token)
return token
} catch (e:any) {
debug(`[OAuth] ${e.message}`)
debug(`Failed to load cached token: ${e.message}`)
return null
}
}
Expand Down

0 comments on commit dbadd5c

Please sign in to comment.