Browse Source

incomplete v1

Paul Liverman III 4 years ago
commit
ee896cd1de
7 changed files with 110 additions and 0 deletions
  1. 3
    0
      Dockerfile
  2. 37
    0
      app.moon
  3. 14
    0
      config.moon
  4. 8
    0
      create
  5. 34
    0
      helpers.moon
  6. 10
    0
      migrations.moon
  7. 4
    0
      models/Users.moon

+ 3
- 0
Dockerfile View File

@@ -0,0 +1,3 @@
1
+FROM guard13007/docker-lapis:latest
2
+
3
+RUN luarocks install bcrypt

+ 37
- 0
app.moon View File

@@ -0,0 +1,37 @@
1
+lapis = require "lapis"
2
+bcrypt = require "bcrypt"
3
+config = require("lapis.config").get!
4
+
5
+import Users from require "models"
6
+import api, abort, assert_model from require "helpers"
7
+
8
+class extends lapis.Application
9
+  [authenticate: "/0/auth"]: api {
10
+    POST: =>
11
+      local user
12
+      if @params.name
13
+        user = Users\find name: @params.name
14
+      elseif @params.id
15
+        user = Users\find id: @params.id
16
+        abort "No such user." unless user
17
+
18
+      if user
19
+        unless bcrypt.verify(@params.password, user.digest)
20
+          abort "Incorrect password."
21
+      elseif @params.password
22
+        -- TODO create user with specified password
23
+        -- TODO constraints on password for security purposes
24
+        user = assert_model Users\create {
25
+          name: @params.name
26
+          digest: bcrypt.digest(@params.password, config.digest_rounds)
27
+        }
28
+
29
+      return name: user.name, id: user.id
30
+  }
31
+  [name: "/0/:id[%d]"]: api {
32
+    GET: =>
33
+      if user = Users\find id: @params.id
34
+        return name: user.name
35
+      else
36
+        abort "No such user."
37
+  }

+ 14
- 0
config.moon View File

@@ -0,0 +1,14 @@
1
+config = require "lapis.config"
2
+
3
+postgres_password = os.getenv "POSTGRES_PASSWORD"
4
+
5
+config "development", ->
6
+  postgres ->
7
+    host "passwd-db"
8
+    user "postgres"
9
+    database "postgres"
10
+    password postgres_password
11
+  num_workers 2
12
+  code_cache "on"
13
+
14
+  digest_rounds 12

+ 8
- 0
create View File

@@ -0,0 +1,8 @@
1
+#!/usr/bin/env bash
2
+docker build -t passwd0 .
3
+docker network create passwd-db
4
+docker run -d --restart always --name passwd-db --network passwd-db \
5
+  -v "$PWD/db":/var/lib/postgresql/data postgres:10.5-alpine
6
+docker run -d --restart always --name passwd0 --network passwd-db \
7
+  passwd0:latest
8
+docker network connect passwd0 web

+ 34
- 0
helpers.moon View File

@@ -0,0 +1,34 @@
1
+import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
2
+import insert from table
3
+import max from math
4
+
5
+api = (tab) ->
6
+  json_params capture_errors {
7
+    respond_to(tab),
8
+    on_error: =>
9
+      status = 400
10
+      errors = {}
11
+      for err in *@errors
12
+        if "table" == type err
13
+          status = max status, err[1]
14
+          insert errors, err[2]
15
+        else
16
+          insert errors, err
17
+      return(:status, json: { success: false, :errors })
18
+  }
19
+
20
+abort = (status, message) ->
21
+  if message
22
+    yield_error {status, message}
23
+  else
24
+    yield_error status
25
+
26
+assert_model = (result, err) ->
27
+  abort 500, err if err
28
+  return result
29
+
30
+{
31
+  :api
32
+  :abort
33
+  :assert_model
34
+}

+ 10
- 0
migrations.moon View File

@@ -0,0 +1,10 @@
1
+import create_table, types from require "lapis.db.schema"
2
+
3
+{
4
+  [1535780489]: =>
5
+    create_table "users", {
6
+      {"id", types.serial primary_key: true}
7
+      {"name", types.varchar unique: true}
8
+      {"digest", types.text}
9
+    }
10
+}

+ 4
- 0
models/Users.moon View File

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