mirror of https://github.com/kern/filepizza
Use XKCD-style passphrases.
parent
9679dea646
commit
d8cb491402
@ -0,0 +1,2 @@
|
||||
require('babel/register')
|
||||
module.exports = require('./lib/server')
|
||||
@ -1,45 +1,39 @@
|
||||
import bases from 'bases'
|
||||
import crypto from 'crypto'
|
||||
import xkcdPassword from 'xkcd-password'
|
||||
|
||||
var tokenLength = 8
|
||||
var tokens = {}
|
||||
|
||||
exports.create = function (socket) {
|
||||
|
||||
var maxNum = Math.pow(62, tokenLength)
|
||||
var numBytes = Math.ceil(Math.log(maxNum) / Math.log(256))
|
||||
|
||||
var token = ''
|
||||
do {
|
||||
do {
|
||||
var bytes = crypto.randomBytes(numBytes)
|
||||
var num = 0
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
num += Math.pow(256, i) * bytes[i]
|
||||
const TOKEN_OPTIONS = {
|
||||
numWords: 4,
|
||||
minLength: 4,
|
||||
maxLength: 8
|
||||
}
|
||||
} while (num >= maxNum)
|
||||
|
||||
token = bases.toBase62(num)
|
||||
} while (token in tokens)
|
||||
var tokenGenerator = new xkcdPassword()
|
||||
var tokens = {}
|
||||
|
||||
export function create(socket) {
|
||||
|
||||
const result = {
|
||||
return tokenGenerator.generate(TOKEN_OPTIONS).then((parts) => {
|
||||
const token = parts.join('-')
|
||||
let result = {
|
||||
token: token,
|
||||
socket: socket
|
||||
}
|
||||
|
||||
tokens[token] = result
|
||||
return result
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
exports.exists = function (token) {
|
||||
export function exists(token) {
|
||||
return token in tokens
|
||||
}
|
||||
|
||||
exports.find = function (token) {
|
||||
export function find(token) {
|
||||
return tokens[token]
|
||||
}
|
||||
|
||||
exports.remove = function (client) {
|
||||
export function remove(client) {
|
||||
delete tokens[client.token]
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue