mirror of
https://github.com/langgenius/dify.git
synced 2025-12-04 14:56:42 +00:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <xh001x@hotmail.com>
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import json
|
|
import uuid
|
|
|
|
from core.plugin.impl.base import BasePluginClient
|
|
from extensions.ext_redis import redis_client
|
|
|
|
|
|
class OAuthProxyService(BasePluginClient):
|
|
# Default max age for proxy context parameter in seconds
|
|
__MAX_AGE__ = 5 * 60 # 5 minutes
|
|
__KEY_PREFIX__ = "oauth_proxy_context:"
|
|
|
|
@staticmethod
|
|
def create_proxy_context(
|
|
user_id: str,
|
|
tenant_id: str,
|
|
plugin_id: str,
|
|
provider: str,
|
|
credential_id: str | None = None,
|
|
):
|
|
"""
|
|
Create a proxy context for an OAuth 2.0 authorization request.
|
|
|
|
This parameter is a crucial security measure to prevent Cross-Site Request
|
|
Forgery (CSRF) attacks. It works by generating a unique nonce and storing it
|
|
in a distributed cache (Redis) along with the user's session context.
|
|
|
|
The returned nonce should be included as the 'proxy_context' parameter in the
|
|
authorization URL. Upon callback, the `use_proxy_context` method
|
|
is used to verify the state, ensuring the request's integrity and authenticity,
|
|
and mitigating replay attacks.
|
|
"""
|
|
context_id = str(uuid.uuid4())
|
|
data = {
|
|
"user_id": user_id,
|
|
"plugin_id": plugin_id,
|
|
"tenant_id": tenant_id,
|
|
"provider": provider,
|
|
}
|
|
if credential_id:
|
|
data["credential_id"] = credential_id
|
|
redis_client.setex(
|
|
f"{OAuthProxyService.__KEY_PREFIX__}{context_id}",
|
|
OAuthProxyService.__MAX_AGE__,
|
|
json.dumps(data),
|
|
)
|
|
return context_id
|
|
|
|
@staticmethod
|
|
def use_proxy_context(context_id: str):
|
|
"""
|
|
Validate the proxy context parameter.
|
|
This checks if the context_id is valid and not expired.
|
|
"""
|
|
if not context_id:
|
|
raise ValueError("context_id is required")
|
|
# get data from redis
|
|
key = f"{OAuthProxyService.__KEY_PREFIX__}{context_id}"
|
|
data = redis_client.get(key)
|
|
if not data:
|
|
raise ValueError("context_id is invalid")
|
|
redis_client.delete(key)
|
|
return json.loads(data)
|