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

Check PAT privileges prior to logging in. #41

Merged
merged 1 commit into from
Nov 21, 2023
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
7 changes: 7 additions & 0 deletions .changeset/flat-colts-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'gov4git-desktop-app': patch
---

Check PAT privileges prior to logging in.

- Addresses #39, #40, and gov4git/gov4git#133
8 changes: 8 additions & 0 deletions src/electron/services/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ export class ConfigService extends AbstractConfigService {
const errors: string[] = []
const user = config.user!

const scopes = await this.gitService.getOAuthScopes(user.pat)
if (!scopes.includes('repo')) {
errors.push(
'Personal Access Token has insufficient privileges. Please ensure PAT has rights to top-level repo scope.',
)
return errors
}

if (!(await this.gitService.doesUserExist(user as GitUserInfo))) {
errors.push(`Invalid user credentials`)
}
Expand Down
11 changes: 11 additions & 0 deletions src/electron/services/GitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ export class GitService {
}
}

public getOAuthScopes = async (token: string): Promise<string[]> => {
const response = await fetch(`${this.apiBaseUrl}`, {
method: 'GET',
headers: {
Authorization: `Token ${token}`,
},
})
const scopes = (response.headers.get('X-OAuth-Scopes') ?? '').split(', ')
return scopes
}

protected throwIfUserDoesNotExist = async (
user: GitUserInfo,
from: string,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/components/SettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Card,
Field,
Input,
Label,

Check warning on line 6 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, release/linux-unpacked)

'Label' is defined but never used

Check warning on line 6 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (macos-latest, release/mac)

'Label' is defined but never used

Check warning on line 6 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (windows-latest, release/win-upacked)

'Label' is defined but never used
LabelProps,
} from '@fluentui/react-components'
import { InfoLabel } from '@fluentui/react-components/unstable'

Check warning on line 9 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, release/linux-unpacked)

'InfoLabel' is defined but never used

Check warning on line 9 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (macos-latest, release/mac)

'InfoLabel' is defined but never used

Check warning on line 9 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (windows-latest, release/win-upacked)

'InfoLabel' is defined but never used
import { useAtom, useAtomValue } from 'jotai'
import { FC, FormEvent, useCallback, useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
Expand Down Expand Up @@ -73,6 +73,7 @@
eventBus.emit('user-logged-in')
}
} catch (ex) {
setLoading(false)
await catchError(`Failed to save config. ${ex}`)
}
},
Expand Down Expand Up @@ -123,7 +124,7 @@
className={styles.field}
// @ts-expect-error children signature
label={{
children: (_: unknown, slotProps: LabelProps) => (

Check warning on line 127 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, release/linux-unpacked)

'_' is defined but never used

Check warning on line 127 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, release/linux-unpacked)

'slotProps' is defined but never used

Check warning on line 127 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (macos-latest, release/mac)

'_' is defined but never used

Check warning on line 127 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (macos-latest, release/mac)

'slotProps' is defined but never used

Check warning on line 127 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (windows-latest, release/win-upacked)

'_' is defined but never used

Check warning on line 127 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (windows-latest, release/win-upacked)

'slotProps' is defined but never used
<label htmlFor="PAT" className={styles.labelText}>
Personal Access Token
</label>
Expand All @@ -143,7 +144,7 @@
className={styles.field}
// @ts-expect-error children signature
label={{
children: (_: unknown, slotProps: LabelProps) => (

Check warning on line 147 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, release/linux-unpacked)

'_' is defined but never used

Check warning on line 147 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (macos-latest, release/mac)

'_' is defined but never used

Check warning on line 147 in src/renderer/src/components/SettingsForm.tsx

View workflow job for this annotation

GitHub Actions / build (windows-latest, release/win-upacked)

'_' is defined but never used
<label htmlFor="communityRepoUrl" className={styles.labelText}>
Community URL
</label>
Expand Down
Loading