This repository was archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.lua
More file actions
140 lines (125 loc) · 3.58 KB
/
Copy pathapp.lua
File metadata and controls
140 lines (125 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package.path = package.path .. ';./?/init.lua'
local lapis = require 'lapis'
local validate = require 'lapis.validate'
local config = require 'lapis.config'.get()
local jwt = require 'resty.jwt'
local validators = require 'resty.jwt-validators'
local raven = require 'raven'
local UsersModel = require 'models.users'
local app = lapis.Application()
app.include = function(self, a)
self.__class.include(self, a, nil, self)
end
-- Validators
require 'util.validators.uuid'
require 'util.validators.matches_regexp'
require 'util.validators.is_array'
function validate.validate_functions.exists(input)
return not not input, '%s must be provided'
end
local rvn = raven.new {
sender = require('raven.senders.ngx').new {
dsn = 'https://15652eb7625a4485bbabde18e37fed37@o271654.ingest.sentry.io/5453638'
}
}
-- Middleware
app:before_filter(function(self)
self.res.headers['Access-Control-Allow-Origin'] = '*'
self.res.headers['Access-Control-Allow-Methods'] = '*'
self.res.headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type, Cache-Control'
if self.req.method == 'OPTIONS' then
self:write({
status = 203
})
return
end
if self.route_name ~= 'users.login' and self.route_name ~= 'integrations.events' and self.route_name ~= 'integrations.commands' and self.route_name ~= 'integrations.reply' and self.route_name ~= 'users.register' and self.route_name ~= 'users.newsletter' and self.route_name ~= 'voice.users' and self.route_name ~= 'voice.started' and self.route_name ~= 'channels.webhook' then
local token = jwt:verify(config.jwt.public, self.req.headers.Authorization or self.params.authorization, {
iss = validators.equals('chat.innatical.com'),
aud = validators.equals('chat.innatical.com'),
nbf = validators.is_not_before(),
exp = validators.is_not_expired()
})
if token.verified == true then
local user = UsersModel:find({ id = token.payload.sub })
if user then
if not user.disabled then
self.user = user
else
self:write({
status = 403,
json = {
errors = { 'DisabledUser' }
}
})
end
else
self:write({
status = 404,
json = {
errors = { 'UserNotFound' }
}
})
end
else
self:write({
status = 403,
json = {
errors = { 'InvalidAuthorization' }
}
})
end
end
end)
if config._name == 'production' then
function app:handle_error(err, trace)
print('ERROR:', err, trace)
rvn:captureException({{
type = err,
value = trace,
module = '__builtins__'
}}, {
transaction = ngx.var.request_uri
})
return {
status = 500,
json = {
errors = {
'ServerError'
}
}
}
end
elseif config._name == 'development' then
function app:handle_error(err, trace)
print('ERROR:', err, trace)
return {
status = 500,
layout = false,
err .. trace
}
end
end
function app:handle_404()
return { status = 404, json = {
errors = {
'RouteNotFound'
}
}}
end
-- Routes
app:include('routes.users')
app:include('routes.channels')
app:include('routes.communities')
app:include('routes.events')
app:include('routes.invites')
app:include('routes.conversations')
app:include('routes.messages')
app:include('routes.voice')
app:include('routes.groups')
app:include('routes.admin')
app:include('routes.members')
app:include('routes.relationships')
app:include('routes.products')
app:include('routes.integrations')
return app