Skip to content

Commit 7277e48

Browse files
committed
Add user parameter replacement functionality in OIDC API Proxy
1 parent 5e4ae58 commit 7277e48

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

endpoints/helpers/endpoint.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import httpx
55
import werkzeug
6+
import werkzeug.datastructures
67

78

89
def proxy_response(
@@ -128,3 +129,26 @@ def check_llm_streaming_request(request: werkzeug.Request) -> Tuple[bool, bool]:
128129
is_stream = True
129130

130131
return is_llm, is_stream
132+
133+
134+
def replace_user_params(
135+
user: str,
136+
args: werkzeug.datastructures.MultiDict[str, str],
137+
json: Any | None,
138+
data: werkzeug.datastructures.ImmutableMultiDict[str, str],
139+
) -> Tuple[
140+
werkzeug.datastructures.MultiDict[str, str],
141+
Any | None,
142+
werkzeug.datastructures.ImmutableMultiDict[str, str],
143+
]:
144+
if not user:
145+
return args, json, data
146+
147+
if args:
148+
args["user"] = user
149+
if json is not None:
150+
json["user"] = user
151+
if data:
152+
data["user"] = user
153+
154+
return args, json, data

endpoints/oidc_api_proxy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dify_plugin import Endpoint
55
from werkzeug import Request, Response
66

7-
from endpoints.helpers.endpoint import OidcApiProxyErrorResponse, proxy_response
7+
from endpoints.helpers.endpoint import OidcApiProxyErrorResponse, proxy_response, replace_user_params
88
from endpoints.helpers.oidc import OpenIDConnectDiscoveryProvider
99

1010

@@ -16,6 +16,7 @@ def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
1616
oidc_scope = str(settings.get("oidc_scope", ""))
1717
dify_api_url = str(settings.get("dify_api_url", ""))
1818
dify_api_key = str(settings.get("dify_api_key", ""))
19+
dify_replace_user_param_claim = str(settings.get("dify_replace_user_param_claim", ""))
1920

2021
# prepare dify api url by removing trailing slash
2122
if dify_api_url.endswith("/"):
@@ -43,7 +44,7 @@ def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
4344
# Verify access token
4445
try:
4546
oidc_provider = OpenIDConnectDiscoveryProvider(self.session, oidc_issuer, oidc_audience, oidc_scope)
46-
_ = oidc_provider.verify_access_token(access_token)
47+
oidc_claims = oidc_provider.verify_access_token(access_token)
4748
except Exception as e:
4849
return OidcApiProxyErrorResponse(str(e), 401)
4950

@@ -61,8 +62,14 @@ def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
6162
**({"Content-Type": r.headers["Content-Type"]} if r.headers.get("Content-Type") else {}),
6263
}
6364

64-
# prepare json if request is json
65+
# prepare params, json and data
66+
params = r.args
6567
json = r.get_json() if r.is_json else None
68+
data = r.form
69+
70+
# replace user params
71+
user = str(oidc_claims.get(dify_replace_user_param_claim, ""))
72+
params, json, data = replace_user_params(user, params, json, data)
6673

6774
# prepare files if request has files
6875
files = [
@@ -72,7 +79,7 @@ def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
7279
# Forward request to Dify API with Syncronous HTTP Client
7380
try:
7481
return proxy_response(
75-
request=r, method=r.method, url=url, headers=headers, params=r.args, json=json, data=r.form, files=files
82+
request=r, method=r.method, url=url, headers=headers, params=params, json=json, data=data, files=files
7683
)
7784
except Exception as e:
7885
print(str(e))

group/oidc_api_proxy.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ settings:
3434
en_US: Dify - API Key
3535
placeholder:
3636
en_US: Please input Dify API Key
37+
- name: dify_replace_user_param_claim
38+
type: text-input
39+
required: false
40+
label:
41+
en_US: Dify - Replace user parameters using OIDC claims
42+
placeholder:
43+
en_US: Please input the claim name to use for replacement (if required).
3744
endpoints:
3845
- endpoints/definitions/get/conversations.yaml
3946
- endpoints/definitions/get/info.yaml

0 commit comments

Comments
 (0)