diff --git a/src/client/realm-connection-manager.tsx b/src/client/realm-connection-manager.tsx index e46f51d..c03ee14 100644 --- a/src/client/realm-connection-manager.tsx +++ b/src/client/realm-connection-manager.tsx @@ -1,5 +1,8 @@ +import {generateSignableJwt} from '#common/crypto/jwks.js' +import {jwtSchema} from '#common/crypto/jwts.js' import {RealmBrand} from '#common/protocol' import {useSignal} from '@preact/signals' +import {nanoid} from 'nanoid' import {useCallback, useContext} from 'preact/hooks' import {RealmConnectionContext} from './realm/context-connection' import {RealmIdentityContext} from './realm/context-identity' @@ -15,19 +18,62 @@ export const RealmConnectionManager: preact.FunctionComponent = () => { const register = useCallback(() => { console.log('register') - connectionContext.realmid.value = RealmBrand.generate() - }, [connectionContext.realmid]) + connectionContext.connectopts.value = { + realmid: RealmBrand.generate(), + register: true, + } + }, [connectionContext.connectopts]) const exchange = useCallback(() => { console.log('exchange', invitation.value) - }, [invitation]) + jwtSchema + .parseAsync(invitation.value) + .then((token) => { + connectionContext.connectopts.value = { + realmid: RealmBrand.parse(token.claims.aud), + invitation: invitation.value, + } + }) + .catch((exc: unknown) => { + console.error('couldnt exchange', exc) + }) + }, [invitation, connectionContext.connectopts]) - return connectionContext?.realmid.value ? ( -

- Connection: - {connectionContext.realmid.value} - {connectionContext.connected.value ? '🟢 Connected' : '🔴 Disconnected'} -

+ const generate = useCallback(() => { + if (!connectionContext.realm.value) return + identityContext.identity + .sign( + generateSignableJwt({ + aud: connectionContext.realm.value.realmid, + iss: connectionContext.realm.value.identid, + sub: 'invitation', + jti: nanoid(), + exp: Date.now() + 5 * 60, + }), + ) + .then((token) => (invitation.value = token)) + .catch((exc: unknown) => { + console.error('error generating invite', exc) + }) + }, [invitation, connectionContext.realm, identityContext.identity]) + + return connectionContext?.realm.value ? ( +
+

+ Connection: + {connectionContext.realm.value.realmid} + {connectionContext.connected.value ? '🟢 Connected' : '🔴 Disconnected'} +

+