mirror of
https://github.com/overleaf/overleaf.git
synced 2025-03-22 02:04:31 +00:00
Add commentary to token-generator, and move token-alpha to top level
This commit is contained in:
parent
ac513a1355
commit
490ccc6051
1 changed files with 16 additions and 1 deletions
|
@ -1,14 +1,29 @@
|
|||
crypto = require 'crypto'
|
||||
|
||||
# This module mirrors the token generation in Overleaf (`random_token.rb`),
|
||||
# for the purposes of implementing token-based project access, like the
|
||||
# 'unlisted-projects' feature in Overleaf
|
||||
|
||||
module.exports = ProjectTokenGenerator =
|
||||
|
||||
# (From Overleaf `random_token.rb`)
|
||||
# Letters (not numbers! see generate_token) used in tokens. They're all
|
||||
# consonsants, to avoid embarassing words (I can't think of any that use only
|
||||
# a y), and lower case "l" is omitted, because in many fonts it is
|
||||
# indistinguishable from an upper case "I" (and sometimes even the number 1).
|
||||
TOKEN_ALPHA: 'bcdfghjkmnpqrstvwxyz'
|
||||
|
||||
# Generate a 12-char token with only characters from TOKEN_ALPHA,
|
||||
# suitable for use as a read-only token for a project
|
||||
readOnlyToken: () ->
|
||||
length = 12
|
||||
tokenAlpha = 'bcdfghjkmnpqrstvwxyz'
|
||||
tokenAlpha = ProjectTokenGenerator.TOKEN_ALPHA
|
||||
result = ''
|
||||
crypto.randomBytes(length).map( (a) -> result += tokenAlpha[a % tokenAlpha.length] )
|
||||
return result
|
||||
|
||||
# Generate a longer token, with a numeric prefix,
|
||||
# suitable for use as a read-and-write token for a project
|
||||
readAndWriteToken: () ->
|
||||
numerics = Math.random().toString().slice(2, 12)
|
||||
token = ProjectTokenGenerator.readOnlyToken()
|
||||
|
|
Loading…
Reference in a new issue