idp-e2e-proof.mjs183 lines · main
| 1 | /** |
| 2 | * Briven Auth OIDC IdP E2E proof (service-level, Doltgres). |
| 3 | * |
| 4 | * Confidential client + public PKCE client: |
| 5 | * create client → auth request → consent/code → token → userinfo → refresh → revoke → introspect |
| 6 | * |
| 7 | * cd apps/api |
| 8 | * BRIVEN_ENGINE_DATABASE_URL=... BRIVEN_DATA_PLANE_URL=... \ |
| 9 | * BRIVEN_AUTH_CORE_ENABLED=true BRIVEN_ENV=development \ |
| 10 | * BRIVEN_API_ORIGIN=https://api.briven.tech BRIVEN_WEB_ORIGIN=https://briven.tech \ |
| 11 | * BRIVEN_BETTER_AUTH_SECRET=dev-secret-at-least-32-chars-long!! \ |
| 12 | * bun scripts/idp-e2e-proof.mjs |
| 13 | */ |
| 14 | |
| 15 | process.env.BRIVEN_AUTH_CORE_ENABLED = 'true'; |
| 16 | process.env.BRIVEN_ENV = process.env.BRIVEN_ENV ?? 'development'; |
| 17 | process.env.BRIVEN_ENGINE_DATABASE_URL = |
| 18 | process.env.BRIVEN_ENGINE_DATABASE_URL ?? |
| 19 | 'postgres://postgres:devpass@127.0.0.1:5434/briven_engine?sslmode=disable'; |
| 20 | process.env.BRIVEN_DATA_PLANE_URL = |
| 21 | process.env.BRIVEN_DATA_PLANE_URL ?? |
| 22 | 'postgres://postgres:devpass@127.0.0.1:5434/postgres?sslmode=disable'; |
| 23 | process.env.BRIVEN_API_ORIGIN = |
| 24 | process.env.BRIVEN_API_ORIGIN ?? 'https://api.briven.tech'; |
| 25 | process.env.BRIVEN_WEB_ORIGIN = |
| 26 | process.env.BRIVEN_WEB_ORIGIN ?? 'https://briven.tech'; |
| 27 | process.env.BRIVEN_BETTER_AUTH_SECRET = |
| 28 | process.env.BRIVEN_BETTER_AUTH_SECRET ?? 'dev-secret-at-least-32-chars-long!!'; |
| 29 | |
| 30 | import { createHash, randomBytes } from 'node:crypto'; |
| 31 | |
| 32 | const { ensureBrivenEngineDatabase } = await import( |
| 33 | '../src/services/auth-core/ensure-db.ts' |
| 34 | ); |
| 35 | const { initAuthCoreSdk } = await import('../src/services/auth-core/engine.ts'); |
| 36 | const { signUpEmailPassword } = await import( |
| 37 | '../src/services/auth-core/emailpassword.ts' |
| 38 | ); |
| 39 | const { createOidcClient } = await import( |
| 40 | '../src/services/auth-core/idp-clients.ts' |
| 41 | ); |
| 42 | const { |
| 43 | createAuthRequest, |
| 44 | issueAuthCodeAndRedirect, |
| 45 | exchangeAuthorizationCode, |
| 46 | exchangeRefreshToken, |
| 47 | buildUserInfo, |
| 48 | revokeToken, |
| 49 | introspectToken, |
| 50 | discoveryDocument, |
| 51 | } = await import('../src/services/auth-core/idp-flow.ts'); |
| 52 | const { getOidcJwks } = await import('../src/services/auth-core/idp-signing.ts'); |
| 53 | |
| 54 | function fail(msg, extra) { |
| 55 | console.error('FAIL', msg, extra ?? ''); |
| 56 | process.exit(1); |
| 57 | } |
| 58 | function ok(msg) { |
| 59 | console.log('ok', msg); |
| 60 | } |
| 61 | |
| 62 | console.log('=== IdP E2E proof (briven-engine OIDC) ==='); |
| 63 | |
| 64 | const db = await ensureBrivenEngineDatabase(); |
| 65 | if (!db.ok) fail('ensure db', db); |
| 66 | if (!(await initAuthCoreSdk())) fail('init sdk'); |
| 67 | |
| 68 | const doc = discoveryDocument(); |
| 69 | if (!doc.authorization_endpoint || !doc.token_endpoint || !doc.jwks_uri) { |
| 70 | fail('discovery missing endpoints', doc); |
| 71 | } |
| 72 | ok('discovery shape'); |
| 73 | |
| 74 | const jwks = await getOidcJwks(); |
| 75 | if (!jwks.keys?.length) fail('jwks empty'); |
| 76 | ok(`jwks keys=${jwks.keys.length}`); |
| 77 | |
| 78 | const projectId = `p_idp_${Date.now().toString(36)}`; |
| 79 | const email = `idp_${Date.now()}@example.com`; |
| 80 | const su = await signUpEmailPassword({ |
| 81 | email, |
| 82 | password: 'IdpProof!99xx', |
| 83 | projectId, |
| 84 | }); |
| 85 | if (su.status !== 'OK') fail('signup', su); |
| 86 | const userId = su.user.id; |
| 87 | ok(`user ${userId}`); |
| 88 | |
| 89 | const redirect = 'http://localhost:9999/cb'; |
| 90 | const conf = await createOidcClient({ |
| 91 | projectId, |
| 92 | name: 'E2E Confidential', |
| 93 | redirectUris: [redirect], |
| 94 | isPublic: false, |
| 95 | }); |
| 96 | if (!conf.clientSecret) fail('confidential secret missing'); |
| 97 | ok(`confidential client ${conf.client.clientId}`); |
| 98 | |
| 99 | const authReq = await createAuthRequest({ |
| 100 | client: conf.client, |
| 101 | redirectUri: redirect, |
| 102 | scope: 'openid profile email offline_access', |
| 103 | state: 'st1', |
| 104 | nonce: 'n1', |
| 105 | }); |
| 106 | const { redirectUrl } = await issueAuthCodeAndRedirect(authReq.id, userId); |
| 107 | const code = new URL(redirectUrl).searchParams.get('code'); |
| 108 | if (!code) fail('no code in redirect', redirectUrl); |
| 109 | ok('authorization code issued'); |
| 110 | |
| 111 | const tok = await exchangeAuthorizationCode({ |
| 112 | code, |
| 113 | redirectUri: redirect, |
| 114 | clientId: conf.client.clientId, |
| 115 | clientSecret: conf.clientSecret, |
| 116 | }); |
| 117 | if (!tok.ok) fail('token exchange', tok); |
| 118 | if (!tok.access_token || !tok.id_token) fail('missing tokens', tok); |
| 119 | ok('token exchange (confidential)'); |
| 120 | |
| 121 | const info = await buildUserInfo(tok.access_token); |
| 122 | if (!info.ok) fail('userinfo', info); |
| 123 | const sub = info.body?.sub; |
| 124 | if (sub !== userId) fail('userinfo sub mismatch', info); |
| 125 | ok(`userinfo sub=${sub}`); |
| 126 | |
| 127 | if (!tok.refresh_token) fail('expected refresh_token with offline_access'); |
| 128 | const refreshed = await exchangeRefreshToken({ |
| 129 | refreshToken: tok.refresh_token, |
| 130 | clientId: conf.client.clientId, |
| 131 | clientSecret: conf.clientSecret, |
| 132 | }); |
| 133 | if (!refreshed.ok) fail('refresh', refreshed); |
| 134 | ok('refresh token'); |
| 135 | |
| 136 | const intro = await introspectToken({ |
| 137 | token: refreshed.access_token, |
| 138 | clientId: conf.client.clientId, |
| 139 | clientSecret: conf.clientSecret, |
| 140 | }); |
| 141 | if (!intro.active) fail('introspect inactive', intro); |
| 142 | ok('introspect active'); |
| 143 | |
| 144 | const rev = await revokeToken({ |
| 145 | token: tok.refresh_token, |
| 146 | clientId: conf.client.clientId, |
| 147 | clientSecret: conf.clientSecret, |
| 148 | }); |
| 149 | if (!rev.ok) fail('revoke', rev); |
| 150 | ok('revoke'); |
| 151 | |
| 152 | // Public + PKCE |
| 153 | const verifier = randomBytes(32).toString('base64url'); |
| 154 | const challenge = createHash('sha256').update(verifier).digest('base64url'); |
| 155 | const pub = await createOidcClient({ |
| 156 | projectId, |
| 157 | name: 'E2E Public PKCE', |
| 158 | redirectUris: [redirect], |
| 159 | isPublic: true, |
| 160 | }); |
| 161 | const authReq2 = await createAuthRequest({ |
| 162 | client: pub.client, |
| 163 | redirectUri: redirect, |
| 164 | scope: 'openid email', |
| 165 | codeChallenge: challenge, |
| 166 | codeChallengeMethod: 'S256', |
| 167 | }); |
| 168 | const { redirectUrl: redir2 } = await issueAuthCodeAndRedirect( |
| 169 | authReq2.id, |
| 170 | userId, |
| 171 | ); |
| 172 | const code2 = new URL(redir2).searchParams.get('code'); |
| 173 | const tok2 = await exchangeAuthorizationCode({ |
| 174 | code: code2, |
| 175 | redirectUri: redirect, |
| 176 | clientId: pub.client.clientId, |
| 177 | codeVerifier: verifier, |
| 178 | }); |
| 179 | if (!tok2.ok) fail('pkce token', tok2); |
| 180 | ok('public client + PKCE'); |
| 181 | |
| 182 | console.log('=== IdP E2E proof PASSED ==='); |
| 183 | process.exit(0); |