simplex/models/APIKeys.moon

25 lines
775 B
Plaintext
Raw Normal View History

2018-04-23 12:00:42 +00:00
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