Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Login extends BaseCommand {
static override examples = [
'<%= config.bin %> login',
'<%= config.bin %> login -p acme-corp',
'<%= config.bin %> login --scope "accounting.transactions.read accounting.contacts.read accounting.reports.read"',
]

static override flags = {
Expand All @@ -21,6 +22,10 @@ export default class Login extends BaseCommand {
description: 'Xero client ID (overrides profile)',
env: 'XERO_CLIENT_ID',
}),
scope: Flags.string({
description: 'Override OAuth scopes (space-separated). openid/profile/email/offline_access auto-prepended.',
env: 'XERO_SCOPES',
}),
}

async run(): Promise<void> {
Expand All @@ -29,7 +34,7 @@ export default class Login extends BaseCommand {

this.log('Opening browser for Xero login...')

const {tokenSet, tenantId, tenantName} = await performLogin(clientId)
const {tokenSet, tenantId, tenantName} = await performLogin(clientId, flags.scope)
await cacheTokenSet(profileName, tokenSet, tenantId, tenantName)

this.log(`Logged in to ${tenantName}`)
Expand Down
15 changes: 10 additions & 5 deletions src/lib/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ const XERO_TOKEN_BASE = 'https://identity.xero.com'
const REDIRECT_URI = 'http://localhost:8742/callback'
const CALLBACK_TIMEOUT_MS = 120_000

const REQUIRED_OAUTH_SCOPES = ['openid', 'profile', 'email', 'offline_access']

const SCOPES = [
'openid',
'profile',
'email',
'offline_access',
...REQUIRED_OAUTH_SCOPES,
// Contacts & settings
'accounting.contacts',
'accounting.settings',
Expand Down Expand Up @@ -56,12 +55,18 @@ function generateCodeChallenge(verifier: string): string {
return createHash('sha256').update(verifier).digest('base64url')
}

function resolveScopes(scopes?: string): string {
if (!scopes) return SCOPES
const requested = scopes.split(/\s+/).filter(Boolean)
return [...new Set([...REQUIRED_OAUTH_SCOPES, ...requested])].join(' ')
}

function buildAuthUrl(clientId: string, codeChallenge: string, state: string, scopes?: string): string {
const params = new URLSearchParams({
response_type: 'code',
client_id: clientId,
redirect_uri: REDIRECT_URI,
scope: scopes ?? SCOPES,
scope: resolveScopes(scopes),
code_challenge: codeChallenge,
code_challenge_method: 'S256',
state,
Expand Down