Skip to content

Commit

Permalink
Merge pull request #262 from ZIMkaRU/feature/add-invalid-auth-token-f…
Browse files Browse the repository at this point in the history
…lag-to-unauthorized-401-error

Add auth token invalid flag into AuthError
  • Loading branch information
prdn authored Mar 22, 2023
2 parents d33c94a + 5f480dd commit b949c8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
9 changes: 7 additions & 2 deletions workers/loc.api/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const {
ConflictError,
ArgsParamsError
} = require('bfx-report/workers/loc.api/errors')
const {
getErrorArgs
} = require('bfx-report/workers/loc.api/errors/helpers')

class CollSyncPermissionError extends BaseError {
constructor (message = 'ERR_PERMISSION_DENIED_TO_SYNC_SELECTED_COLL') {
Expand Down Expand Up @@ -220,8 +223,10 @@ class SyncInfoUpdatingError extends BaseError {
}

class AuthTokenGenerationError extends AuthError {
constructor (message = 'ERR_AUTH_TOKEN_HAS_NOT_BEEN_GENERATED') {
super(message)
constructor (args) {
const _args = getErrorArgs(args, 'ERR_AUTH_TOKEN_HAS_NOT_BEEN_GENERATED')

super(_args)
}
}

Expand Down
47 changes: 29 additions & 18 deletions workers/loc.api/sync/authenticator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,27 +1151,36 @@ class Authenticator {
}

async generateAuthToken (args) {
const opts = {
ttl: this.authTokenTTLSec,
writePermission: false
}
try {
const opts = {
ttl: this.authTokenTTLSec,
writePermission: false
}

const res = await this.getDataFromApi({
getData: (s, args) => this.rService._generateToken(args, opts),
args,
callerName: 'AUTHENTICATOR',
eNetErrorAttemptsTimeframeMin: 10 / 60,
eNetErrorAttemptsTimeoutMs: 1000,
shouldNotInterrupt: true
})
const res = await this.getDataFromApi({
getData: (s, args) => this.rService._generateToken(args, opts),
args,
callerName: 'AUTHENTICATOR',
eNetErrorAttemptsTimeframeMin: 10 / 60,
eNetErrorAttemptsTimeoutMs: 1000,
shouldNotInterrupt: true
})

const [authToken] = Array.isArray(res) ? res : [null]
const [authToken] = Array.isArray(res) ? res : [null]

if (!authToken) {
throw new AuthTokenGenerationError()
}
if (!authToken) {
throw new AuthTokenGenerationError()
}

return authToken
return authToken
} catch (err) {
throw new AuthTokenGenerationError({
data: {
isAuthTokenGenerationError: true,
rootMessage: err.toString()
}
})
}
}

async invalidateAuthToken (args) {
Expand Down Expand Up @@ -1254,7 +1263,9 @@ class Authenticator {
)

if (res?.changes < 1) {
throw new AuthTokenGenerationError()
throw new AuthTokenGenerationError({
data: { isAuthTokenGenerationError: true }
})
}

session.authToken = newAuthToken
Expand Down

0 comments on commit b949c8d

Please sign in to comment.