-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.nginx
81 lines (70 loc) · 2.84 KB
/
backend.nginx
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
worker_processes 1;
events {
worker_connections 512;
}
error_log logs/error.log debug;
http {
server {
listen 0.0.0.0:80;
location /tokens {
client_max_body_size 100k;
client_body_buffer_size 100k;
content_by_lua_block {
local cjson = require "cjson.safe"
local jwt = require "resty.jwt"
if ngx.req.get_method() ~= "POST" then
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.header.content_type = "application/json; charset=utf-8"
ngx.say(cjson.encode({ message = "Method not allow" }))
return ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
ngx.log(ngx.ERR, "body not found")
end
ngx.log(ngx.DEBUG, "body found::::", body)
cjson.decode_array_with_array_mt(true)
local args = cjson.decode(body)
cjson.decode_array_with_array_mt(false)
if (args.refreshToken and args.refreshToken == "qwertyuiopoiuytrewq") or (args.email == "admin" and args.password == "admin") then
local jwt_token = jwt:sign(
"qwertyuiop",
{
header={typ="JWT", alg="HS256"},
payload={
user="admin",
exp=ngx.now() + 60*3
}
}
)
ngx.status = ngx.HTTP_OK
ngx.header.content_type = "application/json; charset=utf-8"
ngx.say(cjson.encode({ status = true, token = jwt_token, refreshToken = "qwertyuiopoiuytrewq" }))
return ngx.exit(ngx.HTTP_OK)
end
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.header.content_type = "application/json; charset=utf-8"
ngx.say(cjson.encode({ message = "Not a valid token." }))
return ngx.exit(ngx.HTTP_UNAUTHORIZED)
}
}
location /v1/user {
content_by_lua_block {
ngx.say("Backend request_uri: ", ngx.var.request_uri)
ngx.say("Method: ", ngx.req.get_method())
ngx.say("Headers:")
local h, err = ngx.req.get_headers()
for k, v in pairs(h) do
if type(v) == "table" then
for i = 1, #v do
ngx.say(k, ": ", v[i])
end
else
ngx.say(k, ": ", v)
end
end
}
}
}
}