Skip to content

Commit 873f318

Browse files
committed
recommend users to use access_by_lua_block instead of content_by_lua_block
1 parent 21cf6f8 commit 873f318

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

.memory_leak/test/teste.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ __DATA__
1212
content_by_lua_block {
1313
local libjwt = require("resty.libjwt")
1414
local cjson = require("cjson.safe")
15-
local claim, err = libjwt.validate({
16-
["jwks_files"] = {"/usr/share/tokens/jwks.json"},
15+
local token, err = libjwt.validate({
16+
jwks_files = {"/usr/share/tokens/jwks.json"},
1717
})
18-
if claim then
18+
if token then
1919
ngx.status = ngx.HTTP_OK
2020
local response = {
2121
message = "ok"

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,19 @@ Here is an example of how to configure **libjwt** in `nginx.conf`:
7878
server {
7979
listen 80;
8080
location /private {
81-
content_by_lua_block {
81+
access_by_lua_block {
8282
local libjwt = require("resty.libjwt")
83-
local cjson = require("cjson.safe")
84-
local claim, err = libjwt.validate({
83+
local token, err = libjwt.validate({
8584
jwks_files = {"/usr/share/tokens/jwks.json"}
8685
})
87-
if claim then
88-
local claim_str = cjson.encode(claim) or "Invalid Claim"
89-
ngx.log(ngx.ERR, "JWT Claims: " .. claim_str)
90-
ngx.status = ngx.HTTP_OK
91-
return ngx.say(claim_str)
86+
if token then
87+
-- You may add logic as needed, accessing the JWT claims:
88+
-- token.claim.sub
89+
-- token.claim.iss
9290
end
93-
ngx.status = ngx.HTTP_UNAUTHORIZED
94-
local response = { message = "Unauthorized" }
95-
return ngx.say(cjson.encode(response))
9691
}
92+
93+
proxy_pass http://your_backend;
9794
}
9895
}
9996

e2e/nginx/nginx.private.jwks.conf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ http {
1818
content_by_lua_block {
1919
local libjwt = require("resty.libjwt")
2020
local cjson = require("cjson.safe")
21-
local claim, err = libjwt.validate({
22-
["jwks_files"] = {"/usr/share/tokens/jwks.json"},
21+
local token, err = libjwt.validate({
22+
jwks_files = {"/usr/share/tokens/jwks.json"},
2323
})
2424
ngx.header.content_type = "application/json"
2525
if err and err ~= "" then
@@ -29,9 +29,9 @@ http {
2929
}
3030
return ngx.say(cjson.encode(response))
3131
end
32-
if claim then
33-
local claim_str = cjson.encode(claim) or "Invalid Claim"
34-
ngx.log(ngx.ERR, "JWT Claims: " .. claim_str)
32+
if token then
33+
local claim_str = cjson.encode(token.claim) or "Invalid token"
34+
ngx.log(ngx.ERR, "Token Claims: " .. claim_str)
3535
ngx.status = ngx.HTTP_OK
3636
return ngx.say(claim_str)
3737
end

e2e/nginx/replace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func ReplaceNginxConfig(pathFile string, newPaths []string) (file []byte, err er
1414
return nil, err
1515
}
1616
config := string(content)
17-
re := regexp.MustCompile(`\["jwks_files"\]\s*=\s*\{[^}]*\}`)
17+
re := regexp.MustCompile(`jwks_files\s*=\s*\{[^}]*\}`)
1818
newValue := fmt.Sprintf(`["jwks_files"] = {%s}`, strings.Join(newPaths, `, `))
1919
modifiedConfig := re.ReplaceAllString(config, newValue)
2020
return []byte(modifiedConfig), nil

nginx.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ http {
1515
}
1616

1717
location /private {
18-
content_by_lua_block {
18+
access_by_lua_block {
1919
local libjwt = require("resty.libjwt")
2020
local cjson = require("cjson.safe")
21-
local claim, err = libjwt.validate({
21+
local token, err = libjwt.validate({
2222
["jwks_files"] = {"/usr/share/tokens/jwks.json"},
2323
})
24-
if claim then
25-
local claim_str = cjson.encode(claim) or "Invalid Claim"
24+
if token then
25+
local claim_str = cjson.encode(claim) or "Invalid Token"
2626
ngx.status = ngx.HTTP_OK
2727
return ngx.say(claim_str)
2828
end

0 commit comments

Comments
 (0)