qiwi/qorsproxy

View on GitHub
src/main/js/servlet/corsproxy/parse.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import basicAuth from 'basic-auth'
import crypto from 'node:crypto'
import url from './url.js'

export default (req) => {
  const path = url.parseRequest(req)
  const auth = basicAuth(req) || {}
  const user = auth.name
  const password = auth.pass
  const secret = user && crypto.createHash('md5').update(user + ':' + password).digest('hex')
  const host = path.host
  const origin = req.get('origin')
  const from = (origin && url.parse(origin).hostname) || origin

  return {
    path,
    to: host,
    from, // TODO req.ip as fallback?
    user,
    secret,
    origin
  }
}