Software /
code /
prosody
Annotate
util/jsonpointer.lua @ 12699:b3d0c1457584
util.jwt: Add support for RSA-based algorithms (RS256, PS256)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 02 Jul 2022 12:26:43 +0100 |
parent | 12499:03e307952816 |
child | 12781:22066b02887f |
rev | line source |
---|---|
12499
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
1 local m_type = math.type or function (n) |
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
2 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; |
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
3 end; |
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
4 |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local function unescape_token(escaped_token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local unescaped = escaped_token:gsub("~1", "/"):gsub("~0", "~") |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 return unescaped |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local function resolve_json_pointer(ref, path) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local ptr_len = #path + 1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 for part, pos in path:gmatch("/([^/]*)()") do |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local token = unescape_token(part) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 if not (type(ref) == "table") then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 return nil |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local idx = next(ref) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 if type(idx) == "string" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 new_ref = ref[token] |
12499
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
22 elseif m_type(idx) == "integer" then |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local i = tonumber(token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 if token == "-" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 i = #ref + 1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end |
12496
87c3d45208ef
util.jsonpointer: Fix off-by-one in array resolution
Kim Alvefur <zash@zash.se>
parents:
12131
diff
changeset
|
27 new_ref = ref[i + 1] |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 else |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 return nil, "invalid-table" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 if pos == ptr_len then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 return new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 elseif type(new_ref) == "table" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 ref = new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 elseif not (type(ref) == "table") then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 return nil, "invalid-path" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 return ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 return { resolve = resolve_json_pointer } |