diff options
Diffstat (limited to 'names/migrate-check.js')
| -rw-r--r-- | names/migrate-check.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/names/migrate-check.js b/names/migrate-check.js new file mode 100644 index 0000000..d0248a6 --- /dev/null +++ b/names/migrate-check.js @@ -0,0 +1,19 @@ +// /migrate-grant's second check. The bearer token proves the caller is +// coinos.io, not that the name's own account asked, so the old account must +// already be flagged migrated (its own session flags itself just before the +// grant). A bug upstream then can't hand someone else's name to a stranger. +// Returns null when the grant may go ahead, else [status, error]. +export async function migrateGrantRefusal(name, fetchFn = fetch) { + let u; + try { + const r = await fetchFn(`https://coinos.io/api/users/${encodeURIComponent(name)}`, { signal: AbortSignal.timeout(8000) }); + if (r.status === 404) return [404, 'no such coinos.io account']; + if (r.status !== 200) throw new Error(`coinos.io said ${r.status}`); + u = await r.json(); + } catch { + return [502, "couldn't check the coinos.io account, try again"]; + } + if (!u || String(u.username || '').toLowerCase() !== String(name).toLowerCase()) return [404, 'no such coinos.io account']; + if (u.migrated !== true) return [409, 'that coinos.io account has not migrated']; + return null; +} |
