Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Begin users, and setup db models
Browse files Browse the repository at this point in the history
  • Loading branch information
Lleyton Gray committed May 10, 2020
0 parents commit 3d028dc
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
*.key
*.key.pub
*.pub
client_body_temp/
fastcgi_temp/
logs/
proxy_temp/
scgi_temp/
mime.types
nginx.conf.compiled
19 changes: 19 additions & 0 deletions app.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local lapis = require 'lapis'
local validate = require 'lapis.validate'
local encoding = require 'lapis.util.encoding'
local helpers = require 'lapis.application'
local Users = require 'models.users'
local jwt = require 'resty.jwt'

local rand = require 'openssl.rand'
local argon2 = require 'argon2'
local uuid = require 'util.uuid'

local app = lapis.Application()
require('applications.users')(app)

app:get('/', function()
return 'Welcome to uHHHHHHH' .. require('lapis.version')
end)

return app
37 changes: 37 additions & 0 deletions applications/communities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local lapis = require 'lapis'
local validate = require 'lapis.validate'
local encoding = require 'lapis.util.encoding'
local helpers = require 'lapis.application'

local Communities = require 'models.communities'
local uuid = require 'util.uuid'

return function(app)
app:post('/communities', helpers.capture_errors_json(function(self)


local community = Communities:create({ -- TODO: handle all db errors
id = assert(uuid()),
name = self.params.name, -- TODO: Differenciate between query params and form
icon = '',
large = true
})

return {
json = {
id = community.id,
name = community.name,
icon = community.icon,
large = community.large
}
}
end))

app:get('/communities/:id/channels', helpers.capture_errors_json(function(self)

end))

app:patch('/communities/:id/channels', helpers.capture_errors_json(function(self)

end))
end
99 changes: 99 additions & 0 deletions applications/users.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
local lapis = require 'lapis'
local validate = require 'lapis.validate'
local encoding = require 'lapis.util.encoding'
local helpers = require 'lapis.application'
local Users = require 'models.users'
local jwt = require 'resty.jwt'
local inspect = require 'inspect' -- snip snooop

local rand = require 'openssl.rand'
local argon2 = require 'argon2'
local uuid = require 'util.uuid'

return function(app)
app:post('/users', helpers.capture_errors_json(function(self)
self.res.headers["Access-Control-Allow-Origin"] = "*"
print(inspect(self.params))
validate.assert_valid(self.params, {
{ 'username', exists = true, min_length = 3, max_length = 16, matches_pattern = '^%a+$', 'InvalidUsername' },
{ 'password', exists = true, min_length = 8, max_length = 128, 'InvalidPassword' },
{ 'email', exists = true, min_length = 3, max_length = 128, 'InvalidEmail' }
})

local salt = encoding.encode_base64(assert(rand.bytes(32))) -- 256 bit salt because we paranoid bois
local hashed = assert(argon2.hash_encoded(self.params.password, salt, {
variant = argon2.variants.argon2_id,
parallelism = 2,
m_cost = 2 ^ 18,
t_cost = 8
}))

local user = Users:create({
username = self.params.username,
password = hashed,
email = self.params.email,
id = assert(uuid()),
avatar = 'https://cdn.nekos.life/avatar/avatar_48.png',
discriminator = 6969
})

return {
status = 201,
json = {
id = user.id,
username = user.username,
avatar = user.avatar,
discriminator = user.discriminator
}
}
end))

app:post('/users/login', helpers.capture_errors_json(function(self)
print(inspect(self.params))
self.res.headers["Access-Control-Allow-Origin"] = "*"
validate.assert_valid(self.params, {
{ 'email', exists = true, min_length = 3, max_length = 128, matches_pattern = '^%a+', 'InvalidEmail' },
{ 'password', exists = true, min_length = 8, max_length = 128, 'InvalidPassword' }
})


local user = Users:find({ email = self.params.email })
if not user then
helpers.yield_error('NotFound')
end

local ok, err = argon2.verify(user.password, self.params.password)
if ok and not err then
return {
json = {
authorization = true,
id = user.id,
username = user.username,
avatar = user.avatar,
discriminator = user.discriminator
}
}
else
helpers.yield_error('WrongPassword')
end
end))

app:get('/users/:id', helpers.capture_errors_json(function(self)
self.res.headers["Access-Control-Allow-Origin"] = "*"
validate.assert_valid(self.params, {}) -- TODO: Validate UUID

local user = Users:find({ id = self.params.id })
if not user then
helpers.yield_error('NotFound')
end

return {
json = {
id = user.id,
username = user.username,
avatar = user.avatar,
discriminator = user.discriminator
}
}
end))
end
10 changes: 10 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local config = require("lapis.config")

