-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnginx.conf
71 lines (59 loc) · 1.68 KB
/
nginx.conf
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
error_log /dev/stderr info;
events {
worker_connections 1024;
}
http {
init_by_lua_block {
local fernet = require "resty.fernet"
local key = fernet:generate_key()
ngx.shared.fernet = fernet:new(key)
}
server {
listen 8080;
location = /login {
content_by_lua_block {
local cjson = require "cjson.safe"
local json, err = cjson.encode({ username="resty", api_key="secret" })
if err then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
local token, err = ngx.shared.fernet:encrypt(json)
if err then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
ngx.header["Set-Cookie"] = "session=" .. token .. ";"
ngx.exit(ngx.HTTP_NO_CONTENT)
}
}
location / {
content_by_lua_block {
local session_cookie = ngx.var.cookie_session
if not session_cookie then
ngx.log(ngx.INFO, "no session cookie")
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
local json, err = ngx.shared.fernet:decrypt(session_cookie)
if err then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
local cjson = require "cjson.safe"
local session, err = cjson.decode(json)
if err then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
ngx.log(ngx.INFO, session.api_key)
ngx.say("Hello " .. session.username)
-- could store for use in other phases
-- ngx.ctx.session = session
ngx.exit(ngx.HTTP_OK)
}
}
location = /health {
return 204;
}
}
}