added name/password constraints, should be ready for usage

This commit is contained in:
Paul Liverman III 2018-09-09 16:18:54 -07:00
parent ee896cd1de
commit ae8cd46e04
2 changed files with 13 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import api, abort, assert_model from require "helpers"
class extends lapis.Application class extends lapis.Application
[authenticate: "/0/auth"]: api { [authenticate: "/0/auth"]: api {
POST: => POST: =>
-- find user by name or id if specified
local user local user
if @params.name if @params.name
user = Users\find name: @params.name user = Users\find name: @params.name
@ -15,16 +16,26 @@ class extends lapis.Application
user = Users\find id: @params.id user = Users\find id: @params.id
abort "No such user." unless user abort "No such user." unless user
-- if a user by that name exists, see if the password is correct
if user if user
unless bcrypt.verify(@params.password, user.digest) unless bcrypt.verify(@params.password, user.digest)
abort "Incorrect password." abort "Incorrect password."
-- else create a user
elseif @params.password elseif @params.password
-- TODO create user with specified password assert_valid(@params, {
-- TODO constraints on password for security purposes { "name", exists: true, min_length: 1, max_length: 255, matches_pattern: "%w+" }
{ "password", exists: true, min_length: 8, max_length: 255 }
})
-- TODO passwords should be checked against known breached passwords
-- TODO passwords should be required to follow a few other basic security checks
-- actually, these are invalidated just by checking against breached passwords I think
user = assert_model Users\create { user = assert_model Users\create {
name: @params.name name: @params.name
digest: bcrypt.digest(@params.password, config.digest_rounds) digest: bcrypt.digest(@params.password, config.digest_rounds)
} }
-- if a password wasn't specified...
else
abort "Must specify name or id, and password."
return name: user.name, id: user.id return name: user.name, id: user.id
} }

View File

@ -1,4 +1,3 @@
import Model from require "lapis.db.model" import Model from require "lapis.db.model"
class Users extends Model class Users extends Model
-- TODO constraints on usernames under 256 bytes, alphanumerics only