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(background/paymentSession): handle Invalid Token error #428

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,9 @@ export class OpenPaymentsService {
walletAddress,
amount: transformedAmount
}).catch((err) => {
if (err instanceof OpenPaymentsClientError) {
if (err.status === 400 && err.code === 'invalid_client') {
const msg = this.t('connectWallet_error_invalidClient')
throw new Error(msg, { cause: err })
}
if (isInvalidClientError(err)) {
const msg = this.t('connectWallet_error_invalidClient')
throw new Error(msg, { cause: err })
}
throw err
})
Expand Down Expand Up @@ -630,3 +628,14 @@ export const isSignatureValidationError = (error: any) => {
error.description?.includes('Signature validation error')
)
}

export const isTokenExpiredError = (error: any) => {
if (!isOpenPaymentsClientError(error)) return false
return isTokenInvalidError(error) || isTokenInactiveError(error)
}
export const isTokenInvalidError = (error: OpenPaymentsClientError) => {
return error.status === 401 && error.description === 'Invalid Token'
}
export const isTokenInactiveError = (error: OpenPaymentsClientError) => {
return error.status === 403 && error.description === 'Inactive Token'
}
18 changes: 6 additions & 12 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { OpenPaymentsClientError } from '@interledger/open-payments/dist/client'
import { sendMonetizationEvent } from '../lib/messages'
import { convert, sleep } from '@/shared/helpers'
import { transformBalance } from '@/popup/lib/utils'
import { isKeyRevokedError } from './openPayments'
import { isKeyRevokedError, isTokenExpiredError } from './openPayments'
import type { EventsService, OpenPaymentsService, TabState } from '.'
import type { Tabs } from 'webextension-polyfill'
import type { MonetizationEventDetails } from '@/shared/messages'
Expand Down Expand Up @@ -158,13 +158,10 @@ export class PaymentSession {
} catch (e) {
if (isKeyRevokedError(e)) {
this.events.emit('open_payments.key_revoked')
} else if (isTokenExpiredError(e)) {
await this.openPaymentsService.rotateToken()
continue
} else if (e instanceof OpenPaymentsClientError) {
// Status code 403 -> expired access token
if (e.status === 403) {
await this.openPaymentsService.rotateToken()
continue
}

// We need better error handling.
if (e.status === 400) {
await this.setIncomingPaymentUrl()
Expand Down Expand Up @@ -302,11 +299,8 @@ export class PaymentSession {
} catch (e) {
if (isKeyRevokedError(e)) {
this.events.emit('open_payments.key_revoked')
} else if (e instanceof OpenPaymentsClientError) {
// Status code 403 -> expired access token
if (e.status === 403) {
await this.openPaymentsService.rotateToken()
}
} else if (isTokenExpiredError(e)) {
await this.openPaymentsService.rotateToken()
}
} finally {
if (outgoingPayment) {
Expand Down
Loading