import { initSession, doLogin, getHashDid, getHashKind, getHashLink, loginHashState, doClaimAssociate, getHashOauthParams, saveHashForLogin, restoreHashFromLogin, clearOAuthHash, isAlreadyAssociated, fetchRecords, log, doAssociate, doRequesterAssociate, doBidderAssociate, randomName } from '../main.js'; import './dka-attest-confirm.js'; import './dka-key-list.js'; import './dka-share-sheet.js'; import './dka-oauth-transfer.js'; export class DkaApp extends HTMLElement { connectedCallback() { this._oac = null; this._agent = null; this._sessionHandle = null; this._state = 'loading'; // 'loading' | 'login' | 'main' this._boundHashChange = this._onHashChange.bind(this); window.addEventListener('hashchange', this._boundHashChange); window.addEventListener('popstate', this._boundHashChange); // OAuth callback — handle synchronously before renderLoading, else // query params (code+state+iss) get lost before the async _init runs. const qp = new URLSearchParams(window.location.search); if (qp.get('code') && qp.get('state') && qp.get('iss')) { log('info', 'app', 'connectedCallback:oauthCallback', { hasCode: true }); this._state = 'login'; this.renderLogin(); // Block hashChange re-render until transfer completes (success/error // sets _transferDone=true, or a new QR scan changes the hash). this._oauthHash = window.location.hash; this._transferDone = true; return; } this.renderLoading(); this._init(); } disconnectedCallback() { window.removeEventListener('hashchange', this._boundHashChange); window.removeEventListener('popstate', this._boundHashChange); } _escape(s) { const el = document.createElement('span'); el.textContent = String(s); return el.innerHTML; } _onHashChange() { log('info', 'app', 'hashChange', { hash: window.location.hash, state: this._state }); if (this._state === 'main') { const oauthParams = getHashOauthParams(); if (oauthParams) { this.renderMain(); return; } this._updateAttestFromHash(); } else if (this._state === 'login') { const oauthParams = getHashOauthParams(); if (oauthParams) { // If hash changed (new QR scan), allow re-render. if (this._oauthHash !== window.location.hash) { this._transferDone = false; } if (this._transferDone) return; this.renderLogin(); } } } _updateAttestFromHash() { const hashDid = getHashDid(); log('info', 'app', 'hashChange:updateAttest', { hashDid: hashDid || null }); const attest = this.querySelector('#attest-confirm'); if (!attest) return; // Never show attest modal for oauth transfer values if (hashDid && !hashDid.startsWith('oauth=') && !hashDid.startsWith('oauth:')) { const kind = getHashKind(); attest.setAttribute('did-key', hashDid); attest.removeAttribute('requester-did'); attest.removeAttribute('bidder-did'); if (kind === 'requester') attest.setAttribute('requester-did', hashDid); if (kind === 'bidder') attest.setAttribute('bidder-did', hashDid); attest.removeAttribute('already-associated'); attest.style.display = ''; const keyList = this.querySelector('#key-list'); if (keyList && keyList._records && kind === 'key' && isAlreadyAssociated(keyList._records, hashDid)) { attest.setAttribute('already-associated', 'true'); } } else { attest.style.display = 'none'; } } renderLoading() { this.innerHTML = ` Loading session… `; } async _init() { try { log('debug', 'app', '_init:start'); // Skip BrowserOAuthClient init if returning from our custom OAuth callback. // BrowserOAuthClient would try to handle code+state itself and hang. const qp = new URLSearchParams(window.location.search); if (qp.get('code') && qp.get('state') && qp.get('iss')) { log('info', 'app', '_init:oauthCallback', { hasCode: true }); this._oac = null; this._agent = null; this._sessionHandle = null; this.renderLogin(); return; } const { oac, agent, sessionHandle } = await initSession(); this._oac = oac; this._agent = agent; this._sessionHandle = sessionHandle; if (agent && sessionHandle) { log('info', 'app', '_init:loggedIn', { handle: sessionHandle }); this.renderMain(); } else { log('info', 'app', '_init:loggedOut'); this.renderLogin(); } } catch (err) { log('error', 'app', '_init:error', { error: String(err) }); this.querySelector('#load-error').classList.remove('hidden'); this.querySelector('#load-error').textContent = `An error occurred: ${err}`; } } renderLogin() { this._state = 'login'; const hashDid = getHashDid(); if (hashDid) log('info', 'app', 'renderLogin:hashDetected', { did: hashDid }); // Check for oauth transfer hash — show transfer component instead of login let oauthParams = getHashOauthParams(); if (!oauthParams) { // Restore from sessionStorage (survived OAuth redirect) const restored = restoreHashFromLogin(); if (restored && restored.startsWith('oauth=')) { window.location.hash = restored; return; } } if (oauthParams) { this.innerHTML = ` `; return; } this.innerHTML = ` DID Key Associator Login with the Atmosphere Enter your handle to continue Login If you're a Bluesky user, you already have an Atmosphere account. Create Account with Bluesky Social Other options Source Code `; this.querySelector('#login-form').addEventListener('submit', async (e) => { e.preventDefault(); const btn = this.querySelector('#login-submit'); btn.setAttribute('aria-busy', 'true'); btn.textContent = 'Logging in…'; this.querySelector('#login-error').textContent = ''; try { const state = loginHashState(); saveHashForLogin(); await doLogin(this._oac, e.target.username.value, state); } catch (err) { this.querySelector('#login-error').textContent = `Login error: ${err}`; } btn.removeAttribute('aria-busy'); btn.textContent = 'Login'; }); this.querySelector('#bsky-btn').addEventListener('click', () => { const state = loginHashState(); saveHashForLogin(); doLogin(this._oac, 'https://bsky.social', state); }); } async renderMain() { this._state = 'main'; // Check for oauth transfer hash FIRST (before hashDid consumes sessionStorage) let oauthParams = getHashOauthParams(); if (!oauthParams) { const restored = restoreHashFromLogin(); if (restored && restored.startsWith('oauth=')) { window.location.hash = restored; return; } } if (oauthParams) { this.innerHTML = ` `; return; } const hashDid = getHashDid(); log('info', 'app', 'renderMain:hashDid', { hashDid: hashDid || null }); this.innerHTML = ` DID Key Associator Logout @${this._sessionHandle} `; const keyList = this.querySelector('#key-list'); keyList._agent = this._agent; keyList._sessionHandle = this._sessionHandle; await keyList.refreshKeys(); // Check if hash DID is already associated const isPlc = hashDid && (hashDid.startsWith('did:plc:') || hashDid.startsWith('did:web:')); if (hashDid && !isPlc && isAlreadyAssociated(keyList._records, hashDid)) { log('info', 'app', 'renderMain:alreadyAssociated', { didKey: hashDid }); const attest = this.querySelector('#attest-confirm'); attest.setAttribute('already-associated', 'true'); } // Attest confirm events const attest = this.querySelector('#attest-confirm'); const kind = getHashKind(); if (kind === 'requester') { attest.setAttribute('requester-did', hashDid); } else if (kind === 'bidder') { attest.setAttribute('bidder-did', hashDid); } attest.addEventListener('dka:attest', async () => { // Re-read hash fresh — hashDid/kind captured at renderMain time may be stale const currentLink = getHashLink(); const currentDid = currentLink?.did || null; const currentKind = getHashKind(); log('info', 'app', 'renderMain:attest', { didKey: currentDid, kind: currentKind }); attest.setAttribute('aria-busy', 'true'); const confirmBtn = attest.querySelector('#attest-confirm-btn'); if (confirmBtn) { confirmBtn.disabled = true; confirmBtn.textContent = 'Confirming…'; } try { if ((currentKind === 'requester' || currentKind === 'bidder') && currentLink.nonce) { const service = currentLink.service || `${currentKind}_associate`; log('info', 'app', 'attest:claimAssociate', { did: currentDid, service }); const res = await doClaimAssociate(this._agent, { did: currentDid, service, nonce: currentLink.nonce }); keyList._successUri = res.uri; await keyList.refreshKeys(); keyList._showSuccess(); } else if (currentKind === 'requester') { log('info', 'app', 'attest:requesterAssociate', { requesterDid: currentDid }); await doRequesterAssociate(this._agent, currentDid); log('info', 'app', 'attest:requesterAssociated', { requesterDid: currentDid }); await keyList.refreshKeys(); keyList._showSuccess(); } else if (currentKind === 'bidder') { log('info', 'app', 'attest:bidderAssociate', { bidderDid: currentDid }); await doBidderAssociate(this._agent, currentDid); log('info', 'app', 'attest:bidderAssociated', { bidderDid: currentDid }); await keyList.refreshKeys(); keyList._showSuccess(); } else { const name = randomName(); log('info', 'app', 'attest:associating', { didKey: currentDid, name }); await doAssociate(this._agent, { didKey: currentDid, name }); log('info', 'app', 'attest:associated', { didKey: currentDid }); await keyList.refreshKeys(); keyList._showSuccess(); } } catch (err) { log('error', 'app', 'attest:assocError', { error: String(err) }); if (confirmBtn) { confirmBtn.disabled = false; confirmBtn.textContent = currentKind === 'requester' ? 'Yes, this is my requester' : currentKind === 'bidder' ? 'Yes, this is my bidder' : 'Yes, this is mine — attest publicly'; } return; } attest.removeAttribute('aria-busy'); attest.style.display = 'none'; clearOAuthHash(); if (window.location.hash) { history.replaceState(null, '', window.location.pathname + window.location.search); } }); attest.addEventListener('dka:ignore', () => { log('info', 'app', 'renderMain:ignore', { didKey: hashDid }); attest.style.display = 'none'; clearOAuthHash(); if (window.location.hash) { history.replaceState(null, '', window.location.pathname + window.location.search); } }); // Logout this.querySelector('#logout-btn').addEventListener('click', () => { this._oac.revoke(this._agent.did); window.location.reload(); }); } } if (!customElements.get('dka-app')) { customElements.define('dka-app', DkaApp); }
Enter your handle to continue
If you're a Bluesky user, you already have an Atmosphere account.