Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import { JoseKey, NodeOAuthClient, requestLocalLock, type NodeSavedSession, type NodeSavedState,} from '@atproto/oauth-client-node'import { kvStore } from './db.ts'
export const SCOPE = [ 'atproto', 'repo:community.lexicon.calendar.event', 'repo:community.lexicon.calendar.rsvp', 'blob:image/*',].join(' ')
export const PORT = Number(process.env.PORT ?? 3000)/** Public https origin in production (e.g. https://juno.up.railway.app). Unset → loopback dev client. */export const PUBLIC_URL = process.env.PUBLIC_URL?.replace(/\/$/, '')/** Where the native app wants to land after sign-in. */export const APP_CALLBACK = process.env.APP_CALLBACK ?? 'juno://auth'
export const baseUrl = PUBLIC_URL ?? `http://127.0.0.1:${PORT}`const redirectUri = `${baseUrl}/oauth/callback`
async function buildClient() { const stateStore = kvStore<NodeSavedState>('oauth_state', 10 * 60_000) const sessionStore = kvStore<NodeSavedSession>('oauth_session')
if (!PUBLIC_URL) { // Loopback client for local development: no keys, metadata encoded in the client_id. const clientId = `http://localhost?${new URLSearchParams({ redirect_uri: redirectUri, scope: SCOPE })}` return new NodeOAuthClient({ clientMetadata: { client_id: clientId, redirect_uris: [redirectUri], scope: SCOPE, grant_types: ['authorization_code', 'refresh_token'], response_types: ['code'], application_type: 'web', token_endpoint_auth_method: 'none', dpop_bound_access_tokens: true, }, stateStore, sessionStore, requestLock: requestLocalLock, }) }
const jwk = process.env.PRIVATE_KEY_JWK if (!jwk) throw new Error('PRIVATE_KEY_JWK is required when PUBLIC_URL is set (run `pnpm gen-key`)') const key = await JoseKey.fromImportable(jwk, 'juno-key-1')
return new NodeOAuthClient({ clientMetadata: { client_id: `${PUBLIC_URL}/oauth-client-metadata.json`, client_name: 'Juno', client_uri: PUBLIC_URL, redirect_uris: [redirectUri], scope: SCOPE, grant_types: ['authorization_code', 'refresh_token'], response_types: ['code'], application_type: 'web', token_endpoint_auth_method: 'private_key_jwt', token_endpoint_auth_signing_alg: 'ES256', dpop_bound_access_tokens: true, jwks_uri: `${PUBLIC_URL}/jwks.json`, }, keyset: [key], stateStore, sessionStore, requestLock: requestLocalLock, })}
export const oauthClient = await buildClient()