-
Notifications
You must be signed in to change notification settings - Fork 5
feat(auth): add bearer token authentication #145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d70e7c
feat(auth): add server-side bearer token authentication
gricha 9d792db
feat(auth): add CLI commands for token management
gricha daa8dcd
feat(auth): add token handling to Web UI
gricha b226ac6
feat(auth): add token handling to Mobile app
gricha f2f5e40
test(auth): add unit and integration tests for auth flow
gricha b483647
fix(auth): serve static files before auth check for web UI
gricha 16aead5
fix(auth): allow web UI static paths without auth
gricha 2446067
fix(auth): persist auth config on loadAgentConfig
gricha 66c8e8d
fix(web): add type argument to RPCLink generic
gricha b913d50
fix(agent): skip SPA fallback for API paths
gricha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { AgentConfig } from '../shared/types'; | ||
| import { getTailscaleIdentity } from '../tailscale'; | ||
|
|
||
| export interface AuthResult { | ||
| ok: boolean; | ||
| identity?: { type: 'token' | 'tailscale'; user?: string }; | ||
| } | ||
|
|
||
| const PUBLIC_PATHS = ['/health']; | ||
|
|
||
| const WEB_UI_PATTERNS = [/^\/$/, /^\/index\.html$/, /^\/assets\//, /^\/favicon\.ico$/]; | ||
|
|
||
| function isPublicPath(pathname: string): boolean { | ||
| if (PUBLIC_PATHS.includes(pathname)) { | ||
| return true; | ||
| } | ||
| return WEB_UI_PATTERNS.some((pattern) => pattern.test(pathname)); | ||
| } | ||
|
|
||
| export function checkAuth(req: Request, config: AgentConfig): AuthResult { | ||
| const url = new URL(req.url); | ||
|
|
||
| if (isPublicPath(url.pathname)) { | ||
| return { ok: true }; | ||
| } | ||
|
|
||
| if (!config.auth?.token) { | ||
| return { ok: true }; | ||
| } | ||
|
|
||
| const tsIdentity = getTailscaleIdentity(req); | ||
| if (tsIdentity) { | ||
| return { ok: true, identity: { type: 'tailscale', user: tsIdentity.email } }; | ||
| } | ||
|
|
||
| const authHeader = req.headers.get('Authorization'); | ||
| if (authHeader?.startsWith('Bearer ')) { | ||
| const token = authHeader.slice(7); | ||
| if (token === config.auth.token) { | ||
| return { ok: true, identity: { type: 'token' } }; | ||
| } | ||
| } | ||
|
|
||
| return { ok: false }; | ||
| } | ||
|
|
||
| export function unauthorizedResponse(): Response { | ||
| return new Response('Unauthorized', { | ||
| status: 401, | ||
| headers: { | ||
| 'WWW-Authenticate': 'Bearer', | ||
| 'Access-Control-Allow-Origin': '*', | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import crypto from 'crypto'; | ||
| import { loadAgentConfig, saveAgentConfig, getConfigDir } from '../config/loader'; | ||
|
|
||
| export async function authInit(): Promise<void> { | ||
| const configDir = getConfigDir(); | ||
| const config = await loadAgentConfig(configDir); | ||
|
|
||
| if (config.auth?.token) { | ||
| console.log('Auth token already exists.'); | ||
| console.log('To regenerate, remove auth.token from config.json first.'); | ||
| return; | ||
| } | ||
|
|
||
| const token = `perry-${crypto.randomBytes(16).toString('hex')}`; | ||
| config.auth = { ...config.auth, token }; | ||
| await saveAgentConfig(config, configDir); | ||
|
|
||
| console.log(`Auth token generated: ${token}`); | ||
| console.log(`Configure clients with: perry config token ${token}`); | ||
| console.log('Restart the agent for auth to take effect.'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.