Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add jwe decrypt plugin #10252

Merged
merged 23 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6223178
feat: add jwe decrypt plugin
fishioon Sep 25, 2023
88ca63b
feat: add unit test
fishioon Oct 3, 2023
53167a1
feat: fix ci
fishioon Oct 8, 2023
be97b1b
feat: fix cr
fishioon Nov 14, 2023
697aa4b
Merge branch 'master' of github.com:apache/apisix into feat-jwe
shreemaan-abhishek Nov 21, 2023
96481ea
fix test error
shreemaan-abhishek Nov 21, 2023
504bd7a
Merge branch 'master' of github.com:apache/apisix into feat-jwe
shreemaan-abhishek Nov 22, 2023
22164eb
Update docs and add missing `secret` attribute
kayx23 Nov 24, 2023
e919e79
fix test name
shreemaan-abhishek Nov 25, 2023
e24587f
apply suggestions
shreemaan-abhishek Nov 25, 2023
7ca6c57
use `is_base64_encoded`
shreemaan-abhishek Nov 25, 2023
fa3e66a
remove log
shreemaan-abhishek Nov 25, 2023
d21d9d7
two line breaks
shreemaan-abhishek Nov 25, 2023
e1e7e1d
Merge branch 'feat-jwe' of github.com:fishioon/apisix into feat-jwe
shreemaan-abhishek Nov 25, 2023
1088887
use `resty.aes` instead of a `resty.openssl`
shreemaan-abhishek Nov 29, 2023
f370d41
use `dec`
shreemaan-abhishek Nov 29, 2023
a24e3fd
Apply suggestions from code review
shreemaan-abhishek Nov 30, 2023
b843ec4
fix test title
shreemaan-abhishek Nov 30, 2023
f2ab448
remove incorrect attributes from consumer conf in test
shreemaan-abhishek Nov 30, 2023
c2d889a
fix test title name
shreemaan-abhishek Nov 30, 2023
098fa05
don't sleep
shreemaan-abhishek Nov 30, 2023
1b43ae5
fix method usage
shreemaan-abhishek Nov 30, 2023
6b20a33
add more tests
shreemaan-abhishek Nov 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 229 additions & 0 deletions apisix/plugins/jwe-decrypt.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local consumer_mod = require("apisix.consumer")
local base64 = require("ngx.base64")
local plugin_name = "jwe-decrypt"
local ngx = ngx
local sub_str = string.sub
local cipher = require("resty.openssl.cipher").new("aes-256-gcm")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fishioon have you noticed this comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any issue in using the current method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we use lua-resty-string, we don't need to import any new library


local schema = {
type = "object",
properties = {
header = {
type = "string",
default = "Authorization"
},
forward_header = {
type = "string",
default = "Authorization"
},
strict = {
type = "boolean",
default = true
}
},
required = { "header", "forward_header" },
}

local consumer_schema = {
shreemaan-abhishek marked this conversation as resolved.
Show resolved Hide resolved
type = "object",
properties = {
key = { type = "string" },
secret = { type = "string", minLength = 32 },
base64_secret = { type = "boolean" },
},
required = { "key", "secret" },
}


local _M = {
version = 0.1,
priority = 2509,
type = 'auth',
name = plugin_name,
schema = schema,
consumer_schema = consumer_schema
}


function _M.check_schema(conf, schema_type)
core.log.info("input conf: ", core.json.delay_encode(conf))
if schema_type == core.schema.TYPE_CONSUMER then
return core.schema.check(consumer_schema, conf)
end
return core.schema.check(schema, conf)
monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved
end

local function get_secret(conf)
local secret = conf.secret

if conf.base64_secret then
return base64.decode_base64(secret)
end

return secret
end

monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved

