OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / scripts / test-token.js

72 lines · javascript ref:
#!/usr/bin/env bare

/**
 * Test token validation
 */

import Corestore from 'corestore'
import Hyperbee from 'hyperbee'
import process from 'bare-process'
import crypto from 'bare-crypto'

async function main() {
  const token = process.argv[2]

  if (!token) {
    console.error('Usage: bare test-token.js <token>')
    process.exit(1)
  }

  console.log('🔑 Testing token validation...\n')
  console.log('Token:', token)

  // Initialize storage
  const store = new Corestore('./pear-git-storage')
  await store.ready()

  const core = store.get({ name: 'pear-git-db' })
  const db = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'json' })
  await db.ready()

  try {
    const tokenHash = crypto.createHash('sha256').update(token).digest('hex')
    console.log('Token hash:', tokenHash)
    console.log('')

    const entry = await db.get(`token:${tokenHash}`)

    if (!entry) {
      console.log('❌ Token not found in database')
      process.exit(1)
    }

    const tokenData = entry.value
    console.log('✅ Token found!')
    console.log('')
    console.log('Token data:')
    console.log(`  Username: ${tokenData.username}`)
    console.log(`  Description: ${tokenData.description}`)
    console.log(`  Permissions: ${tokenData.permissions.join(', ')}`)
    console.log(`  Expires: ${tokenData.expiresAt ? new Date(tokenData.expiresAt).toISOString() : 'Never'}`)
    console.log('')

    if (tokenData.expiresAt && Date.now() > tokenData.expiresAt) {
      console.log('❌ Token is expired')
      process.exit(1)
    }

    console.log('✅ Token is valid!')
  } catch (err) {
    console.error('❌ Error validating token:', err.message)
    process.exit(1)
  } finally {
    await db.close()
    await store.close()
  }
}

main().catch(err => {
  console.error('Fatal error:', err)
  process.exit(1)
})