config("development", {
postgres = {
host = "127.0.0.1",
user = "postgres",
password = "password",
database = "neko-chat"
}
})
52 changes: 52 additions & 0 deletions migrations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local schema = require 'lapis.db.schema'
local types = schema.types

return {
[1588207587] = function()
schema.create_table('users', {
{ 'id', 'uuid NOT NULL' },
{ 'username', types.text },
{ 'avatar', types.text },
{ 'password', types.text },
{ 'discriminator', types.integer },
{ 'email', types.text },

'PRIMARY KEY (id)'
})

schema.create_table('messages', {
{ 'id', 'uuid NOT NULL' },
{ 'author_id', 'uuid NOT NULL' },
{ 'content', types.text },
{ 'time', types.time },
{ 'channel_id', 'uuid NOT NULL' },

'PRIMARY KEY (id)'
})

schema.create_table('communities', {
{ 'id', 'uuid NOT NULL' },
{ 'icon', types.text },
{ 'name', types.text },
{ 'large', types.boolean },

'PRIMARY KEY (id)'
})

schema.create_table('channels', {
{ 'id', 'uuid NOT NULL' },
{ 'name', types.text },
{ 'community_id', 'uuid NOT NULL' },

'PRIMARY KEY (id)'
})

schema.create_table('members', {
{ 'id', 'uuid NOT NULL' },
{ 'user_id', 'uuid NOT NULL' },
{ 'community_id', 'uuid NOT NULL' },

'PRIMARY KEY (id)'
})
end
}
10 changes: 10 additions & 0 deletions models/channels.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local Model = require('lapis.db.model').Model

local Channels = Model:extend('channels', {
relations = {
{ 'messages', has_many = 'messages' },
{ 'community', belongs_to = 'communities' }
}
})

return Channels
10 changes: 10 additions & 0 deletions models/communities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local Model = require('lapis.db.model').Model

local Communities = Model:extend('communities', {
relations = {
{ 'channels', has_many = 'channels' },
{ 'members', has_many = 'members' }
}
})

return Communities
8 changes: 8 additions & 0 deletions models/members.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local Model = require('lapis.db.model').Model

local Members = Model:extend('members', {
{ 'user', belongs_to = 'users' },
{ 'member', belongs_to = 'communities' }
})

return Members
10 changes: 10 additions & 0 deletions models/messages.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local Model = require('lapis.db.model').Model

local Messages = Model:extend('messages', {
relations = {
{ 'author', belongs_to = 'users' },
{ 'channel', belongs_to = 'channel' }
}
})

return Messages
10 changes: 10 additions & 0 deletions models/users.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local Model = require('lapis.db.model').Model

local Users = Model:extend('users', {
relations = {
{ 'messages', has_many = 'messages' },
{ 'members', has_many = 'members' }
}
})

return Users
32 changes: 32 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
worker_processes ${{NUM_WORKERS}};
error_log stderr notice;
daemon off;
pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;

server {
listen ${{PORT}};
lua_code_cache ${{CODE_CACHE}};

location / {
default_type text/html;
content_by_lua_block {
require("lapis").serve("app")
}
}

location /static/ {
alias static/;
}

location /favicon.ico {
alias static/favicon.ico;
}
}
}
5 changes: 5 additions & 0 deletions nuke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

docker stop nekos-dev-postgres
docker rm nekos-dev-postgres
docker run --name nekos-dev-postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -e POSTGRES_DB=neko-chat -d postgres
30 changes: 30 additions & 0 deletions reservednames.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
return {
'irs',
'cra',
'ccp',
'apple',
'google',
'pornhub',
'joelhub',
'joelinnatical',
'innatical',
'innatical.com',
'atik',
'atiktech',
'atiktech.co',
'points',
'points.city',
'tailosive',
'adam',
'lleyton',
'joel',
'joelosher',
'josh',
'discord',
'slack',
'minecraft',
'roblox',
'airbus',
'boeing',
'microsoft'
}
20 changes: 20 additions & 0 deletions util/jwt.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local jwt = require 'resty.jwt'

local function generateLoginToken(id)
local keyfile = io.open('jwtrs256.key', 'r')
local key = keyfile:read('a')
local table = {
header = {
typ = 'JWT',
alg = 'RS256'
},
payload = {
user = {
id = id
}
}
}
return jwt:sign(key, table)
end

return generateLoginToken

0 comments on commit 3d028dc

Please sign in to comment.