simplex/models/APIKeys.moon
Paul Liverman III 104c4cfde7 init
2018-04-23 05:00:42 -07:00

25 lines
775 B
Plaintext

bcrypt = require "bcrypt"
import Model from require "lapis.db.model"
import encode_base64 from require "lapis.util.encoding"
import autoload from require "locator"
import settings from autoload "utility"
class APIKeys extends Model
@primary_key: "user_id"
@table_name: => "api_keys"
@create: (user={}, opts) =>
unless (user.name and user.digest)
error "API Keys need to be created using a user model."
values = {}
while true
values.key = encode_base64 bcrypt.digest "#{settings["simplex.key-increment"]}#{user.name}#{user.digest}", settings["users.bcrypt-digest-rounds"]
settings["simplex.key-increment"] += 1
unless APIKeys\find key: values.key
settings.set "simplex.key-increment"
break
super values, opts