local function load_jwe_token(jwe_token)
local o = { valid = false }
o.header, o.enckey, o.iv, o.ciphertext, o.tag = jwe_token:match("(.-)%.(.-)%.(.-)%.(.-)%.(.*)")
if not o.header then
return o
end
local he = base64.decode_base64url(o.header)
if not he then
return o
end
o.header_obj = core.json.decode(he)
if not o.header_obj then
return o
end
o.valid = true
return o
end


local function jwe_decrypt_with_obj(o, consumer)
local secret = get_secret(consumer.auth_conf)
local dec = base64.decode_base64url
return cipher:decrypt(secret, dec(o.iv), dec(o.ciphertext), false, o.header, dec(o.tag))
end


local function jwe_encrypt(o, consumer)
local secret = get_secret(consumer.auth_conf)
local enc = base64.encode_base64url
o.ciphertext = cipher:encrypt(secret, o.iv, o.plaintext, false, o.header)
o.tag = cipher:get_aead_tag()
return o.header .. ".." .. enc(o.iv) .. "." .. enc(o.ciphertext) .. "." .. enc(o.tag)
end


local function get_consumer(key)
local consumer_conf = consumer_mod.plugin(plugin_name)
if not consumer_conf then
return nil
end
local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
if not consumers then
return nil
end
core.log.info("consumers: ", core.json.delay_encode(consumers))
return consumers[key]
end


local function fetch_jwe_token(conf, ctx)
local token = core.request.header(ctx, conf.header)
if token then
local prefix = sub_str(token, 1, 7)
if prefix == 'Bearer ' or prefix == 'bearer ' then
return sub_str(token, 8)
end

return token
end
end


function _M.rewrite(conf, ctx)
-- fetch token and hide credentials if necessary
local jwe_token, err = fetch_jwe_token(conf, ctx)
if not jwe_token and conf.strict then
core.log.info("failed to fetch JWE token: ", err)
return 403, { message = "missing JWE token in request" }
end

local jwe_obj = load_jwe_token(jwe_token)
if not jwe_obj.valid then
return 400, { message = "JWE token invalid" }
end

if not jwe_obj.header_obj.kid then
return 400, { message = "missing kid in JWE token" }
end

local consumer = get_consumer(jwe_obj.header_obj.kid)
if not consumer then
return 400, { message = "invalid kid in JWE token" }
end

local plaintext, err = jwe_decrypt_with_obj(jwe_obj, consumer)
if err ~= nil then
return 400, { message = "failed to decrypt JWE token" }
end
core.request.set_header(ctx, conf.forward_header, plaintext)
end

local function gen_token()
local args = core.request.get_uri_args()
if not args or not args.key then
return core.response.exit(400)
end

local key = args.key
local payload = args.payload
if payload then
payload = ngx.unescape_uri(payload)
end

local consumer = get_consumer(key)
if not consumer then
return core.response.exit(404)
end

core.log.info("consumer: ", core.json.delay_encode(consumer))

local iv = args.iv
if not iv then
-- TODO: random bytes
iv = "123456789012"
end

local obj = {
iv = iv,
plaintext = payload,
header_obj = {
kid = key,
alg = "dir",
enc = "A256GCM",
},
}
obj.header = base64.encode_base64url(core.json.encode(obj.header_obj))
local jwe_token = jwe_encrypt(obj, consumer)
if jwe_token then
return core.response.exit(200, jwe_token)
end

return core.response.exit(404)
end


function _M.api()
return {
{
methods = { "GET" },
uri = "/apisix/plugin/jwe/encrypt",
handler = gen_token,
}
}
end

return _M
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ plugins: # plugin list (sorted by priority)
- hmac-auth # priority: 2530
- basic-auth # priority: 2520
- jwt-auth # priority: 2510
- jwe-decrypt # priority: 2509
- key-auth # priority: 2500
- consumer-restriction # priority: 2400
- forward-auth # priority: 2002
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"items": [
"plugins/key-auth",
"plugins/jwt-auth",
"plugins/jwe-decrypt",
"plugins/basic-auth",
"plugins/authz-keycloak",
"plugins/authz-casdoor",
Expand Down
Loading
Loading