Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(api): thin OAuth controllers and extract application services (#40692)

Byron.wang30e60698514a
diff --git a/api/.importlinter b/api/.importlinter
index 227d512372f..3e0e106a1d3 100644
--- a/api/.importlinter
+++ b/api/.importlinter
@@ -64,7 +64,6 @@ ignore_imports =
core.app.task_pipeline.message_cycle_manager -> services.annotation_service
core.app.workflow.layers.persistence -> services.workflow.inspector_events
core.datasource.datasource_manager -> services.datasource_provider_service
- core.helper.creators -> services.oauth_server
core.helper.credential_utils -> services.enterprise.plugin_manager_service
core.helper.credential_utils -> services.feature_service
core.indexing_runner -> services.vector_space_admission_service
@@ -116,8 +115,6 @@ ignore_imports =
libs.login -> models.model
libs.oauth -> core.helper.http_client_pooling
libs.oauth_bearer -> models
- libs.oauth_data_source -> core.db.session_factory
- libs.oauth_data_source -> core.helper.http_client_pooling
libs.rsa -> extensions.ext_storage
libs.workspace_permission -> services.enterprise.enterprise_service
libs.workspace_permission -> services.feature_service
@@ -308,6 +305,40 @@ forbidden_modules =
sqlalchemy
werkzeug
+[importlinter:contract:data-source-oauth-service-boundary]
+name = Data source OAuth application service is framework and persistence neutral
+type = forbidden
+source_modules =
+ services.data_source_oauth_service
+forbidden_modules =
+ configs
+ controllers
+ extensions
+ flask
+ httpx
+ libs
+ models
+ repositories
+ sqlalchemy
+ werkzeug
+
+[importlinter:contract:oauth-server-application-service-boundary]
+name = OAuth server application service is framework and persistence neutral
+type = forbidden
+source_modules =
+ services.oauth_server_service
+forbidden_modules =
+ configs
+ controllers
+ extensions
+ flask
+ httpx
+ libs
+ models
+ repositories
+ sqlalchemy
+ werkzeug
+
[importlinter:contract:workspace-member-query-service-boundary]
name = Workspace member query application service is framework and persistence neutral
type = forbidden
diff --git a/api/controllers/console/app/app.py b/api/controllers/console/app/app.py
index 8efbaf70d1f..af1d9d6c971 100644
--- a/api/controllers/console/app/app.py
+++ b/api/controllers/console/app/app.py
@@ -43,6 +43,7 @@ from core.ops.ops_trace_manager import OpsTraceManager
from core.rag.entities import PreProcessingRule, Rule, Segmentation
from core.rag.retrieval.retrieval_methods import RetrievalMethod
from core.trigger.constants import TRIGGER_NODE_TYPES
+from extensions.ext_application_services import application_services
from extensions.ext_database import db
from fields.base import ResponseModel
from graphon.enums import WorkflowExecutionStatus
@@ -1084,7 +1085,17 @@ class AppPublishToCreatorsPlatformApi(Resource):
dsl_bytes = dsl_content.encode("utf-8")
claim_code = upload_dsl(dsl_bytes)
- redirect_url = get_redirect_url(current_user_id, claim_code)
+ # TODO: Move this configuration and OAuth orchestration into the Creators Platform application service
+ # when that domain is refactored. This controller-level integration is a temporary compatibility bridge.
+ oauth_code = None
+ client_id = str(dify_config.CREATORS_PLATFORM_OAUTH_CLIENT_ID or "")
+ if client_id:
+ authorization = application_services().oauth_server.issue_authorization_code(
+ client_id=client_id,
+ account_id=current_user_id,
+ )
+ oauth_code = authorization.code
+ redirect_url = get_redirect_url(claim_code, oauth_code=oauth_code)
return RedirectUrlResponse(redirect_url=redirect_url).model_dump(mode="json")
diff --git a/api/controllers/console/auth/data_source_oauth.py b/api/controllers/console/auth/data_source_oauth.py
index 6ad2efa360a..792e698fab9 100644
--- a/api/controllers/console/auth/data_source_oauth.py
+++ b/api/controllers/console/auth/data_source_oauth.py
@@ -1,39 +1,50 @@
import logging
+from http import HTTPStatus
+from urllib.parse import parse_qsl, quote, urlencode, urlsplit, urlunsplit
from uuid import UUID
-import httpx
-from flask import current_app, redirect, request
+from flask import redirect
from flask_restx import Resource
-from pydantic import BaseModel, Field
+from pydantic import BaseModel, Field, ValidationError
from configs import dify_config
from controllers.common.fields import RedirectResponse
-from controllers.common.schema import query_params_from_model, register_response_schema_model, register_schema_models
-from libs.login import login_required
-from libs.oauth_data_source import NotionOAuth
+from controllers.common.schema import (
+ query_params_from_model,
+ query_params_from_request,
+ register_response_schema_model,
+ register_response_schema_models,
+ register_schema_models,
+)
+from controllers.console.flask_admission import console_account_admission
+from core.rbac import RBACPermission, RBACResourceScope
+from extensions.ext_application_services import application_services
+from fields.base import ResponseModel
+from libs.helper import dump_response
+from machinery.context import RequestContext
+from models.account import TenantAccountRole
+from services.data_source_oauth_service import (
+ DataSourceOAuthConfigurationError,
+ DataSourceOAuthError,
+ InvalidDataSourceOAuthCodeError,
+ InvalidDataSourceOAuthProviderError,
+)
+from services.entities.data_source_oauth_entities import DataSourceOAuthCallback
from .. import console_ns
-from ..wraps import (
- RBACPermission,
- RBACResourceScope,
- account_initialization_required,
- is_admin_or_owner_required,
- rbac_permission_required,
- setup_required,
-)
logger = logging.getLogger(__name__)
-class OAuthDataSourceResponse(BaseModel):
+class OAuthDataSourceResponse(ResponseModel):
data: str = Field(description="Authorization URL or 'internal' for internal setup")
-class OAuthDataSourceBindingResponse(BaseModel):
+class OAuthDataSourceBindingResponse(ResponseModel):
result: str = Field(description="Operation result")
-class OAuthDataSourceSyncResponse(BaseModel):
+class OAuthDataSourceSyncResponse(ResponseModel):
result: str = Field(description="Operation result")
@@ -47,26 +58,43 @@ class OAuthDataSourceBindingQuery(BaseModel):
register_schema_models(
+ console_ns,
+ OAuthDataSourceCallbackQuery,
+ OAuthDataSourceBindingQuery,
+)
+register_response_schema_models(
console_ns,
OAuthDataSourceResponse,
OAuthDataSourceBindingResponse,
OAuthDataSourceSyncResponse,
- OAuthDataSourceCallbackQuery,
- OAuthDataSourceBindingQuery,
)
register_response_schema_model(console_ns, RedirectResponse)
+_ADMIN_OR_OWNER_ROLES = frozenset({TenantAccountRole.ADMIN, TenantAccountRole.OWNER})
+
+
+def _invalid_provider_response() -> tuple[dict[str, str], HTTPStatus]:
+ return {"error": "Invalid provider"}, HTTPStatus.BAD_REQUEST
-def get_oauth_providers():
- with current_app.app_context():
- notion_oauth = NotionOAuth(
- client_id=dify_config.NOTION_CLIENT_ID or "",
- client_secret=dify_config.NOTION_CLIENT_SECRET or "",
- redirect_uri=dify_config.CONSOLE_API_URL + "/console/api/oauth/data-source/callback/notion",
- )
- OAUTH_PROVIDERS = {"notion": notion_oauth}
- return OAUTH_PROVIDERS
+def _provider_failure_response(provider: str, error: Exception) -> tuple[dict[str, str], HTTPStatus]:
+ logger.exception("OAuth data source operation failed for provider %s", provider, exc_info=error)
+ return {"error": "OAuth data source process failed"}, HTTPStatus.BAD_REQUEST
+
+
+def _callback_redirect_location(callback: DataSourceOAuthCallback) -> str:
+ callback_value = ("code", callback.code) if callback.code is not None else ("error", callback.error)
+ split_url = urlsplit(dify_config.CONSOLE_WEB_URL)
+ query = [*parse_qsl(split_url.query, keep_blank_values=True), ("type", callback.provider), callback_value]
+ return urlunsplit(
+ (
+ split_url.scheme,
+ split_url.netloc,
+ split_url.path,
+ urlencode(query, quote_via=quote),
+ split_url.fragment,
+ )
+ )
@console_ns.route("/oauth/data-source/<string:provider>")
@@ -75,30 +103,30 @@ class OAuthDataSource(Resource):
@console_ns.doc(description="Get OAuth authorization URL for data source provider")
@console_ns.doc(params={"provider": "Data source provider name (notion)"})
@console_ns.response(
- 200,
+ HTTPStatus.OK,
"Authorization URL or internal setup success",
console_ns.models[OAuthDataSourceResponse.__name__],
)
- @console_ns.response(400, "Invalid provider")
- @console_ns.response(403, "Admin privileges required")
- @is_admin_or_owner_required
- @rbac_permission_required(RBACResourceScope.WORKSPACE, RBACPermission.CREDENTIAL_MANAGE, resource_required=False)
- def get(self, provider: str):
- # The role of the current user in the table must be admin or owner
- OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
- with current_app.app_context():
- oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
- if not oauth_provider:
- return {"error": "Invalid provider"}, 400
- if dify_config.NOTION_INTEGRATION_TYPE == "internal":
- internal_secret = dify_config.NOTION_INTERNAL_SECRET
- if not internal_secret:
- return ({"error": "Internal secret is not set"},)
- oauth_provider.save_internal_access_token(internal_secret)
- return {"data": "internal"}
- else:
- auth_url = oauth_provider.get_authorization_url()
- return {"data": auth_url}, 200
+ @console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid provider")
+ @console_ns.response(HTTPStatus.FORBIDDEN, "Admin privileges required")
+ @console_account_admission(
+ allowed_roles=_ADMIN_OR_OWNER_ROLES,
+ rbac_resource_scope=RBACResourceScope.WORKSPACE,
+ rbac_permission=RBACPermission.CREDENTIAL_MANAGE,
+ rbac_resource_required=False,
+ )
+ def get(self, request_context: RequestContext, provider: str):
+ try:
+ service = application_services().resolve_data_source_oauth(provider)
+ authorization = service.start_authorization(request_context)
+ except InvalidDataSourceOAuthProviderError:
+ return _invalid_provider_response()
+ except DataSourceOAuthConfigurationError as error:
+ return {"error": str(error)}, HTTPStatus.BAD_REQUEST
+ except DataSourceOAuthError as error:
+ return _provider_failure_response(provider, error)
+
+ return dump_response(OAuthDataSourceResponse, {"data": authorization}), HTTPStatus.OK
@console_ns.route("/oauth/data-source/callback/<string:provider>")
@@ -107,24 +135,21 @@ class OAuthDataSourceCallback(Resource):
@console_ns.doc(description="Handle OAuth callback from data source provider")
@console_ns.doc(params={"provider": "Data source provider name (notion)"})
@console_ns.doc(params=query_params_from_model(OAuthDataSourceCallbackQuery))
- @console_ns.response(302, "Redirect to console with result", console_ns.models[RedirectResponse.__name__])
- @console_ns.response(400, "Invalid provider")
+ @console_ns.response(
+ HTTPStatus.FOUND, "Redirect to console with result", console_ns.models[RedirectResponse.__name__]
+ )
+ @console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid provider")
def get(self, provider: str):
- OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
- with current_app.app_context():
- oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
- if not oauth_provider:
- return {"error": "Invalid provider"}, 400
- if "code" in request.args:
- code = request.args.get("code")
-
- return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&code={code}")
- elif "error" in request.args:
- error = request.args.get("error")
-
- return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&error={error}")
- else:
- return redirect(f"{dify_config.CONSOLE_WEB_URL}?type=notion&error=Access denied")
+ query = query_params_from_request(OAuthDataSourceCallbackQuery)
+ try:
+ service = application_services().resolve_data_source_oauth(provider)
+ callback = service.complete_callback(
+ code=query.code,
+ error=query.error,
+ )
+ except InvalidDataSourceOAuthProviderError:
+ return _invalid_provider_response()
+ return redirect(_callback_redirect_location(callback))
@console_ns.route("/oauth/data-source/binding/<string:provider>")
@@ -134,30 +159,30 @@ class OAuthDataSourceBinding(Resource):
@console_ns.doc(params={"provider": "Data source provider name (notion)"})
@console_ns.doc(params=query_params_from_model(OAuthDataSourceBindingQuery))
@console_ns.response(
- 200,
+ HTTPStatus.OK,
"Data source binding success",
console_ns.models[OAuthDataSourceBindingResponse.__name__],
)
- @console_ns.response(400, "Invalid provider or code")
- def get(self, provider: str):
- OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
- with current_app.app_context():
- oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
- if not oauth_provider:
- return {"error": "Invalid provider"}, 400
- if "code" in request.args:
- code = request.args.get("code", "")
- if not code:
- return {"error": "Invalid code"}, 400
- try:
- oauth_provider.get_access_token(code)
- except httpx.HTTPStatusError as e:
- logger.exception(
- "An error occurred during the OAuthCallback process with %s: %s", provider, e.response.text
- )
- return {"error": "OAuth data source process failed"}, 400
-
- return {"result": "success"}, 200
+ @console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid provider or code")
+ @console_account_admission()
+ def get(self, request_context: RequestContext, provider: str):
+ try:
+ query = query_params_from_request(OAuthDataSourceBindingQuery)
+ except ValidationError:
+ return {"error": "Invalid code"}, HTTPStatus.BAD_REQUEST
+ if not query.code:
+ return {"error": "Invalid code"}, HTTPStatus.BAD_REQUEST
+ try:
+ service = application_services().resolve_data_source_oauth(provider)
+ service.bind(request_context, code=query.code)
+ except InvalidDataSourceOAuthProviderError:
+ return _invalid_provider_response()
+ except InvalidDataSourceOAuthCodeError:
+ return {"error": "Invalid code"}, HTTPStatus.BAD_REQUEST
+ except DataSourceOAuthError as error:
+ return _provider_failure_response(provider, error)
+
+ return dump_response(OAuthDataSourceBindingResponse, {"result": "success"}), HTTPStatus.OK
@console_ns.route("/oauth/data-source/<string:provider>/<uuid:binding_id>/sync")
@@ -166,27 +191,22 @@ class OAuthDataSourceSync(Resource):
@console_ns.doc(description="Sync data from OAuth data source")
@console_ns.doc(params={"provider": "Data source provider name (notion)", "binding_id": "Data source binding ID"})
@console_ns.response(
- 200,
+ HTTPStatus.OK,
"Data source sync success",
console_ns.models[OAuthDataSourceSyncResponse.__name__],
)
- @console_ns.response(400, "Invalid provider or sync failed")
- @setup_required
- @login_required
- @account_initialization_required
- def get(self, provider: str, binding_id: UUID):
- binding_id_str = str(binding_id)
- OAUTH_DATASOURCE_PROVIDERS = get_oauth_providers()
- with current_app.app_context():
- oauth_provider = OAUTH_DATASOURCE_PROVIDERS.get(provider)
- if not oauth_provider:
- return {"error": "Invalid provider"}, 400
+ @console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid provider or sync failed")
+ @console_account_admission()
+ def get(self, request_context: RequestContext, provider: str, binding_id: UUID):
try:
- oauth_provider.sync_data_source(binding_id_str)
- except httpx.HTTPStatusError as e:
- logger.exception(
- "An error occurred during the OAuthCallback process with %s: %s", provider, e.response.text
+ service = application_services().resolve_data_source_oauth(provider)
+ service.sync(
+ request_context,
+ binding_id=str(binding_id),
)
- return {"error": "OAuth data source process failed"}, 400
+ except InvalidDataSourceOAuthProviderError:
+ return _invalid_provider_response()
+ except DataSourceOAuthError as error:
+ return _provider_failure_response(provider, error)
- return {"result": "success"}, 200
+ return dump_response(OAuthDataSourceSyncResponse, {"result": "success"}), HTTPStatus.OK
diff --git a/api/controllers/console/auth/oauth_server.py b/api/controllers/console/auth/oauth_server.py
index fe297c6c725..15b1c867235 100644
--- a/api/controllers/console/auth/oauth_server.py
+++ b/api/controllers/console/auth/oauth_server.py
@@ -1,21 +1,23 @@
-from collections.abc import Callable
-from functools import wraps
-from typing import Any, Concatenate
+from http import HTTPStatus
+from typing import Any, Never
-from flask import jsonify, request
-from flask.typing import ResponseReturnValue
+from flask import Response, jsonify, request
from flask_restx import Resource
-from pydantic import BaseModel
-from werkzeug.exceptions import BadRequest, NotFound
+from pydantic import BaseModel, ValidationError
+from werkzeug.exceptions import BadRequest, NotFound, UnprocessableEntity
from controllers.common.schema import register_response_schema_models, register_schema_models
-from controllers.console.wraps import account_initialization_required, model_validate, setup_required, with_current_user
-from core.db.session_factory import session_factory
-from graphon.model_runtime.utils.encoders import jsonable_encoder
-from libs.login import login_required
-from models import Account
-from models.model import OAuthProviderApp
-from services.oauth_server import OAUTH_ACCESS_TOKEN_EXPIRES_IN, OAuthGrantType, OAuthServerService
+from controllers.console.flask_admission import console_account_admission
+from controllers.console.wraps import setup_required
+from extensions.ext_application_services import application_services
+from fields.base import ResponseModel
+from libs.helper import dump_response
+from machinery.context import RequestContext
+from services.oauth_server_service import (
+ OAuthServerClientNotFoundError,
+ OAuthServerRequestError,
+ OAuthServerUnauthorizedError,
+)
from .. import console_ns
@@ -38,31 +40,31 @@ class OAuthTokenRequest(BaseModel):
refresh_token: str | None = None
-class OAuthProviderAppResponse(BaseModel):
+class OAuthProviderAppResponse(ResponseModel):
app_icon: str
app_label: dict[str, Any]
scope: str
auto_authorize: bool
-class OAuthProviderAuthorizeResponse(BaseModel):
+class OAuthProviderAuthorizeResponse(ResponseModel):
code: str
-class OAuthProviderTokenResponse(BaseModel):
+class OAuthProviderTokenResponse(ResponseModel):
access_token: str
token_type: str
expires_in: int
refresh_token: str
-class OAuthProviderAccountResponse(BaseModel):
+class OAuthProviderAccountResponse(ResponseModel):
id: str
name: str
email: str
avatar: str | None = None
- interface_language: str
- timezone: str
+ interface_language: str | None
+ timezone: str | None
register_schema_models(console_ns, OAuthClientPayload, OAuthProviderRequest, OAuthTokenRequest)
@@ -75,189 +77,116 @@ register_response_schema_models(
)
-def oauth_server_client_id_required[T, **P, R](
- view: Callable[Concatenate[T, OAuthProviderApp, P], R],
-) -> Callable[Concatenate[T, P], R]:
- @wraps(view)
- def decorated(self: T, *args: P.args, **kwargs: P.kwargs) -> R:
- json_data = request.get_json()
- if json_data is None:
- raise BadRequest("client_id is required")
-
- payload = OAuthClientPayload.model_validate(json_data)
- client_id = payload.client_id
-
- oauth_provider_app = OAuthServerService.get_oauth_provider_app(client_id)
- if not oauth_provider_app:
- raise NotFound("client_id is invalid")
-
- return view(self, oauth_provider_app, *args, **kwargs)
-
- return decorated
-
-
-def oauth_server_access_token_required[T, **P, R](
- view: Callable[Concatenate[T, OAuthProviderApp, Account, P], R],
-) -> Callable[Concatenate[T, OAuthProviderApp, P], R | ResponseReturnValue]:
- @wraps(view)
- def decorated(
- self: T, oauth_provider_app: OAuthProviderApp, *args: P.args, **kwargs: P.kwargs
- ) -> R | ResponseReturnValue:
- if not isinstance(oauth_provider_app, OAuthProviderApp):
- raise BadRequest("Invalid oauth_provider_app")
-
- authorization_header = request.headers.get("Authorization")
- if not authorization_header:
- response = jsonify({"error": "Authorization header is required"})
- response.status_code = 401
- response.headers["WWW-Authenticate"] = "Bearer"
- return response
-
- parts = authorization_header.strip().split(None, 1)
- if len(parts) != 2:
- response = jsonify({"error": "Invalid Authorization header format"})
- response.status_code = 401
- response.headers["WWW-Authenticate"] = "Bearer"
- return response
-
- token_type = parts[0].strip()
- if token_type.lower() != "bearer":
- response = jsonify({"error": "token_type is invalid"})
- response.status_code = 401
- response.headers["WWW-Authenticate"] = "Bearer"
- return response
-
- access_token = parts[1].strip()
- if not access_token:
- response = jsonify({"error": "access_token is required"})
- response.status_code = 401
- response.headers["WWW-Authenticate"] = "Bearer"
- return response
-
- with session_factory.create_session() as session:
- account = OAuthServerService.validate_oauth_access_token(
- oauth_provider_app.client_id, access_token, session
- )
- if not account:
- response = jsonify({"error": "access_token or client_id is invalid"})
- response.status_code = 401
- response.headers["WWW-Authenticate"] = "Bearer"
- return response
+def _parse_payload[PayloadT: BaseModel](model: type[PayloadT]) -> PayloadT:
+ json_data = request.get_json()
+ if json_data is None:
+ raise BadRequest("client_id is required")
+ try:
+ return model.model_validate(json_data)
+ except ValidationError as exc:
+ raise UnprocessableEntity(exc.json()) from exc
+
+
+def _raise_application_error(error: OAuthServerClientNotFoundError | OAuthServerRequestError) -> Never:
+ if isinstance(error, OAuthServerClientNotFoundError):
+ raise NotFound(str(error)) from error
+ raise BadRequest(str(error)) from error
+
+
+def _parse_bearer_access_token() -> tuple[str | None, str | None]:
+ authorization_header = request.headers.get("Authorization")
+ if not authorization_header:
+ return None, "Authorization header is required"
- return view(self, oauth_provider_app, account, *args, **kwargs)
+ parts = authorization_header.strip().split(None, 1)
+ if len(parts) != 2:
+ return None, "Invalid Authorization header format"
- return decorated
+ token_type, access_token = (part.strip() for part in parts)
+ if token_type.lower() != "bearer":
+ return None, "token_type is invalid"
+ if not access_token:
+ return None, "access_token is required"
+ return access_token, None
+
+
+def _unauthorized_response(error: str) -> Response:
+ response = jsonify({"error": error})
+ response.status_code = HTTPStatus.UNAUTHORIZED
+ response.headers["WWW-Authenticate"] = "Bearer"
+ return response
@console_ns.route("/oauth/provider")
class OAuthServerAppApi(Resource):
- @setup_required
@console_ns.expect(console_ns.models[OAuthProviderRequest.__name__])
- @console_ns.response(200, "Success", console_ns.models[OAuthProviderAppResponse.__name__])
- @oauth_server_client_id_required
- @model_validate(OAuthProviderRequest)
- def post(self, payload: OAuthProviderRequest, oauth_provider_app: OAuthProviderApp):
- redirect_uri = payload.redirect_uri
-
- # check if redirect_uri is valid
- if redirect_uri not in oauth_provider_app.redirect_uris:
- raise BadRequest("redirect_uri is invalid")
-
- return jsonable_encoder(
- {
- "app_icon": oauth_provider_app.app_icon,
- "app_label": oauth_provider_app.app_label,
- "scope": oauth_provider_app.scope,
- "auto_authorize": oauth_provider_app.auto_authorize,
- }
- )
+ @console_ns.response(HTTPStatus.OK, "Success", console_ns.models[OAuthProviderAppResponse.__name__])
+ @setup_required
+ def post(self):
+ payload = _parse_payload(OAuthProviderRequest)
+ try:
+ provider = application_services().oauth_server.get_provider(
+ client_id=payload.client_id,
+ redirect_uri=payload.redirect_uri,
+ )
+ except (OAuthServerClientNotFoundError, OAuthServerRequestError) as error:
+ _raise_application_error(error)
+ return dump_response(OAuthProviderAppResponse, provider), HTTPStatus.OK
@console_ns.route("/oauth/provider/authorize")
class OAuthServerUserAuthorizeApi(Resource):
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_user
@console_ns.expect(console_ns.models[OAuthClientPayload.__name__])
- @console_ns.response(200, "Success", console_ns.models[OAuthProviderAuthorizeResponse.__name__])
- @oauth_server_client_id_required
- def post(self, oauth_provider_app: OAuthProviderApp, current_user: Account):
- user_account_id = current_user.id
- code = OAuthServerService.sign_oauth_authorization_code(oauth_provider_app.client_id, user_account_id)
- return jsonable_encoder(
- {
- "code": code,
- }
- )
+ @console_ns.response(HTTPStatus.OK, "Success", console_ns.models[OAuthProviderAuthorizeResponse.__name__])
+ @console_account_admission()
+ def post(self, request_context: RequestContext):
+ payload = _parse_payload(OAuthClientPayload)
+ try:
+ authorization = application_services().oauth_server.authorize(
+ request_context,
+ client_id=payload.client_id,
+ )
+ except OAuthServerClientNotFoundError as error:
+ _raise_application_error(error)
+ return dump_response(OAuthProviderAuthorizeResponse, authorization), HTTPStatus.OK
@console_ns.route("/oauth/provider/token")
class OAuthServerUserTokenApi(Resource):
- @setup_required
@console_ns.expect(console_ns.models[OAuthTokenRequest.__name__])
- @console_ns.response(200, "Success", console_ns.models[OAuthProviderTokenResponse.__name__])
- @oauth_server_client_id_required
- @model_validate(OAuthTokenRequest)
- def post(self, payload: OAuthTokenRequest, oauth_provider_app: OAuthProviderApp):
+ @console_ns.response(HTTPStatus.OK, "Success", console_ns.models[OAuthProviderTokenResponse.__name__])
+ @setup_required
+ def post(self):
+ payload = _parse_payload(OAuthTokenRequest)
try:
- grant_type = OAuthGrantType(payload.grant_type)
- except ValueError:
- raise BadRequest("invalid grant_type")
- match grant_type:
- case OAuthGrantType.AUTHORIZATION_CODE:
- if not payload.code:
- raise BadRequest("code is required")
-
- if payload.client_secret != oauth_provider_app.client_secret:
- raise BadRequest("client_secret is invalid")
-
- if payload.redirect_uri not in oauth_provider_app.redirect_uris:
- raise BadRequest("redirect_uri is invalid")
-
- access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
- grant_type, code=payload.code, client_id=oauth_provider_app.client_id
- )
- return jsonable_encoder(
- {
- "access_token": access_token,
- "token_type": "Bearer",
- "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- "refresh_token": refresh_token,
- }
- )
- case OAuthGrantType.REFRESH_TOKEN:
- if not payload.refresh_token:
- raise BadRequest("refresh_token is required")
-
- access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
- grant_type, refresh_token=payload.refresh_token, client_id=oauth_provider_app.client_id
- )
- return jsonable_encoder(
- {
- "access_token": access_token,
- "token_type": "Bearer",
- "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- "refresh_token": refresh_token,
- }
- )
+ tokens = application_services().oauth_server.exchange_token(
+ client_id=payload.client_id,
+ grant_type=payload.grant_type,
+ code=payload.code,
+ client_secret=payload.client_secret,
+ redirect_uri=payload.redirect_uri,
+ refresh_token=payload.refresh_token,
+ )
+ except (OAuthServerClientNotFoundError, OAuthServerRequestError) as error:
+ _raise_application_error(error)
+ return dump_response(OAuthProviderTokenResponse, tokens), HTTPStatus.OK
@console_ns.route("/oauth/provider/account")
class OAuthServerUserAccountApi(Resource):
- @setup_required
@console_ns.expect(console_ns.models[OAuthClientPayload.__name__])
- @console_ns.response(200, "Success", console_ns.models[OAuthProviderAccountResponse.__name__])
- @oauth_server_client_id_required
- @oauth_server_access_token_required
- def post(self, oauth_provider_app: OAuthProviderApp, account: Account):
- return jsonable_encoder(
- {
- "id": account.id,
- "name": account.name,
- "email": account.email,
- "avatar": account.avatar,
- "interface_language": account.interface_language,
- "timezone": account.timezone,
- }
- )
+ @console_ns.response(HTTPStatus.OK, "Success", console_ns.models[OAuthProviderAccountResponse.__name__])
+ @setup_required
+ def post(self):
+ payload = _parse_payload(OAuthClientPayload)
+ access_token, header_error = _parse_bearer_access_token()
+ try:
+ account = application_services().oauth_server.get_account(
+ client_id=payload.client_id,
+ access_token=access_token,
+ )
+ except OAuthServerClientNotFoundError as error:
+ _raise_application_error(error)
+ except OAuthServerUnauthorizedError as error:
+ return _unauthorized_response(header_error or str(error))
+ return dump_response(OAuthProviderAccountResponse, account), HTTPStatus.OK
diff --git a/api/core/helper/creators.py b/api/core/helper/creators.py
index 154ff8dca68..73f7fa536b4 100644
--- a/api/core/helper/creators.py
+++ b/api/core/helper/creators.py
@@ -31,13 +31,9 @@ def upload_dsl(dsl_file_bytes: bytes, filename: str = "template.yaml") -> str:
return claim_code
-def get_redirect_url(user_account_id: str, claim_code: str) -> str:
+def get_redirect_url(claim_code: str, *, oauth_code: str | None = None) -> str:
base_url = str(dify_config.CREATORS_PLATFORM_API_URL).rstrip("/")
params: dict[str, str] = {"dsl_claim_code": claim_code}
- client_id = str(dify_config.CREATORS_PLATFORM_OAUTH_CLIENT_ID or "")
- if client_id:
- from services.oauth_server import OAuthServerService
-
- oauth_code = OAuthServerService.sign_oauth_authorization_code(client_id, user_account_id)
+ if oauth_code:
params["oauth_code"] = oauth_code
return f"{base_url}?{urlencode(params)}"
diff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.py
index f1cbb99808a..6b10c8bcf54 100644
--- a/api/extensions/ext_application_services.py
+++ b/api/extensions/ext_application_services.py
@@ -1,6 +1,7 @@
"""Composition root for application services used by transport adapters."""
import json
+from collections.abc import Mapping
from dataclasses import dataclass
from typing import cast
@@ -12,6 +13,7 @@ from sqlalchemy.orm import Session, sessionmaker
from configs import dify_config
from constants.dsl_version import CURRENT_APP_DSL_VERSION
from core.db.session_factory import get_session_maker
+from core.helper.ssrf_proxy import ssrf_proxy
from core.schemas.schema_manager import SchemaManager
from enums import DeploymentEdition, WebAppAccessMode
from extensions.ext_redis import RedisClientWrapper, redis_client
@@ -23,8 +25,10 @@ from repositories.account_repository import SQLAlchemyAccountRepository
from repositories.app_definition_query_repository import AppDefinitionQueryRepository
from repositories.app_site_command_repository import AppSiteCommandRepository
from repositories.data_source_api_key_auth_repository import SQLAlchemyDataSourceApiKeyAuthBindingRepository
+from repositories.data_source_oauth_binding_repository import SQLAlchemyDataSourceOAuthBindingRepository
from repositories.explore_banner_query_repository import ExploreBannerQueryRepository
from repositories.installation_state_repository import InstallationStateRepository
+from repositories.oauth_server_repository import RedisOAuthServerTokenRepository, SQLAlchemyOAuthServerRepository
from repositories.recommended_app_catalog_repository import DatabaseRecommendedAppCatalogRepository
from repositories.tag_repository import TagRepository
from repositories.trial_app_query_repository import TrialAppQueryRepository
@@ -78,6 +82,7 @@ from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthS
from services.billing_portal_service import BillingPortalService
from services.billing_service import BillingService
from services.compliance_download_service import ComplianceDownloadService
+from services.data_source_oauth_service import DataSourceOAuthService, InvalidDataSourceOAuthProviderError
from services.enterprise.enterprise_service import EnterpriseService
from services.errors.enterprise import EnterpriseServiceError
from services.explore_banner_query_service import ExploreBannerQueryService
@@ -86,6 +91,8 @@ from services.feature_service import FeatureService
from services.feature_service_gateway import FeatureServiceGateway
from services.file_service import FileService
from services.init_validation_service import InitValidationService
+from services.notion_data_source_gateway import NotionDataSourceGateway
+from services.oauth_server_service import OAUTH_ACCESS_TOKEN_EXPIRES_IN, OAuthServerService
from services.partner_tenant_binding_service import PartnerTenantBindingService
from services.recommended_app_catalog_gateway import (
BuiltinRecommendedAppCatalogGateway,
@@ -151,12 +158,14 @@ class ApplicationServices:
billing_portal: BillingPortalService
compliance_downloads: ComplianceDownloadService
data_source_api_key_auth: DataSourceApiKeyAuthService
+ data_source_oauth: Mapping[str, DataSourceOAuthService]
webapp_access: WebAppAccessQueryService
web_app_runtime: WebAppRuntimeQueryService
explore_banner_queries: ExploreBannerQueryService
schema_definitions: SchemaDefinitionService
setup: SetupService
feature_queries: FeatureQueryService
+ oauth_server: OAuthServerService
init_validation: InitValidationService
partner_tenant_bindings: PartnerTenantBindingService
recommended_app_queries: RecommendedAppQueryService
@@ -165,6 +174,46 @@ class ApplicationServices:
workspace_member_queries: WorkspaceMemberQueryService
tags: TagApplicationService
+ def resolve_data_source_oauth(self, provider: str) -> DataSourceOAuthService:
+ service = self.data_source_oauth.get(provider)
+ if service is None:
+ raise InvalidDataSourceOAuthProviderError("Invalid provider")
+ return service
+
+
+def _build_data_source_oauth_services(
+ *,
+ database_client: sessionmaker[Session],
+) -> Mapping[str, DataSourceOAuthService]:
+ notion_data_source = NotionDataSourceGateway(
+ client_id=dify_config.NOTION_CLIENT_ID or "",
+ client_secret=dify_config.NOTION_CLIENT_SECRET or "",
+ redirect_uri=dify_config.CONSOLE_API_URL + "/console/api/oauth/data-source/callback/notion",
+ http_client=ssrf_proxy,
+ )
+ bindings = SQLAlchemyDataSourceOAuthBindingRepository(session_factory=database_client)
+ return {
+ "notion": DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=notion_data_source,
+ bindings=bindings,
+ is_internal_provider=dify_config.NOTION_INTEGRATION_TYPE == "internal",
+ internal_access_token=dify_config.NOTION_INTERNAL_SECRET,
+ )
+ }
+
+
+def _build_oauth_server_service(
+ *,
+ database_client: sessionmaker[Session],
+ redis: RedisClientWrapper,
+) -> OAuthServerService:
+ return OAuthServerService(
+ repository=SQLAlchemyOAuthServerRepository(session_factory=database_client),
+ tokens=RedisOAuthServerTokenRepository(redis=redis),
+ access_token_expires_in=OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ )
+
def build_application_services(
*,
@@ -302,6 +351,7 @@ def build_application_services(
validator=ProviderApiKeyAuthCredentialValidator(),
encryptor=TenantApiKeyAuthCredentialEncryptor(),
),
+ data_source_oauth=_build_data_source_oauth_services(database_client=database_client),
webapp_access=WebAppAccessQueryService(
access=WebAppAccessQueryRepository(session_factory=database_client),
webapp_auth_enabled=FeatureService.is_webapp_auth_enabled(),
@@ -329,6 +379,7 @@ def build_application_services(
features=feature_gateway,
app_dsl_version=CURRENT_APP_DSL_VERSION,
),
+ oauth_server=_build_oauth_server_service(database_client=database_client, redis=redis),
init_validation=InitValidationService(
state=installation_state,
validation_required=(deployment_edition != DeploymentEdition.CLOUD and bool(initialization_password)),
diff --git a/api/libs/oauth_data_source.py b/api/libs/oauth_data_source.py
deleted file mode 100644
index d9971d39928..00000000000
--- a/api/libs/oauth_data_source.py
+++ /dev/null
@@ -1,357 +0,0 @@
-import urllib.parse
-from typing import Any, Literal, TypedDict, override
-
-import httpx
-from flask_login import current_user
-from pydantic import TypeAdapter
-from sqlalchemy import select
-
-from core.db.session_factory import session_factory
-from core.helper.http_client_pooling import get_pooled_http_client
-from libs.datetime_utils import naive_utc_now
-from models.source import DataSourceOauthBinding
-
-
-class NotionPageSummary(TypedDict):
- page_id: str
- page_name: str
- page_icon: dict[str, str] | None
- parent_id: str
- type: Literal["page", "database"]
-
-
-class NotionSourceInfo(TypedDict):
- workspace_name: str | None
- workspace_icon: str | None
- workspace_id: str | None
- pages: list[NotionPageSummary]
- total: int
-
-
-SOURCE_INFO_STORAGE_ADAPTER = TypeAdapter(dict[str, object])
-NOTION_SOURCE_INFO_ADAPTER = TypeAdapter(NotionSourceInfo)
-NOTION_PAGE_SUMMARY_ADAPTER = TypeAdapter(NotionPageSummary)
-
-
-# Reuse a small pooled client for OAuth data source flows.
-_http_client: httpx.Client = get_pooled_http_client(
- "oauth:notion",
- lambda: httpx.Client(limits=httpx.Limits(max_keepalive_connections=50, max_connections=100)),
-)
-
-
-class OAuthDataSource:
- client_id: str
- client_secret: str
- redirect_uri: str
-
- def __init__(self, client_id: str, client_secret: str, redirect_uri: str):
- self.client_id = client_id
- self.client_secret = client_secret
- self.redirect_uri = redirect_uri
-
- def get_authorization_url(self) -> str:
- raise NotImplementedError()
-
- def get_access_token(self, code: str) -> None:
- raise NotImplementedError()
-
-
-class NotionOAuth(OAuthDataSource):
- _AUTH_URL = "https://api.notion.com/v1/oauth/authorize"
- _TOKEN_URL = "https://api.notion.com/v1/oauth/token"
- _NOTION_PAGE_SEARCH = "https://api.notion.com/v1/search"
- _NOTION_BLOCK_SEARCH = "https://api.notion.com/v1/blocks"
- _NOTION_BOT_USER = "https://api.notion.com/v1/users/me"
-
- @override
- def get_authorization_url(self) -> str:
- params = {
- "client_id": self.client_id,
- "response_type": "code",
- "redirect_uri": self.redirect_uri,
- "owner": "user",
- }
- return f"{self._AUTH_URL}?{urllib.parse.urlencode(params)}"
-
- @override
- def get_access_token(self, code: str) -> None:
- data = {"code": code, "grant_type": "authorization_code", "redirect_uri": self.redirect_uri}
- headers = {"Accept": "application/json"}
- auth = (self.client_id, self.client_secret)
- response = _http_client.post(self._TOKEN_URL, data=data, auth=auth, headers=headers)
-
- response_json = response.json()
- access_token = response_json.get("access_token")
- if not access_token:
- raise ValueError(f"Error in Notion OAuth: {response_json}")
- workspace_name = response_json.get("workspace_name")
- workspace_icon = response_json.get("workspace_icon")
- workspace_id = response_json.get("workspace_id")
- # get all authorized pages
- pages = self.get_authorized_pages(access_token)
- source_info = self._build_source_info(
- workspace_name=workspace_name,
- workspace_icon=workspace_icon,
- workspace_id=workspace_id,
- pages=pages,
- )
- # save data source binding
- with session_factory.create_session() as session:
- data_source_binding = session.scalar(
- select(DataSourceOauthBinding).where(
- DataSourceOauthBinding.tenant_id == current_user.current_tenant_id,
- DataSourceOauthBinding.provider == "notion",
- DataSourceOauthBinding.access_token == access_token,
- )
- )
- if data_source_binding:
- data_source_binding.source_info = SOURCE_INFO_STORAGE_ADAPTER.validate_python(source_info)
- data_source_binding.disabled = False
- data_source_binding.updated_at = naive_utc_now()
- session.commit()
- else:
- new_data_source_binding = DataSourceOauthBinding(
- tenant_id=current_user.current_tenant_id,
- access_token=access_token,
- source_info=SOURCE_INFO_STORAGE_ADAPTER.validate_python(source_info),
- provider="notion",
- )
- session.add(new_data_source_binding)
- session.commit()
-
- def save_internal_access_token(self, access_token: str) -> None:
- workspace_name = self.notion_workspace_name(access_token)
- workspace_icon = None
- workspace_id = current_user.current_tenant_id
- # get all authorized pages
- pages = self.get_authorized_pages(access_token)
- source_info = self._build_source_info(
- workspace_name=workspace_name,
- workspace_icon=workspace_icon,
- workspace_id=workspace_id,
- pages=pages,
- )
- # save data source binding
- with session_factory.create_session() as session:
- data_source_binding = session.scalar(
- select(DataSourceOauthBinding).where(
- DataSourceOauthBinding.tenant_id == current_user.current_tenant_id,
- DataSourceOauthBinding.provider == "notion",
- DataSourceOauthBinding.access_token == access_token,
- )
- )
- if data_source_binding:
- data_source_binding.source_info = SOURCE_INFO_STORAGE_ADAPTER.validate_python(source_info)
- data_source_binding.disabled = False
- data_source_binding.updated_at = naive_utc_now()
- session.commit()
- else:
- new_data_source_binding = DataSourceOauthBinding(
- tenant_id=current_user.current_tenant_id,
- access_token=access_token,
- source_info=SOURCE_INFO_STORAGE_ADAPTER.validate_python(source_info),
- provider="notion",
- )
- session.add(new_data_source_binding)
- session.commit()
-
- def sync_data_source(self, binding_id: str) -> None:
- # save data source binding
- with session_factory.create_session() as session:
- data_source_binding = session.scalar(
- select(DataSourceOauthBinding).where(
- DataSourceOauthBinding.tenant_id == current_user.current_tenant_id,
- DataSourceOauthBinding.provider == "notion",
- DataSourceOauthBinding.id == binding_id,
- DataSourceOauthBinding.disabled == False,
- )
- )
-
- if data_source_binding:
- # get all authorized pages
- pages = self.get_authorized_pages(data_source_binding.access_token)
- source_info = NOTION_SOURCE_INFO_ADAPTER.validate_python(data_source_binding.source_info)
- new_source_info = self._build_source_info(
- workspace_name=source_info["workspace_name"],
- workspace_icon=source_info["workspace_icon"],
- workspace_id=source_info["workspace_id"],
- pages=pages,
- )
- data_source_binding.source_info = SOURCE_INFO_STORAGE_ADAPTER.validate_python(new_source_info)
- data_source_binding.disabled = False
- data_source_binding.updated_at = naive_utc_now()
- session.commit()
- else:
- raise ValueError("Data source binding not found")
-
- def get_authorized_pages(self, access_token: str) -> list[NotionPageSummary]:
- pages: list[NotionPageSummary] = []
- page_results = self.notion_page_search(access_token)
- database_results = self.notion_database_search(access_token)
- # get page detail
- for page_result in page_results:
- page_id = page_result["id"]
- page_name = "Untitled"
- for key in page_result["properties"]:
- if "title" in page_result["properties"][key] and page_result["properties"][key]["title"]:
- title_list = page_result["properties"][key]["title"]
- if len(title_list) > 0 and "plain_text" in title_list[0]:
- page_name = title_list[0]["plain_text"]
- page_icon = page_result["icon"]
- if page_icon:
- icon_type = page_icon["type"]
- if icon_type in {"external", "file"}:
- url = page_icon[icon_type]["url"]
- icon = {"type": "url", "url": url if url.startswith("http") else f"https://www.notion.so{url}"}
- else:
- icon = {"type": "emoji", "emoji": page_icon[icon_type]}
- else:
- icon = None
- parent = page_result["parent"]
- parent_type = parent["type"]
- if parent_type == "block_id":
- parent_id = self.notion_block_parent_page_id(access_token, parent[parent_type])
- elif parent_type == "workspace":
- parent_id = "root"
- else:
- parent_id = parent[parent_type]
- page = {
- "page_id": page_id,
- "page_name": page_name,
- "page_icon": icon,
- "parent_id": parent_id,
- "type": "page",
- }
- pages.append(NOTION_PAGE_SUMMARY_ADAPTER.validate_python(page))
- # get database detail
- for database_result in database_results:
- page_id = database_result["id"]
- if len(database_result["title"]) > 0:
- page_name = database_result["title"][0]["plain_text"]
- else:
- page_name = "Untitled"
- page_icon = database_result["icon"]
- if page_icon:
- icon_type = page_icon["type"]
- if icon_type in {"external", "file"}:
- url = page_icon[icon_type]["url"]
- icon = {"type": "url", "url": url if url.startswith("http") else f"https://www.notion.so{url}"}
- else:
- icon = {"type": icon_type, icon_type: page_icon[icon_type]}
- else:
- icon = None
- parent = database_result["parent"]
- parent_type = parent["type"]
- if parent_type == "block_id":
- parent_id = self.notion_block_parent_page_id(access_token, parent[parent_type])
- elif parent_type == "workspace":
- parent_id = "root"
- else:
- parent_id = parent[parent_type]
- page = {
- "page_id": page_id,
- "page_name": page_name,
- "page_icon": icon,
- "parent_id": parent_id,
- "type": "database",
- }
- pages.append(NOTION_PAGE_SUMMARY_ADAPTER.validate_python(page))
- return pages
-
- def notion_page_search(self, access_token: str) -> list[dict[str, Any]]:
- results: list[dict[str, Any]] = []
- next_cursor = None
- has_more = True
-
- while has_more:
- data: dict[str, Any] = {
- "filter": {"value": "page", "property": "object"},
- **({"start_cursor": next_cursor} if next_cursor else {}),
- }
- headers = {
- "Content-Type": "application/json",
- "Authorization": f"Bearer {access_token}",
- "Notion-Version": "2022-06-28",
- }
-
- response = _http_client.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers)
- response_json = response.json()
-
- results.extend(response_json.get("results", []))
-
- has_more = response_json.get("has_more", False)
- next_cursor = response_json.get("next_cursor", None)
-
- return results
-
- def notion_block_parent_page_id(self, access_token: str, block_id: str) -> str:
- headers = {
- "Authorization": f"Bearer {access_token}",
- "Notion-Version": "2022-06-28",
- }
- response = _http_client.get(url=f"{self._NOTION_BLOCK_SEARCH}/{block_id}", headers=headers)
- response_json = response.json()
- if response.status_code != 200:
- message = response_json.get("message", "unknown error")
- raise ValueError(f"Error fetching block parent page ID: {message}")
- parent = response_json["parent"]
- parent_type = parent["type"]
- if parent_type == "block_id":
- return self.notion_block_parent_page_id(access_token, parent[parent_type])
- return parent[parent_type]
-
- def notion_workspace_name(self, access_token: str) -> str:
- headers = {
- "Authorization": f"Bearer {access_token}",
- "Notion-Version": "2022-06-28",
- }
- response = _http_client.get(url=self._NOTION_BOT_USER, headers=headers)
- response_json = response.json()
- if "object" in response_json and response_json["object"] == "user":
- user_type = response_json["type"]
- user_info = response_json[user_type]
- if "workspace_name" in user_info:
- return user_info["workspace_name"]
- return "workspace"
-
- def notion_database_search(self, access_token: str) -> list[dict[str, Any]]:
- results: list[dict[str, Any]] = []
- next_cursor = None
- has_more = True
-
- while has_more:
- data: dict[str, Any] = {
- "filter": {"value": "database", "property": "object"},
- **({"start_cursor": next_cursor} if next_cursor else {}),
- }
- headers = {
- "Content-Type": "application/json",
- "Authorization": f"Bearer {access_token}",
- "Notion-Version": "2022-06-28",
- }
- response = _http_client.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers)
- response_json = response.json()
-
- results.extend(response_json.get("results", []))
-
- has_more = response_json.get("has_more", False)
- next_cursor = response_json.get("next_cursor", None)
-
- return results
-
- @staticmethod
- def _build_source_info(
- *,
- workspace_name: str | None,
- workspace_icon: str | None,
- workspace_id: str | None,
- pages: list[NotionPageSummary],
- ) -> NotionSourceInfo:
- return {
- "workspace_name": workspace_name,
- "workspace_icon": workspace_icon,
- "workspace_id": workspace_id,
- "pages": pages,
- "total": len(pages),
- }
diff --git a/api/repositories/data_source_oauth_binding_repository.py b/api/repositories/data_source_oauth_binding_repository.py
new file mode 100644
index 00000000000..5c52140f13a
--- /dev/null
+++ b/api/repositories/data_source_oauth_binding_repository.py
@@ -0,0 +1,107 @@
+"""SQLAlchemy repository for OAuth data-source bindings."""
+
+from collections.abc import Mapping
+from typing import override
+
+from pydantic import TypeAdapter
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from libs.datetime_utils import naive_utc_now
+from models.source import DataSourceOauthBinding
+from services.data_source_oauth_service import DataSourceOAuthBindingRepository
+from services.entities.data_source_oauth_entities import (
+ DataSourceOAuthAuthorization,
+ DataSourceOAuthBindingRecord,
+)
+
+_SOURCE_INFO_ADAPTER = TypeAdapter(dict[str, object])
+
+
+class SQLAlchemyDataSourceOAuthBindingRepository(DataSourceOAuthBindingRepository):
+ def __init__(self, session_factory: sessionmaker[Session]) -> None:
+ self._session_factory = session_factory
+
+ @override
+ def upsert_authorization(
+ self,
+ *,
+ workspace_id: str,
+ provider: str,
+ authorization: DataSourceOAuthAuthorization,
+ ) -> None:
+ source_info = _SOURCE_INFO_ADAPTER.validate_python(authorization.source_info)
+ with self._session_factory.begin() as session:
+ binding = session.scalar(
+ select(DataSourceOauthBinding).where(
+ DataSourceOauthBinding.tenant_id == workspace_id,
+ DataSourceOauthBinding.provider == provider,
+ DataSourceOauthBinding.access_token == authorization.access_token,
+ )
+ )
+ if binding is None:
+ session.add(
+ DataSourceOauthBinding(
+ tenant_id=workspace_id,
+ provider=provider,
+ access_token=authorization.access_token,
+ source_info=source_info,
+ )
+ )
+ return
+
+ binding.source_info = source_info
+ binding.disabled = False
+ binding.updated_at = naive_utc_now()
+
+ @override
+ def get_enabled(
+ self,
+ *,
+ workspace_id: str,
+ provider: str,
+ binding_id: str,
+ ) -> DataSourceOAuthBindingRecord | None:
+ with self._session_factory() as session:
+ binding = session.scalar(
+ select(DataSourceOauthBinding).where(
+ DataSourceOauthBinding.tenant_id == workspace_id,
+ DataSourceOauthBinding.provider == provider,
+ DataSourceOauthBinding.id == binding_id,
+ DataSourceOauthBinding.disabled.is_(False),
+ )
+ )
+ if binding is None:
+ return None
+
+ return DataSourceOAuthBindingRecord(
+ id=binding.id,
+ access_token=binding.access_token,
+ source_info=_SOURCE_INFO_ADAPTER.validate_python(binding.source_info),
+ )
+
+ @override
+ def update_source_info(
+ self,
+ *,
+ workspace_id: str,
+ provider: str,
+ binding_id: str,
+ source_info: Mapping[str, object],
+ ) -> bool:
+ validated_source_info = _SOURCE_INFO_ADAPTER.validate_python(source_info)
+ with self._session_factory.begin() as session:
+ binding = session.scalar(
+ select(DataSourceOauthBinding).where(
+ DataSourceOauthBinding.tenant_id == workspace_id,
+ DataSourceOauthBinding.provider == provider,
+ DataSourceOauthBinding.id == binding_id,
+ DataSourceOauthBinding.disabled.is_(False),
+ )
+ )
+ if binding is None:
+ return False
+
+ binding.source_info = validated_source_info
+ binding.updated_at = naive_utc_now()
+ return True
diff --git a/api/repositories/oauth_server_repository.py b/api/repositories/oauth_server_repository.py
new file mode 100644
index 00000000000..9f9e7fa8570
--- /dev/null
+++ b/api/repositories/oauth_server_repository.py
@@ -0,0 +1,146 @@
+"""Persistence adapters for OAuth authorization-server state."""
+
+import uuid
+from typing import override
+
+from pydantic import TypeAdapter
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from extensions.ext_redis import RedisClientWrapper
+from models import Account
+from models.model import OAuthProviderApp
+from services.entities.oauth_server_entities import (
+ OAuthProviderAccountRecord,
+ OAuthProviderAccountStatus,
+ OAuthProviderAppRecord,
+)
+from services.oauth_server_service import (
+ OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ OAUTH_AUTHORIZATION_CODE_EXPIRES_IN,
+ OAUTH_REFRESH_TOKEN_EXPIRES_IN,
+ OAuthServerRepository,
+ OAuthServerRequestError,
+ OAuthServerTokenRepository,
+)
+
+_APP_LABEL_ADAPTER = TypeAdapter(dict[str, object])
+_REDIRECT_URIS_ADAPTER = TypeAdapter(list[str])
+
+_AUTHORIZATION_CODE_KEY = "oauth_provider:{client_id}:authorization_code:{code}"
+_ACCESS_TOKEN_KEY = "oauth_provider:{client_id}:access_token:{token}"
+_REFRESH_TOKEN_KEY = "oauth_provider:{client_id}:refresh_token:{token}"
+
+
+class SQLAlchemyOAuthServerRepository(OAuthServerRepository):
+ def __init__(self, session_factory: sessionmaker[Session]) -> None:
+ self._session_factory = session_factory
+
+ @override
+ def get_provider_app_by_client_id(self, client_id: str) -> OAuthProviderAppRecord | None:
+ stmt = select(
+ OAuthProviderApp.app_icon,
+ OAuthProviderApp.client_id,
+ OAuthProviderApp.client_secret,
+ OAuthProviderApp.app_label,
+ OAuthProviderApp.redirect_uris,
+ OAuthProviderApp.scope,
+ OAuthProviderApp.auto_authorize,
+ ).where(OAuthProviderApp.client_id == client_id)
+
+ with self._session_factory() as session:
+ row = session.execute(stmt).one_or_none()
+ if row is None:
+ return None
+ return OAuthProviderAppRecord(
+ app_icon=row.app_icon,
+ client_id=row.client_id,
+ client_secret=row.client_secret,
+ app_label=_APP_LABEL_ADAPTER.validate_python(row.app_label),
+ redirect_uris=tuple(_REDIRECT_URIS_ADAPTER.validate_python(row.redirect_uris)),
+ scope=row.scope,
+ auto_authorize=row.auto_authorize,
+ )
+
+ @override
+ def get_account_by_id(self, account_id: str) -> OAuthProviderAccountRecord | None:
+ stmt = select(
+ Account.id,
+ Account.name,
+ Account.email,
+ Account.avatar,
+ Account.interface_language,
+ Account.timezone,
+ Account.status,
+ ).where(Account.id == account_id)
+
+ with self._session_factory() as session:
+ row = session.execute(stmt).one_or_none()
+ if row is None:
+ return None
+ return OAuthProviderAccountRecord(
+ id=row.id,
+ name=row.name,
+ email=row.email,
+ avatar=row.avatar,
+ interface_language=row.interface_language,
+ timezone=row.timezone,
+ status=OAuthProviderAccountStatus(row.status.value),
+ )
+
+
+class RedisOAuthServerTokenRepository(OAuthServerTokenRepository):
+ def __init__(self, redis: RedisClientWrapper) -> None:
+ self._redis = redis
+
+ @override
+ def issue_authorization_code(self, client_id: str, account_id: str) -> str:
+ code = str(uuid.uuid4())
+ key = _AUTHORIZATION_CODE_KEY.format(client_id=client_id, code=code)
+ self._redis.set(key, account_id, ex=OAUTH_AUTHORIZATION_CODE_EXPIRES_IN)
+ return code
+
+ @override
+ def exchange_authorization_code(self, client_id: str, code: str) -> tuple[str, str]:
+ key = _AUTHORIZATION_CODE_KEY.format(client_id=client_id, code=code)
+ account_id = self._redis.getdel(key)
+ if not account_id:
+ raise OAuthServerRequestError("invalid code")
+
+ normalized_account_id = self._decode(account_id)
+ return (
+ self._issue_access_token(client_id, normalized_account_id),
+ self._issue_refresh_token(client_id, normalized_account_id),
+ )
+
+ @override
+ def refresh_access_token(self, client_id: str, refresh_token: str) -> tuple[str, str]:
+ key = _REFRESH_TOKEN_KEY.format(client_id=client_id, token=refresh_token)
+ account_id = self._redis.get(key)
+ if not account_id:
+ raise OAuthServerRequestError("invalid refresh token")
+
+ access_token = self._issue_access_token(client_id, self._decode(account_id))
+ return access_token, refresh_token
+
+ @override
+ def resolve_account_id(self, client_id: str, access_token: str) -> str | None:
+ key = _ACCESS_TOKEN_KEY.format(client_id=client_id, token=access_token)
+ account_id = self._redis.get(key)
+ return self._decode(account_id) if account_id else None
+
+ def _issue_access_token(self, client_id: str, account_id: str) -> str:
+ token = str(uuid.uuid4())
+ key = _ACCESS_TOKEN_KEY.format(client_id=client_id, token=token)
+ self._redis.set(key, account_id, ex=OAUTH_ACCESS_TOKEN_EXPIRES_IN)
+ return token
+
+ def _issue_refresh_token(self, client_id: str, account_id: str) -> str:
+ token = str(uuid.uuid4())
+ key = _REFRESH_TOKEN_KEY.format(client_id=client_id, token=token)
+ self._redis.set(key, account_id, ex=OAUTH_REFRESH_TOKEN_EXPIRES_IN)
+ return token
+
+ @staticmethod
+ def _decode(value: str | bytes) -> str:
+ return value.decode("utf-8") if isinstance(value, bytes) else value
diff --git a/api/services/data_source_oauth_service.py b/api/services/data_source_oauth_service.py
new file mode 100644
index 00000000000..bc1261d3efe
--- /dev/null
+++ b/api/services/data_source_oauth_service.py
@@ -0,0 +1,148 @@
+"""Application service for Console OAuth data-source use cases."""
+
+from collections.abc import Mapping
+from typing import Protocol
+
+from machinery.context import RequestContext
+from services.entities.data_source_oauth_entities import (
+ DataSourceOAuthAuthorization,
+ DataSourceOAuthBindingRecord,
+ DataSourceOAuthCallback,
+)
+
+
+class DataSourceOAuthError(Exception):
+ """Base error for an OAuth data-source use case."""
+
+
+class InvalidDataSourceOAuthProviderError(DataSourceOAuthError):
+ """Raised when the requested OAuth provider is not configured."""
+
+
+class InvalidDataSourceOAuthCodeError(DataSourceOAuthError):
+ """Raised when an authorization code is missing or invalid."""
+
+
+class DataSourceOAuthConfigurationError(DataSourceOAuthError):
+ """Raised when the configured OAuth flow cannot be started."""
+
+
+class DataSourceProviderGateway(Protocol):
+ def get_authorization_url(self) -> str: ...
+
+ def authorize(self, code: str) -> DataSourceOAuthAuthorization: ...
+
+ def authorize_internal(self, access_token: str, workspace_id: str) -> DataSourceOAuthAuthorization: ...
+
+ def refresh(
+ self,
+ access_token: str,
+ source_info: Mapping[str, object],
+ ) -> Mapping[str, object]: ...
+
+
+class DataSourceOAuthBindingRepository(Protocol):
+ def upsert_authorization(
+ self,
+ *,
+ workspace_id: str,
+ provider: str,
+ authorization: DataSourceOAuthAuthorization,
+ ) -> None: ...
+
+ def get_enabled(
+ self,
+ *,
+ workspace_id: str,
+ provider: str,
+ binding_id: str,
+ ) -> DataSourceOAuthBindingRecord | None: ...
+
+ def update_source_info(
+ self,
+ *,
+ workspace_id: str,
+ provider: str,
+ binding_id: str,
+ source_info: Mapping[str, object],
+ ) -> bool: ...
+
+
+class DataSourceOAuthService:
+ def __init__(
+ self,
+ *,
+ provider_name: str,
+ provider_gateway: DataSourceProviderGateway,
+ bindings: DataSourceOAuthBindingRepository,
+ is_internal_provider: bool = False,
+ internal_access_token: str | None = None,
+ ) -> None:
+ self._provider_name = provider_name
+ self._provider_gateway = provider_gateway
+ self._bindings = bindings
+ self._is_internal_provider = is_internal_provider
+ self._internal_access_token = internal_access_token
+
+ def start_authorization(self, context: RequestContext) -> str:
+ if not self._is_internal_provider:
+ return self._provider_gateway.get_authorization_url()
+
+ access_token = self._internal_access_token
+ if not access_token:
+ raise DataSourceOAuthConfigurationError("Internal secret is not set")
+
+ workspace_id = self._require_active_workspace(context)
+ authorization = self._provider_gateway.authorize_internal(access_token, workspace_id)
+ self._bindings.upsert_authorization(
+ workspace_id=workspace_id,
+ provider=self._provider_name,
+ authorization=authorization,
+ )
+ return "internal"
+
+ def complete_callback(self, *, code: str | None, error: str | None) -> DataSourceOAuthCallback:
+ return DataSourceOAuthCallback(
+ provider=self._provider_name,
+ code=code,
+ error=None if code is not None else error or "Access denied",
+ )
+
+ def bind(self, context: RequestContext, *, code: str) -> None:
+ if not code:
+ raise InvalidDataSourceOAuthCodeError("Invalid code")
+
+ workspace_id = self._require_active_workspace(context)
+ authorization = self._provider_gateway.authorize(code)
+ self._bindings.upsert_authorization(
+ workspace_id=workspace_id,
+ provider=self._provider_name,
+ authorization=authorization,
+ )
+
+ def sync(self, context: RequestContext, *, binding_id: str) -> None:
+ workspace_id = self._require_active_workspace(context)
+ binding = self._bindings.get_enabled(
+ workspace_id=workspace_id,
+ provider=self._provider_name,
+ binding_id=binding_id,
+ )
+ if binding is None:
+ raise DataSourceOAuthError("Data source binding not found")
+
+ source_info = self._provider_gateway.refresh(binding.access_token, binding.source_info)
+ updated = self._bindings.update_source_info(
+ workspace_id=workspace_id,
+ provider=self._provider_name,
+ binding_id=binding.id,
+ source_info=source_info,
+ )
+ if not updated:
+ raise DataSourceOAuthError("Data source binding not found")
+
+ @staticmethod
+ def _require_active_workspace(context: RequestContext) -> str:
+ workspace_id = context.active_workspace_id
+ if workspace_id is None:
+ raise RuntimeError("Console account admission did not resolve an active workspace")
+ return workspace_id
diff --git a/api/services/entities/data_source_oauth_entities.py b/api/services/entities/data_source_oauth_entities.py
new file mode 100644
index 00000000000..844cec22847
--- /dev/null
+++ b/api/services/entities/data_source_oauth_entities.py
@@ -0,0 +1,24 @@
+"""Framework-neutral data contracts for OAuth data-source integrations."""
+
+from collections.abc import Mapping
+from dataclasses import dataclass
+
+
+@dataclass(frozen=True, slots=True)
+class DataSourceOAuthAuthorization:
+ access_token: str
+ source_info: Mapping[str, object]
+
+
+@dataclass(frozen=True, slots=True)
+class DataSourceOAuthCallback:
+ provider: str
+ code: str | None
+ error: str | None
+
+
+@dataclass(frozen=True, slots=True)
+class DataSourceOAuthBindingRecord:
+ id: str
+ access_token: str
+ source_info: Mapping[str, object]
diff --git a/api/services/entities/oauth_server_entities.py b/api/services/entities/oauth_server_entities.py
new file mode 100644
index 00000000000..25194cf1795
--- /dev/null
+++ b/api/services/entities/oauth_server_entities.py
@@ -0,0 +1,71 @@
+"""Framework-neutral contracts for the Console OAuth authorization server."""
+
+from collections.abc import Mapping
+from dataclasses import dataclass
+from enum import StrEnum
+
+
+class OAuthGrantType(StrEnum):
+ AUTHORIZATION_CODE = "authorization_code"
+ REFRESH_TOKEN = "refresh_token"
+
+
+class OAuthProviderAccountStatus(StrEnum):
+ PENDING = "pending"
+ UNINITIALIZED = "uninitialized"
+ ACTIVE = "active"
+ BANNED = "banned"
+ CLOSED = "closed"
+
+
+@dataclass(frozen=True, slots=True)
+class OAuthProviderAppRecord:
+ app_icon: str
+ client_id: str
+ client_secret: str
+ app_label: Mapping[str, object]
+ redirect_uris: tuple[str, ...]
+ scope: str
+ auto_authorize: bool
+
+
+@dataclass(frozen=True, slots=True)
+class OAuthProviderAppPresentation:
+ app_icon: str
+ app_label: Mapping[str, object]
+ scope: str
+ auto_authorize: bool
+
+
+@dataclass(frozen=True, slots=True)
+class OAuthAuthorizationCode:
+ code: str
+
+
+@dataclass(frozen=True, slots=True)
+class OAuthTokenSet:
+ access_token: str
+ token_type: str
+ expires_in: int
+ refresh_token: str
+
+
+@dataclass(frozen=True, slots=True)
+class OAuthProviderAccount:
+ id: str
+ name: str
+ email: str
+ avatar: str | None
+ interface_language: str | None
+ timezone: str | None
+
+
+@dataclass(frozen=True, slots=True)
+class OAuthProviderAccountRecord:
+ id: str
+ name: str
+ email: str
+ avatar: str | None
+ interface_language: str | None
+ timezone: str | None
+ status: OAuthProviderAccountStatus
diff --git a/api/services/notion_data_source_gateway.py b/api/services/notion_data_source_gateway.py
new file mode 100644
index 00000000000..5cadbc82fb9
--- /dev/null
+++ b/api/services/notion_data_source_gateway.py
@@ -0,0 +1,302 @@
+"""Notion adapter for the OAuth data-source application port."""
+
+import urllib.parse
+from collections.abc import Mapping
+from typing import Any, Literal, TypedDict, override
+
+import httpx
+from pydantic import TypeAdapter, ValidationError
+
+from core.helper.ssrf_proxy import MaxRetriesExceededError, SSRFProxy
+from core.tools.errors import ToolSSRFError
+from services.data_source_oauth_service import DataSourceOAuthError, DataSourceProviderGateway
+from services.entities.data_source_oauth_entities import DataSourceOAuthAuthorization
+
+
+class _NotionPageSummary(TypedDict):
+ page_id: str
+ page_name: str
+ page_icon: dict[str, str] | None
+ parent_id: str
+ type: Literal["page", "database"]
+
+
+class _NotionSourceInfo(TypedDict):
+ workspace_name: str | None
+ workspace_icon: str | None
+ workspace_id: str | None
+ pages: list[_NotionPageSummary]
+ total: int
+
+
+_NOTION_SOURCE_INFO_ADAPTER = TypeAdapter(_NotionSourceInfo)
+_NOTION_PAGE_SUMMARY_ADAPTER = TypeAdapter(_NotionPageSummary)
+
+
+class NotionDataSourceGateway(DataSourceProviderGateway):
+ _AUTH_URL = "https://api.notion.com/v1/oauth/authorize"
+ _TOKEN_URL = "https://api.notion.com/v1/oauth/token"
+ _NOTION_PAGE_SEARCH = "https://api.notion.com/v1/search"
+ _NOTION_BLOCK_SEARCH = "https://api.notion.com/v1/blocks"
+ _NOTION_BOT_USER = "https://api.notion.com/v1/users/me"
+
+ def __init__(
+ self,
+ *,
+ client_id: str,
+ client_secret: str,
+ redirect_uri: str,
+ http_client: httpx.Client | SSRFProxy,
+ ) -> None:
+ self._client_id = client_id
+ self._client_secret = client_secret
+ self._redirect_uri = redirect_uri
+ self._http_client = http_client
+
+ @override
+ def get_authorization_url(self) -> str:
+ params = {
+ "client_id": self._client_id,
+ "response_type": "code",
+ "redirect_uri": self._redirect_uri,
+ "owner": "user",
+ }
+ return f"{self._AUTH_URL}?{urllib.parse.urlencode(params)}"
+
+ @override
+ def authorize(self, code: str) -> DataSourceOAuthAuthorization:
+ response_json = self._post_json(
+ self._TOKEN_URL,
+ data={"code": code, "grant_type": "authorization_code", "redirect_uri": self._redirect_uri},
+ auth=(self._client_id, self._client_secret),
+ headers={"Accept": "application/json"},
+ )
+ access_token = response_json.get("access_token")
+ if not isinstance(access_token, str) or not access_token:
+ raise DataSourceOAuthError("Notion OAuth response did not include an access token")
+
+ try:
+ source_info = self._build_source_info(
+ workspace_name=self._optional_string(response_json.get("workspace_name")),
+ workspace_icon=self._optional_string(response_json.get("workspace_icon")),
+ workspace_id=self._optional_string(response_json.get("workspace_id")),
+ pages=self.get_authorized_pages(access_token),
+ )
+ except (KeyError, TypeError, ValidationError) as exc:
+ raise DataSourceOAuthError("Notion OAuth response was invalid") from exc
+ return DataSourceOAuthAuthorization(access_token=access_token, source_info=source_info)
+
+ @override
+ def authorize_internal(self, access_token: str, workspace_id: str) -> DataSourceOAuthAuthorization:
+ try:
+ source_info = self._build_source_info(
+ workspace_name=self.notion_workspace_name(access_token),
+ workspace_icon=None,
+ workspace_id=workspace_id,
+ pages=self.get_authorized_pages(access_token),
+ )
+ except (KeyError, TypeError, ValidationError) as exc:
+ raise DataSourceOAuthError("Notion integration response was invalid") from exc
+ return DataSourceOAuthAuthorization(access_token=access_token, source_info=source_info)
+
+ @override
+ def refresh(self, access_token: str, source_info: Mapping[str, object]) -> Mapping[str, object]:
+ try:
+ return self._build_source_info(
+ workspace_name=self._optional_string(source_info.get("workspace_name")),
+ workspace_icon=self._optional_string(source_info.get("workspace_icon")),
+ workspace_id=self._optional_string(source_info.get("workspace_id")),
+ pages=self.get_authorized_pages(access_token),
+ )
+ except (KeyError, TypeError, ValidationError) as exc:
+ raise DataSourceOAuthError("Notion integration response was invalid") from exc
+
+ def get_authorized_pages(self, access_token: str) -> list[_NotionPageSummary]:
+ pages: list[_NotionPageSummary] = []
+ page_results = self.notion_page_search(access_token)
+ database_results = self.notion_database_search(access_token)
+ for page_result in page_results:
+ page_id = page_result["id"]
+ page_name = "Untitled"
+ for prop in page_result["properties"].values():
+ if title_list := prop.get("title"):
+ if title_list and "plain_text" in title_list[0]:
+ page_name = title_list[0]["plain_text"]
+ page_icon = page_result["icon"]
+ if page_icon:
+ icon_type = page_icon["type"]
+ if icon_type in {"external", "file"}:
+ url = page_icon[icon_type]["url"]
+ icon = {"type": "url", "url": url if url.startswith("http") else f"https://www.notion.so{url}"}
+ else:
+ icon = {"type": "emoji", "emoji": page_icon[icon_type]}
+ else:
+ icon = None
+ parent = page_result["parent"]
+ parent_type = parent["type"]
+ if parent_type == "block_id":
+ parent_id = self.notion_block_parent_page_id(access_token, parent[parent_type])
+ elif parent_type == "workspace":
+ parent_id = "root"
+ else:
+ parent_id = parent[parent_type]
+ pages.append(
+ _NOTION_PAGE_SUMMARY_ADAPTER.validate_python(
+ {
+ "page_id": page_id,
+ "page_name": page_name,
+ "page_icon": icon,
+ "parent_id": parent_id,
+ "type": "page",
+ }
+ )
+ )
+
+ for database_result in database_results:
+ page_id = database_result["id"]
+ page_name = database_result["title"][0]["plain_text"] if database_result["title"] else "Untitled"
+ page_icon = database_result["icon"]
+ if page_icon:
+ icon_type = page_icon["type"]
+ if icon_type in {"external", "file"}:
+ url = page_icon[icon_type]["url"]
+ icon = {"type": "url", "url": url if url.startswith("http") else f"https://www.notion.so{url}"}
+ else:
+ icon = {"type": icon_type, icon_type: page_icon[icon_type]}
+ else:
+ icon = None
+ parent = database_result["parent"]
+ parent_type = parent["type"]
+ if parent_type == "block_id":
+ parent_id = self.notion_block_parent_page_id(access_token, parent[parent_type])
+ elif parent_type == "workspace":
+ parent_id = "root"
+ else:
+ parent_id = parent[parent_type]
+ pages.append(
+ _NOTION_PAGE_SUMMARY_ADAPTER.validate_python(
+ {
+ "page_id": page_id,
+ "page_name": page_name,
+ "page_icon": icon,
+ "parent_id": parent_id,
+ "type": "database",
+ }
+ )
+ )
+ return pages
+
+ def notion_page_search(self, access_token: str) -> list[dict[str, Any]]:
+ return self._search(access_token, object_type="page")
+
+ def notion_database_search(self, access_token: str) -> list[dict[str, Any]]:
+ return self._search(access_token, object_type="database")
+
+ def _search(self, access_token: str, *, object_type: str) -> list[dict[str, Any]]:
+ results: list[dict[str, Any]] = []
+ next_cursor: str | None = None
+ has_more = True
+ while has_more:
+ data: dict[str, Any] = {
+ "filter": {"value": object_type, "property": "object"},
+ **({"start_cursor": next_cursor} if next_cursor else {}),
+ }
+ response_json = self._post_json(
+ self._NOTION_PAGE_SEARCH,
+ json=data,
+ headers=self._notion_headers(access_token, content_type=True),
+ )
+ raw_results = response_json.get("results", [])
+ if not isinstance(raw_results, list):
+ raise DataSourceOAuthError("Notion search response was invalid")
+ results.extend(raw_results)
+ has_more = response_json.get("has_more", False) is True
+ raw_cursor = response_json.get("next_cursor")
+ if has_more and (not isinstance(raw_cursor, str) or not raw_cursor):
+ raise DataSourceOAuthError("Notion search response did not include a valid next_cursor")
+ next_cursor = raw_cursor if isinstance(raw_cursor, str) else None
+ return results
+
+ def notion_block_parent_page_id(self, access_token: str, block_id: str) -> str:
+ response_json = self._get_json(
+ f"{self._NOTION_BLOCK_SEARCH}/{block_id}",
+ headers=self._notion_headers(access_token),
+ )
+ parent = response_json.get("parent")
+ if not isinstance(parent, dict):
+ raise DataSourceOAuthError("Notion block response was invalid")
+ parent_type = parent.get("type")
+ if not isinstance(parent_type, str) or parent_type not in parent:
+ raise DataSourceOAuthError("Notion block parent was invalid")
+ parent_id = parent[parent_type]
+ if not isinstance(parent_id, str):
+ raise DataSourceOAuthError("Notion block parent identifier was invalid")
+ if parent_type == "block_id":
+ return self.notion_block_parent_page_id(access_token, parent_id)
+ return parent_id
+
+ def notion_workspace_name(self, access_token: str) -> str:
+ response_json = self._get_json(self._NOTION_BOT_USER, headers=self._notion_headers(access_token))
+ if response_json.get("object") == "user":
+ user_type = response_json.get("type")
+ user_info = response_json.get(user_type) if isinstance(user_type, str) else None
+ if isinstance(user_info, dict) and isinstance(user_info.get("workspace_name"), str):
+ return user_info["workspace_name"]
+ return "workspace"
+
+ def _post_json(self, url: str, **kwargs: Any) -> dict[str, Any]:
+ try:
+ response = self._http_client.post(url, **kwargs)
+ except (httpx.HTTPError, MaxRetriesExceededError, ToolSSRFError) as exc:
+ raise DataSourceOAuthError("Notion request failed") from exc
+ return self._response_json(response)
+
+ def _get_json(self, url: str, **kwargs: Any) -> dict[str, Any]:
+ try:
+ response = self._http_client.get(url, **kwargs)
+ except (httpx.HTTPError, MaxRetriesExceededError, ToolSSRFError) as exc:
+ raise DataSourceOAuthError("Notion request failed") from exc
+ return self._response_json(response)
+
+ @staticmethod
+ def _response_json(response: httpx.Response) -> dict[str, Any]:
+ try:
+ response.raise_for_status()
+ payload = response.json()
+ except (httpx.HTTPError, ValueError) as exc:
+ raise DataSourceOAuthError("Notion returned an unsuccessful response") from exc
+ if not isinstance(payload, dict):
+ raise DataSourceOAuthError("Notion response was invalid")
+ return payload
+
+ @staticmethod
+ def _notion_headers(access_token: str, *, content_type: bool = False) -> dict[str, str]:
+ headers = {
+ "Authorization": f"Bearer {access_token}",
+ "Notion-Version": "2022-06-28",
+ }
+ if content_type:
+ headers["Content-Type"] = "application/json"
+ return headers
+
+ @staticmethod
+ def _optional_string(value: object) -> str | None:
+ return value if isinstance(value, str) else None
+
+ @staticmethod
+ def _build_source_info(
+ *,
+ workspace_name: str | None,
+ workspace_icon: str | None,
+ workspace_id: str | None,
+ pages: list[_NotionPageSummary],
+ ) -> _NotionSourceInfo:
+ return _NOTION_SOURCE_INFO_ADAPTER.validate_python(
+ {
+ "workspace_name": workspace_name,
+ "workspace_icon": workspace_icon,
+ "workspace_id": workspace_id,
+ "pages": pages,
+ "total": len(pages),
+ }
+ )
diff --git a/api/services/oauth_server.py b/api/services/oauth_server.py
deleted file mode 100644
index 47aa1bc99bf..00000000000
--- a/api/services/oauth_server.py
+++ /dev/null
@@ -1,94 +0,0 @@
-import enum
-import uuid
-
-from sqlalchemy import select
-from sqlalchemy.orm import Session, sessionmaker
-from werkzeug.exceptions import BadRequest
-
-from extensions.ext_database import db
-from extensions.ext_redis import redis_client
-from models import Account
-from models.model import OAuthProviderApp
-from services.account_service import AccountService
-
-
-class OAuthGrantType(enum.StrEnum):
- AUTHORIZATION_CODE = "authorization_code"
- REFRESH_TOKEN = "refresh_token"
-
-
-OAUTH_AUTHORIZATION_CODE_REDIS_KEY = "oauth_provider:{client_id}:authorization_code:{code}"
-OAUTH_ACCESS_TOKEN_REDIS_KEY = "oauth_provider:{client_id}:access_token:{token}"
-OAUTH_ACCESS_TOKEN_EXPIRES_IN = 60 * 60 * 12 # 12 hours
-OAUTH_REFRESH_TOKEN_REDIS_KEY = "oauth_provider:{client_id}:refresh_token:{token}"
-OAUTH_REFRESH_TOKEN_EXPIRES_IN = 60 * 60 * 24 * 30 # 30 days
-
-
-class OAuthServerService:
- @staticmethod
- def get_oauth_provider_app(client_id: str) -> OAuthProviderApp | None:
- query = select(OAuthProviderApp).where(OAuthProviderApp.client_id == client_id)
-
- with sessionmaker(db.engine, expire_on_commit=False).begin() as session:
- return session.execute(query).scalar_one_or_none()
-
- @staticmethod
- def sign_oauth_authorization_code(client_id: str, user_account_id: str) -> str:
- code = str(uuid.uuid4())
- redis_key = OAUTH_AUTHORIZATION_CODE_REDIS_KEY.format(client_id=client_id, code=code)
- redis_client.set(redis_key, user_account_id, ex=60 * 10) # 10 minutes
- return code
-
- @staticmethod
- def sign_oauth_access_token(
- grant_type: OAuthGrantType,
- code: str = "",
- client_id: str = "",
- refresh_token: str = "",
- ) -> tuple[str, str]:
- match grant_type:
- case OAuthGrantType.AUTHORIZATION_CODE:
- redis_key = OAUTH_AUTHORIZATION_CODE_REDIS_KEY.format(client_id=client_id, code=code)
- user_account_id = redis_client.get(redis_key)
- if not user_account_id:
- raise BadRequest("invalid code")
-
- # delete code
- redis_client.delete(redis_key)
-
- access_token = OAuthServerService._sign_oauth_access_token(client_id, user_account_id)
- refresh_token = OAuthServerService._sign_oauth_refresh_token(client_id, user_account_id)
- return access_token, refresh_token
- case OAuthGrantType.REFRESH_TOKEN:
- redis_key = OAUTH_REFRESH_TOKEN_REDIS_KEY.format(client_id=client_id, token=refresh_token)
- user_account_id = redis_client.get(redis_key)
- if not user_account_id:
- raise BadRequest("invalid refresh token")
-
- access_token = OAuthServerService._sign_oauth_access_token(client_id, user_account_id)
- return access_token, refresh_token
-
- @staticmethod
- def _sign_oauth_access_token(client_id: str, user_account_id: str) -> str:
- token = str(uuid.uuid4())
- redis_key = OAUTH_ACCESS_TOKEN_REDIS_KEY.format(client_id=client_id, token=token)
- redis_client.set(redis_key, user_account_id, ex=OAUTH_ACCESS_TOKEN_EXPIRES_IN)
- return token
-
- @staticmethod
- def _sign_oauth_refresh_token(client_id: str, user_account_id: str) -> str:
- token = str(uuid.uuid4())
- redis_key = OAUTH_REFRESH_TOKEN_REDIS_KEY.format(client_id=client_id, token=token)
- redis_client.set(redis_key, user_account_id, ex=OAUTH_REFRESH_TOKEN_EXPIRES_IN)
- return token
-
- @staticmethod
- def validate_oauth_access_token(client_id: str, token: str, session: Session) -> Account | None:
- redis_key = OAUTH_ACCESS_TOKEN_REDIS_KEY.format(client_id=client_id, token=token)
- user_account_id = redis_client.get(redis_key)
- if not user_account_id:
- return None
-
- user_id_str = user_account_id.decode("utf-8")
-
- return AccountService.load_user(user_id_str, session)
diff --git a/api/services/oauth_server_service.py b/api/services/oauth_server_service.py
new file mode 100644
index 00000000000..af5bef2f5fd
--- /dev/null
+++ b/api/services/oauth_server_service.py
@@ -0,0 +1,157 @@
+"""Application service for OAuth authorization-server use cases."""
+
+from typing import Protocol
+
+from machinery.context import RequestContext
+from services.entities.oauth_server_entities import (
+ OAuthAuthorizationCode,
+ OAuthGrantType,
+ OAuthProviderAccount,
+ OAuthProviderAccountRecord,
+ OAuthProviderAccountStatus,
+ OAuthProviderAppPresentation,
+ OAuthProviderAppRecord,
+ OAuthTokenSet,
+)
+
+
+class OAuthServerClientNotFoundError(Exception):
+ """Raised when an OAuth client identifier is unknown."""
+
+
+class OAuthServerRequestError(Exception):
+ """Raised when an OAuth request violates the authorization-server contract."""
+
+
+class OAuthServerUnauthorizedError(Exception):
+ """Raised when OAuth credentials cannot resolve an authorized account."""
+
+
+OAUTH_AUTHORIZATION_CODE_EXPIRES_IN = 60 * 10 # 10 minutes
+OAUTH_ACCESS_TOKEN_EXPIRES_IN = 60 * 60 * 12 # 12 hours
+OAUTH_REFRESH_TOKEN_EXPIRES_IN = 60 * 60 * 24 * 30 # 30 days
+
+
+class OAuthServerRepository(Protocol):
+ def get_provider_app_by_client_id(self, client_id: str) -> OAuthProviderAppRecord | None: ...
+
+ def get_account_by_id(self, account_id: str) -> OAuthProviderAccountRecord | None: ...
+
+
+class OAuthServerTokenRepository(Protocol):
+ def issue_authorization_code(self, client_id: str, account_id: str) -> str: ...
+
+ def exchange_authorization_code(self, client_id: str, code: str) -> tuple[str, str]: ...
+
+ def refresh_access_token(self, client_id: str, refresh_token: str) -> tuple[str, str]: ...
+
+ def resolve_account_id(self, client_id: str, access_token: str) -> str | None: ...
+
+
+class OAuthServerService:
+ def __init__(
+ self,
+ *,
+ repository: OAuthServerRepository,
+ tokens: OAuthServerTokenRepository,
+ access_token_expires_in: int,
+ ) -> None:
+ self._repository = repository
+ self._tokens = tokens
+ self._access_token_expires_in = access_token_expires_in
+
+ def get_provider(self, *, client_id: str, redirect_uri: str) -> OAuthProviderAppPresentation:
+ provider_app = self._require_provider_app(client_id)
+ self._validate_redirect_uri(provider_app, redirect_uri)
+ return OAuthProviderAppPresentation(
+ app_icon=provider_app.app_icon,
+ app_label=provider_app.app_label,
+ scope=provider_app.scope,
+ auto_authorize=provider_app.auto_authorize,
+ )
+
+ def authorize(self, context: RequestContext, *, client_id: str) -> OAuthAuthorizationCode:
+ return self.issue_authorization_code(client_id=client_id, account_id=context.account_id)
+
+ def issue_authorization_code(self, *, client_id: str, account_id: str) -> OAuthAuthorizationCode:
+ provider_app = self._require_provider_app(client_id)
+ code = self._tokens.issue_authorization_code(provider_app.client_id, account_id)
+ return OAuthAuthorizationCode(code=code)
+
+ def exchange_token(
+ self,
+ *,
+ client_id: str,
+ grant_type: str,
+ code: str | None,
+ client_secret: str | None,
+ redirect_uri: str | None,
+ refresh_token: str | None,
+ ) -> OAuthTokenSet:
+ provider_app = self._require_provider_app(client_id)
+ try:
+ parsed_grant_type = OAuthGrantType(grant_type)
+ except ValueError as exc:
+ raise OAuthServerRequestError("invalid grant_type") from exc
+
+ match parsed_grant_type:
+ case OAuthGrantType.AUTHORIZATION_CODE:
+ if not code:
+ raise OAuthServerRequestError("code is required")
+ if client_secret != provider_app.client_secret:
+ raise OAuthServerRequestError("client_secret is invalid")
+ if redirect_uri is None:
+ raise OAuthServerRequestError("redirect_uri is invalid")
+ self._validate_redirect_uri(provider_app, redirect_uri)
+ access_token, issued_refresh_token = self._tokens.exchange_authorization_code(
+ provider_app.client_id,
+ code,
+ )
+ case OAuthGrantType.REFRESH_TOKEN:
+ if not refresh_token:
+ raise OAuthServerRequestError("refresh_token is required")
+ access_token, issued_refresh_token = self._tokens.refresh_access_token(
+ provider_app.client_id,
+ refresh_token,
+ )
+
+ return OAuthTokenSet(
+ access_token=access_token,
+ token_type="Bearer",
+ expires_in=self._access_token_expires_in,
+ refresh_token=issued_refresh_token,
+ )
+
+ def get_account(self, *, client_id: str, access_token: str | None) -> OAuthProviderAccount:
+ provider_app = self._require_provider_app(client_id)
+ if access_token is None:
+ raise OAuthServerUnauthorizedError("access_token is required")
+ account_id = self._tokens.resolve_account_id(provider_app.client_id, access_token)
+ if account_id is None:
+ raise OAuthServerUnauthorizedError("access_token or client_id is invalid")
+ account = self._repository.get_account_by_id(account_id)
+ if account is None:
+ raise OAuthServerUnauthorizedError("access_token or client_id is invalid")
+ if account.status != OAuthProviderAccountStatus.ACTIVE:
+ if account.status == OAuthProviderAccountStatus.BANNED:
+ raise OAuthServerUnauthorizedError("Account is banned.")
+ raise OAuthServerUnauthorizedError("Account is not active.")
+ return OAuthProviderAccount(
+ id=account.id,
+ name=account.name,
+ email=account.email,
+ avatar=account.avatar,
+ interface_language=account.interface_language,
+ timezone=account.timezone,
+ )
+
+ def _require_provider_app(self, client_id: str) -> OAuthProviderAppRecord:
+ provider_app = self._repository.get_provider_app_by_client_id(client_id)
+ if provider_app is None:
+ raise OAuthServerClientNotFoundError("client_id is invalid")
+ return provider_app
+
+ @staticmethod
+ def _validate_redirect_uri(provider_app: OAuthProviderAppRecord, redirect_uri: str) -> None:
+ if redirect_uri not in provider_app.redirect_uris:
+ raise OAuthServerRequestError("redirect_uri is invalid")
diff --git a/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_oauth.py b/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_oauth.py
index f2c45f76da3..c9e800235b7 100644
--- a/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_oauth.py
+++ b/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_oauth.py
@@ -1,17 +1,26 @@
"""Controller integration tests for console OAuth data source routes."""
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, create_autospec, patch
from flask.testing import FlaskClient
from sqlalchemy.orm import Session
+from extensions.ext_application_services import ApplicationServices
from models.source import DataSourceOauthBinding
+from services.data_source_oauth_service import InvalidDataSourceOAuthProviderError
+from services.entities.data_source_oauth_entities import DataSourceOAuthCallback
from tests.test_containers_integration_tests.controllers.console.helpers import (
authenticate_console_client,
create_console_account_and_tenant,
)
+def _services(service: MagicMock) -> MagicMock:
+ services = create_autospec(ApplicationServices, instance=True, spec_set=True)
+ services.resolve_data_source_oauth.return_value = service
+ return services
+
+
def test_get_oauth_url_successful(
db_session_with_containers: Session,
test_client_with_containers: FlaskClient,
@@ -19,12 +28,12 @@ def test_get_oauth_url_successful(
account, tenant = create_console_account_and_tenant(db_session_with_containers)
tenant_id = tenant.id
current_tenant_id = account.current_tenant_id
- provider = MagicMock()
- provider.get_authorization_url.return_value = "http://oauth.provider/auth"
+ service = MagicMock()
+ service.start_authorization.return_value = "http://oauth.provider/auth"
- with (
- patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": provider}),
- patch("controllers.console.auth.data_source_oauth.dify_config.NOTION_INTEGRATION_TYPE", None),
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
):
response = test_client_with_containers.get(
"/console/api/oauth/data-source/notion",
@@ -34,7 +43,7 @@ def test_get_oauth_url_successful(
assert tenant_id == current_tenant_id
assert response.status_code == 200
assert response.get_json() == {"data": "http://oauth.provider/auth"}
- provider.get_authorization_url.assert_called_once()
+ service.start_authorization.assert_called_once()
def test_get_oauth_url_invalid_provider(
@@ -43,7 +52,13 @@ def test_get_oauth_url_invalid_provider(
) -> None:
account, _tenant = create_console_account_and_tenant(db_session_with_containers)
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": MagicMock()}):
+ service = MagicMock()
+ services = _services(service)
+ services.resolve_data_source_oauth.side_effect = InvalidDataSourceOAuthProviderError
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=services,
+ ):
response = test_client_with_containers.get(
"/console/api/oauth/data-source/unknown_provider",
headers=authenticate_console_client(test_client_with_containers, account),
@@ -54,7 +69,12 @@ def test_get_oauth_url_invalid_provider(
def test_oauth_callback_successful(test_client_with_containers: FlaskClient) -> None:
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": MagicMock()}):
+ service = MagicMock()
+ service.complete_callback.return_value = DataSourceOAuthCallback(provider="notion", code="mock_code", error=None)
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
response = test_client_with_containers.get("/console/api/oauth/data-source/callback/notion?code=mock_code")
assert response.status_code == 302
@@ -62,7 +82,14 @@ def test_oauth_callback_successful(test_client_with_containers: FlaskClient) ->
def test_oauth_callback_missing_code(test_client_with_containers: FlaskClient) -> None:
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": MagicMock()}):
+ service = MagicMock()
+ service.complete_callback.return_value = DataSourceOAuthCallback(
+ provider="notion", code=None, error="Access denied"
+ )
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
response = test_client_with_containers.get("/console/api/oauth/data-source/callback/notion")
assert response.status_code == 302
@@ -70,26 +97,53 @@ def test_oauth_callback_missing_code(test_client_with_containers: FlaskClient) -
def test_oauth_callback_invalid_provider(test_client_with_containers: FlaskClient) -> None:
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": MagicMock()}):
+ service = MagicMock()
+ services = _services(service)
+ services.resolve_data_source_oauth.side_effect = InvalidDataSourceOAuthProviderError
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=services,
+ ):
response = test_client_with_containers.get("/console/api/oauth/data-source/callback/invalid?code=mock_code")
assert response.status_code == 400
assert response.get_json() == {"error": "Invalid provider"}
-def test_get_binding_successful(test_client_with_containers: FlaskClient) -> None:
- provider = MagicMock()
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": provider}):
- response = test_client_with_containers.get("/console/api/oauth/data-source/binding/notion?code=auth_code_123")
+def test_get_binding_successful(
+ db_session_with_containers: Session,
+ test_client_with_containers: FlaskClient,
+) -> None:
+ account, _tenant = create_console_account_and_tenant(db_session_with_containers)
+ service = MagicMock()
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
+ response = test_client_with_containers.get(
+ "/console/api/oauth/data-source/binding/notion?code=auth_code_123",
+ headers=authenticate_console_client(test_client_with_containers, account),
+ )
assert response.status_code == 200
assert response.get_json() == {"result": "success"}
- provider.get_access_token.assert_called_once_with("auth_code_123")
+ service.bind.assert_called_once()
-def test_get_binding_missing_code(test_client_with_containers: FlaskClient) -> None:
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": MagicMock()}):
- response = test_client_with_containers.get("/console/api/oauth/data-source/binding/notion?code=")
+def test_get_binding_missing_code(
+ db_session_with_containers: Session,
+ test_client_with_containers: FlaskClient,
+) -> None:
+ account, _tenant = create_console_account_and_tenant(db_session_with_containers)
+ service = MagicMock()
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
+ response = test_client_with_containers.get(
+ "/console/api/oauth/data-source/binding/notion?code=",
+ headers=authenticate_console_client(test_client_with_containers, account),
+ )
assert response.status_code == 400
assert response.get_json() == {"error": "Invalid code"}
@@ -110,8 +164,11 @@ def test_sync_successful(
db_session_with_containers.add(binding)
db_session_with_containers.commit()
- provider = MagicMock()
- with patch("controllers.console.auth.data_source_oauth.get_oauth_providers", return_value={"notion": provider}):
+ service = MagicMock()
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
response = test_client_with_containers.get(
f"/console/api/oauth/data-source/notion/{binding.id}/sync",
headers=authenticate_console_client(test_client_with_containers, account),
@@ -119,4 +176,4 @@ def test_sync_successful(
assert response.status_code == 200
assert response.get_json() == {"result": "success"}
- provider.sync_data_source.assert_called_once_with(binding.id)
+ service.sync.assert_called_once()
diff --git a/api/tests/test_containers_integration_tests/controllers/console/auth/test_oauth_server.py b/api/tests/test_containers_integration_tests/controllers/console/auth/test_oauth_server.py
index b41efa4e7b5..a4cd1b71973 100644
--- a/api/tests/test_containers_integration_tests/controllers/console/auth/test_oauth_server.py
+++ b/api/tests/test_containers_integration_tests/controllers/console/auth/test_oauth_server.py
@@ -1,12 +1,24 @@
"""Controller integration tests for console OAuth server routes."""
-from unittest.mock import patch
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+import pytest
from flask.testing import FlaskClient
from sqlalchemy.orm import Session
-from models.model import OAuthProviderApp
-from services.oauth_server import OAUTH_ACCESS_TOKEN_EXPIRES_IN
+from services.entities.oauth_server_entities import (
+ OAuthAuthorizationCode,
+ OAuthProviderAccount,
+ OAuthProviderAppPresentation,
+ OAuthTokenSet,
+)
+from services.oauth_server_service import (
+ OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ OAuthServerClientNotFoundError,
+ OAuthServerRequestError,
+ OAuthServerUnauthorizedError,
+)
from tests.test_containers_integration_tests.controllers.console.helpers import (
authenticate_console_client,
create_console_account_and_tenant,
@@ -14,15 +26,8 @@ from tests.test_containers_integration_tests.controllers.console.helpers import
)
-def _build_oauth_provider_app() -> OAuthProviderApp:
- return OAuthProviderApp(
- app_icon="icon_url",
- client_id="test_client_id",
- client_secret="test_secret",
- app_label={"en-US": "Test App"},
- redirect_uris=["http://localhost/callback"],
- scope="read,write",
- )
+def _application_services(service: MagicMock) -> SimpleNamespace:
+ return SimpleNamespace(oauth_server=service)
def test_oauth_provider_successful_post(
@@ -30,10 +35,17 @@ def test_oauth_provider_successful_post(
test_client_with_containers: FlaskClient,
) -> None:
ensure_dify_setup(db_session_with_containers)
+ service = MagicMock()
+ service.get_provider.return_value = OAuthProviderAppPresentation(
+ app_icon="icon_url",
+ app_label={"en-US": "Test App"},
+ scope="read,write",
+ auto_authorize=True,
+ )
with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider",
@@ -41,49 +53,43 @@ def test_oauth_provider_successful_post(
)
assert response.status_code == 200
- payload = response.get_json()
- assert payload is not None
- assert payload["app_icon"] == "icon_url"
- assert payload["app_label"] == {"en-US": "Test App"}
- assert payload["scope"] == "read,write"
+ assert response.get_json() == {
+ "app_icon": "icon_url",
+ "app_label": {"en-US": "Test App"},
+ "scope": "read,write",
+ "auto_authorize": True,
+ }
-def test_oauth_provider_invalid_redirect_uri(
+ ("error", "status", "message"),
+ [
+ (OAuthServerRequestError("redirect_uri is invalid"), 400, "redirect_uri is invalid"),
+ (OAuthServerClientNotFoundError("client_id is invalid"), 404, "client_id is invalid"),
+ ],
+)
+def test_oauth_provider_maps_application_errors(
db_session_with_containers: Session,
test_client_with_containers: FlaskClient,
+ error: Exception,
+ status: int,
+ message: str,
) -> None:
ensure_dify_setup(db_session_with_containers)
+ service = MagicMock()
+ service.get_provider.side_effect = error
with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider",
json={"client_id": "test_client_id", "redirect_uri": "http://invalid/callback"},
)
- assert response.status_code == 400
- payload = response.get_json()
- assert payload is not None
- assert "redirect_uri is invalid" in payload["message"]
-
-
-def test_oauth_provider_invalid_client_id(
- db_session_with_containers: Session,
- test_client_with_containers: FlaskClient,
-) -> None:
- ensure_dify_setup(db_session_with_containers)
-
- response = test_client_with_containers.post(
- "/console/api/oauth/provider",
- json={"client_id": "test_invalid_client_id", "redirect_uri": "http://localhost/callback"},
- )
-
- assert response.status_code == 404
- payload = response.get_json()
- assert payload is not None
- assert "client_id is invalid" in payload["message"]
+ assert response.status_code == status
+ assert message in response.get_json()["message"]
def test_oauth_authorize_successful(
@@ -91,16 +97,12 @@ def test_oauth_authorize_successful(
test_client_with_containers: FlaskClient,
) -> None:
account, _tenant = create_console_account_and_tenant(db_session_with_containers)
+ service = MagicMock()
+ service.authorize.return_value = OAuthAuthorizationCode(code="auth_code_123")
- with (
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
- ),
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.sign_oauth_authorization_code",
- return_value="auth_code_123",
- ) as mock_sign,
+ with patch(
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider/authorize",
@@ -110,7 +112,8 @@ def test_oauth_authorize_successful(
assert response.status_code == 200
assert response.get_json() == {"code": "auth_code_123"}
- mock_sign.assert_called_once_with("test_client_id", account.id)
+ context = service.authorize.call_args.args[0]
+ assert context.account_id == account.id
def test_oauth_token_authorization_code_grant(
@@ -118,16 +121,17 @@ def test_oauth_token_authorization_code_grant(
test_client_with_containers: FlaskClient,
) -> None:
ensure_dify_setup(db_session_with_containers)
+ service = MagicMock()
+ service.exchange_token.return_value = OAuthTokenSet(
+ access_token="access_123",
+ token_type="Bearer",
+ expires_in=OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ refresh_token="refresh_123",
+ )
- with (
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
- ),
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.sign_oauth_access_token",
- return_value=("access_123", "refresh_123"),
- ),
+ with patch(
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider/token",
@@ -149,146 +153,97 @@ def test_oauth_token_authorization_code_grant(
}
-def test_oauth_token_authorization_code_grant_missing_code(
- db_session_with_containers: Session,
- test_client_with_containers: FlaskClient,
-) -> None:
- ensure_dify_setup(db_session_with_containers)
-
- with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
- ):
- response = test_client_with_containers.post(
- "/console/api/oauth/provider/token",
- json={
+ ("payload", "message"),
+ [
+ (
+ {
"client_id": "test_client_id",
"grant_type": "authorization_code",
"client_secret": "test_secret",
"redirect_uri": "http://localhost/callback",
},
- )
-
- assert response.status_code == 400
- assert response.get_json()["message"] == "code is required"
-
-
-def test_oauth_token_authorization_code_grant_invalid_secret(
- db_session_with_containers: Session,
- test_client_with_containers: FlaskClient,
-) -> None:
- ensure_dify_setup(db_session_with_containers)
-
- with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
- ):
- response = test_client_with_containers.post(
- "/console/api/oauth/provider/token",
- json={
+ "code is required",
+ ),
+ (
+ {
"client_id": "test_client_id",
"grant_type": "authorization_code",
"code": "auth_code",
"client_secret": "invalid_secret",
"redirect_uri": "http://localhost/callback",
},
- )
-
- assert response.status_code == 400
- assert response.get_json()["message"] == "client_secret is invalid"
-
-
-def test_oauth_token_authorization_code_grant_invalid_redirect_uri(
- db_session_with_containers: Session,
- test_client_with_containers: FlaskClient,
-) -> None:
- ensure_dify_setup(db_session_with_containers)
-
- with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
- ):
- response = test_client_with_containers.post(
- "/console/api/oauth/provider/token",
- json={
+ "client_secret is invalid",
+ ),
+ (
+ {
"client_id": "test_client_id",
"grant_type": "authorization_code",
"code": "auth_code",
"client_secret": "test_secret",
"redirect_uri": "http://invalid/callback",
},
- )
-
- assert response.status_code == 400
- assert response.get_json()["message"] == "redirect_uri is invalid"
-
-
-def test_oauth_token_refresh_token_grant(
- db_session_with_containers: Session,
- test_client_with_containers: FlaskClient,
-) -> None:
- ensure_dify_setup(db_session_with_containers)
-
- with (
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "redirect_uri is invalid",
),
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.sign_oauth_access_token",
- return_value=("new_access", "new_refresh"),
+ (
+ {"client_id": "test_client_id", "grant_type": "refresh_token"},
+ "refresh_token is required",
),
- ):
- response = test_client_with_containers.post(
- "/console/api/oauth/provider/token",
- json={"client_id": "test_client_id", "grant_type": "refresh_token", "refresh_token": "refresh_123"},
- )
-
- assert response.status_code == 200
- assert response.get_json() == {
- "access_token": "new_access",
- "token_type": "Bearer",
- "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- "refresh_token": "new_refresh",
- }
-
-
-def test_oauth_token_refresh_token_grant_missing_token(
+ (
+ {"client_id": "test_client_id", "grant_type": "invalid_grant"},
+ "invalid grant_type",
+ ),
+ ],
+)
+def test_oauth_token_maps_invalid_grant_requests(
db_session_with_containers: Session,
test_client_with_containers: FlaskClient,
+ payload: dict[str, str],
+ message: str,
) -> None:
ensure_dify_setup(db_session_with_containers)
+ service = MagicMock()
+ service.exchange_token.side_effect = OAuthServerRequestError(message)
with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
- response = test_client_with_containers.post(
- "/console/api/oauth/provider/token",
- json={"client_id": "test_client_id", "grant_type": "refresh_token"},
- )
+ response = test_client_with_containers.post("/console/api/oauth/provider/token", json=payload)
assert response.status_code == 400
- assert response.get_json()["message"] == "refresh_token is required"
+ assert response.get_json()["message"] == message
-def test_oauth_token_invalid_grant_type(
+def test_oauth_token_refresh_token_grant(
db_session_with_containers: Session,
test_client_with_containers: FlaskClient,
) -> None:
ensure_dify_setup(db_session_with_containers)
+ service = MagicMock()
+ service.exchange_token.return_value = OAuthTokenSet(
+ access_token="new_access",
+ token_type="Bearer",
+ expires_in=OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ refresh_token="new_refresh",
+ )
with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider/token",
- json={"client_id": "test_client_id", "grant_type": "invalid_grant"},
+ json={"client_id": "test_client_id", "grant_type": "refresh_token", "refresh_token": "refresh_123"},
)
- assert response.status_code == 400
- assert response.get_json()["message"] == "invalid grant_type"
+ assert response.status_code == 200
+ assert response.get_json() == {
+ "access_token": "new_access",
+ "token_type": "Bearer",
+ "expires_in": OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ "refresh_token": "new_refresh",
+ }
def test_oauth_account_successful_retrieval(
@@ -296,19 +251,19 @@ def test_oauth_account_successful_retrieval(
test_client_with_containers: FlaskClient,
) -> None:
ensure_dify_setup(db_session_with_containers)
- account, _tenant = create_console_account_and_tenant(db_session_with_containers)
- account.avatar = "avatar_url"
- db_session_with_containers.commit()
+ service = MagicMock()
+ service.get_account.return_value = OAuthProviderAccount(
+ id="account-1",
+ name="Test User",
+ avatar=None,
+ interface_language=None,
+ timezone=None,
+ )
- with (
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
- ),
- patch(
- "controllers.console.auth.oauth_server.OAuthServerService.validate_oauth_access_token",
- return_value=account,
- ),
+ with patch(
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider/account",
@@ -318,49 +273,63 @@ def test_oauth_account_successful_retrieval(
assert response.status_code == 200
assert response.get_json() == {
- "id": account.id,
+ "id": "account-1",
"name": "Test User",
- "email": account.email,
- "avatar": "avatar_url",
- "interface_language": "en-US",
- "timezone": "UTC",
+ "email": "[email protected]",
+ "avatar": None,
+ "interface_language": None,
+ "timezone": None,
}
-def test_oauth_account_missing_authorization_header(
+ ("headers", "message"),
+ [
+ ({}, "Authorization header is required"),
+ ({"Authorization": "InvalidFormat"}, "Invalid Authorization header format"),
+ ({"Authorization": "Basic token"}, "token_type is invalid"),
+ ({"Authorization": "Bearer "}, "Invalid Authorization header format"),
+ ],
+)
+def test_oauth_account_rejects_invalid_authorization_header(
db_session_with_containers: Session,
test_client_with_containers: FlaskClient,
+ headers: dict[str, str],
+ message: str,
) -> None:
ensure_dify_setup(db_session_with_containers)
-
+ service = MagicMock()
+ service.get_account.side_effect = OAuthServerUnauthorizedError("access_token is required")
with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider/account",
json={"client_id": "test_client_id"},
+ headers=headers,
)
assert response.status_code == 401
- assert response.get_json() == {"error": "Authorization header is required"}
+ assert response.get_json() == {"error": message}
+ assert response.headers["WWW-Authenticate"] == "Bearer"
-def test_oauth_account_invalid_authorization_header_format(
+def test_oauth_account_validates_client_before_authorization_header(
db_session_with_containers: Session,
test_client_with_containers: FlaskClient,
) -> None:
ensure_dify_setup(db_session_with_containers)
-
+ service = MagicMock()
+ service.get_account.side_effect = OAuthServerClientNotFoundError("client_id is invalid")
with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.get_oauth_provider_app",
- return_value=_build_oauth_provider_app(),
+ "controllers.console.auth.oauth_server.application_services",
+ return_value=_application_services(service),
):
response = test_client_with_containers.post(
"/console/api/oauth/provider/account",
- json={"client_id": "test_client_id"},
- headers={"Authorization": "InvalidFormat"},
+ json={"client_id": "invalid"},
)
- assert response.status_code == 401
- assert response.get_json() == {"error": "Invalid Authorization header format"}
+ assert response.status_code == 404
+ assert "client_id is invalid" in response.get_json()["message"]
diff --git a/api/tests/unit_tests/controllers/console/app/test_app_apis.py b/api/tests/unit_tests/controllers/console/app/test_app_apis.py
index 81ba3c6b3df..42c5bd81f0b 100644
--- a/api/tests/unit_tests/controllers/console/app/test_app_apis.py
+++ b/api/tests/unit_tests/controllers/console/app/test_app_apis.py
@@ -241,6 +241,36 @@ class TestCompletionEndpoints:
class TestAppEndpoints:
+ def test_publish_to_creators_platform_issues_oauth_code_through_application_service(
+ self,
+ database_app: Flask,
+ monkeypatch: pytest.MonkeyPatch,
+ ) -> None:
+ api = app_module.AppPublishToCreatorsPlatformApi()
+ method = unwrap(api.post)
+ oauth_server = MagicMock()
+ oauth_server.issue_authorization_code.return_value = MagicMock(code="oauth-code-1")
+ services = MagicMock(oauth_server=oauth_server)
+
+ monkeypatch.setattr(app_module.dify_config, "CREATORS_PLATFORM_FEATURES_ENABLED", True)
+ monkeypatch.setattr(app_module.dify_config, "CREATORS_PLATFORM_OAUTH_CLIENT_ID", "client-1")
+ monkeypatch.setattr(app_module, "application_services", lambda: services)
+ monkeypatch.setattr(app_module.AppDslService, "export_dsl", MagicMock(return_value="app: demo"))
+
+ with (
+ database_app.test_request_context(),
+ patch("core.helper.creators.upload_dsl", return_value="claim-1"),
+ patch("core.helper.creators.get_redirect_url", return_value="https://creators.example.com") as redirect,
+ ):
+ response = method(api, USER_ID, _make_app())
+
+ assert response == {"redirect_url": "https://creators.example.com"}
+ oauth_server.issue_authorization_code.assert_called_once_with(
+ client_id="client-1",
+ account_id=USER_ID,
+ )
+ redirect.assert_called_once_with("claim-1", oauth_code="oauth-code-1")
+
def test_app_put_should_preserve_icon_type_when_payload_omits_it(
self, app: Flask, monkeypatch: pytest.MonkeyPatch, unbound_session: Session
):
diff --git a/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py b/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py
new file mode 100644
index 00000000000..1f6cd681a75
--- /dev/null
+++ b/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py
@@ -0,0 +1,173 @@
+from http import HTTPStatus
+from inspect import unwrap
+from unittest.mock import MagicMock, create_autospec, patch
+from uuid import UUID
+
+from flask import Flask
+
+from controllers.console.auth.data_source_oauth import (
+ OAuthDataSource,
+ OAuthDataSourceBinding,
+ OAuthDataSourceCallback,
+ OAuthDataSourceSync,
+)
+from extensions.ext_application_services import ApplicationServices
+from machinery.context import RequestContext
+from services.data_source_oauth_service import (
+ DataSourceOAuthError,
+ InvalidDataSourceOAuthCodeError,
+ InvalidDataSourceOAuthProviderError,
+)
+from services.entities.data_source_oauth_entities import DataSourceOAuthCallback
+
+
+def _request_context() -> RequestContext:
+ return RequestContext(
+ request_id="request-1",
+ trace_id=None,
+ account_id="account-1",
+ active_workspace_id="workspace-1",
+ )
+
+
+def _services(service: MagicMock) -> MagicMock:
+ services = create_autospec(ApplicationServices, instance=True, spec_set=True)
+ services.resolve_data_source_oauth.return_value = service
+ return services
+
+
+def test_get_delegates_to_application_service_and_serializes_response() -> None:
+ service = MagicMock()
+ service.start_authorization.return_value = "https://notion.example/authorize"
+ context = _request_context()
+
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
+ result = unwrap(OAuthDataSource.get)(OAuthDataSource(), context, "notion")
+
+ assert result == ({"data": "https://notion.example/authorize"}, HTTPStatus.OK)
+ service.start_authorization.assert_called_once_with(context)
+
+
+def test_get_maps_unknown_provider_to_bad_request() -> None:
+ service = MagicMock()
+ services = _services(service)
+ services.resolve_data_source_oauth.side_effect = InvalidDataSourceOAuthProviderError
+
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=services,
+ ):
+ result = unwrap(OAuthDataSource.get)(OAuthDataSource(), _request_context(), "unknown")
+
+ assert result == ({"error": "Invalid provider"}, HTTPStatus.BAD_REQUEST)
+
+
+def test_callback_parses_query_and_returns_flask_redirect() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.complete_callback.return_value = DataSourceOAuthCallback(provider="notion", code="code-1", error=None)
+
+ with (
+ app.test_request_context("/?code=code-1"),
+ patch(
+ "controllers.console.auth.data_source_oauth.dify_config.CONSOLE_WEB_URL",
+ "https://console.example/root?lang=en#top",
+ ),
+ patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ),
+ ):
+ response = OAuthDataSourceCallback().get("notion")
+
+ assert response.status_code == HTTPStatus.FOUND
+ assert response.location == "https://console.example/root?lang=en&type=notion&code=code-1#top"
+ service.complete_callback.assert_called_once_with(code="code-1", error=None)
+
+
+def test_binding_parses_query_and_delegates_with_request_context() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ context = _request_context()
+
+ with (
+ app.test_request_context("/?code=code-1"),
+ patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ),
+ ):
+ result = unwrap(OAuthDataSourceBinding.get)(OAuthDataSourceBinding(), context, "notion")
+
+ assert result == ({"result": "success"}, HTTPStatus.OK)
+ service.bind.assert_called_once_with(context, code="code-1")
+
+
+def test_binding_maps_invalid_code_to_bad_request() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+
+ with (
+ app.test_request_context("/?code="),
+ patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ),
+ ):
+ result = unwrap(OAuthDataSourceBinding.get)(OAuthDataSourceBinding(), _request_context(), "notion")
+
+ assert result == ({"error": "Invalid code"}, HTTPStatus.BAD_REQUEST)
+ service.bind.assert_not_called()
+
+
+def test_binding_maps_application_code_error_to_bad_request() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.bind.side_effect = InvalidDataSourceOAuthCodeError
+
+ with (
+ app.test_request_context("/?code=code-1"),
+ patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ),
+ ):
+ result = unwrap(OAuthDataSourceBinding.get)(OAuthDataSourceBinding(), _request_context(), "notion")
+
+ assert result == ({"error": "Invalid code"}, HTTPStatus.BAD_REQUEST)
+
+
+def test_sync_converts_route_uuid_and_delegates() -> None:
+ service = MagicMock()
+ context = _request_context()
+ binding_id = UUID("11111111-1111-1111-1111-111111111111")
+
+ with patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ):
+ result = unwrap(OAuthDataSourceSync.get)(OAuthDataSourceSync(), context, "notion", binding_id)
+
+ assert result == ({"result": "success"}, HTTPStatus.OK)
+ service.sync.assert_called_once_with(context, binding_id=str(binding_id))
+
+
+def test_sync_hides_provider_failure_details() -> None:
+ service = MagicMock()
+ service.sync.side_effect = DataSourceOAuthError("provider details")
+ binding_id = UUID("11111111-1111-1111-1111-111111111111")
+
+ with (
+ patch(
+ "controllers.console.auth.data_source_oauth.application_services",
+ return_value=_services(service),
+ ),
+ patch("controllers.console.auth.data_source_oauth.logger.exception") as log_exception,
+ ):
+ result = unwrap(OAuthDataSourceSync.get)(OAuthDataSourceSync(), _request_context(), "notion", binding_id)
+
+ assert result == ({"error": "OAuth data source process failed"}, HTTPStatus.BAD_REQUEST)
+ log_exception.assert_called_once()
diff --git a/api/tests/unit_tests/controllers/console/auth/test_oauth_server.py b/api/tests/unit_tests/controllers/console/auth/test_oauth_server.py
index 7a03e089e9d..2718e600204 100644
--- a/api/tests/unit_tests/controllers/console/auth/test_oauth_server.py
+++ b/api/tests/unit_tests/controllers/console/auth/test_oauth_server.py
@@ -1,66 +1,224 @@
-from __future__ import annotations
-
+from http import HTTPStatus
from inspect import unwrap
-from unittest.mock import patch
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+import pytest
+from flask import Flask
+from werkzeug.exceptions import NotFound
from controllers.console.auth.oauth_server import (
OAuthProviderAppResponse,
- OAuthProviderRequest,
OAuthServerAppApi,
OAuthServerUserAccountApi,
OAuthServerUserAuthorizeApi,
+ OAuthServerUserTokenApi,
+)
+from machinery.context import RequestContext
+from services.entities.oauth_server_entities import (
+ OAuthAuthorizationCode,
+ OAuthProviderAccount,
+ OAuthProviderAppPresentation,
+ OAuthTokenSet,
)
-from models import Account
-from models.account import AccountStatus, TenantAccountRole
-from models.model import OAuthProviderApp
+from services.oauth_server_service import OAuthServerClientNotFoundError, OAuthServerUnauthorizedError
-def _make_account() -> Account:
- account = Account(
- name="Test User",
- status=AccountStatus.ACTIVE,
- )
- account.id = "account-1"
- account.role = TenantAccountRole.OWNER
- return account
+def _context() -> RequestContext:
+ return RequestContext("request-1", "trace-1", "account-1", "workspace-1")
-def _make_oauth_provider_app() -> OAuthProviderApp:
- return OAuthProviderApp(
+def _services(service: MagicMock) -> SimpleNamespace:
+ return SimpleNamespace(oauth_server=service)
+
+
+def test_provider_parses_payload_delegates_and_serializes() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.get_provider.return_value = OAuthProviderAppPresentation(
app_icon="icon",
- client_id="client-1",
- client_secret="secret",
app_label={"en-US": "Test App"},
- redirect_uris=["https://example.com/callback"],
scope="read",
+ auto_authorize=True,
+ )
+
+ with (
+ app.test_request_context(json={"client_id": "client-1", "redirect_uri": "https://example.com/callback"}),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ result = unwrap(OAuthServerAppApi.post)(OAuthServerAppApi())
+
+ assert result == (
+ {
+ "app_icon": "icon",
+ "app_label": {"en-US": "Test App"},
+ "scope": "read",
+ "auto_authorize": True,
+ },
+ HTTPStatus.OK,
+ )
+ service.get_provider.assert_called_once_with(
+ client_id="client-1",
+ redirect_uri="https://example.com/callback",
)
-def test_oauth_authorize_uses_injected_current_user() -> None:
- api = OAuthServerUserAuthorizeApi()
- method = unwrap(api.post)
- account = _make_account()
- oauth_provider_app = _make_oauth_provider_app()
+def test_authorize_uses_admission_request_context() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.authorize.return_value = OAuthAuthorizationCode(code="authorization-code")
+ context = _context()
- with patch(
- "controllers.console.auth.oauth_server.OAuthServerService.sign_oauth_authorization_code",
- return_value="authorization-code",
- ) as sign_oauth_authorization_code:
- response = method(api, oauth_provider_app, account)
+ with (
+ app.test_request_context(json={"client_id": "client-1"}),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ result = unwrap(OAuthServerUserAuthorizeApi.post)(OAuthServerUserAuthorizeApi(), context)
- sign_oauth_authorization_code.assert_called_once_with("client-1", "account-1")
- assert response == {"code": "authorization-code"}
+ assert result == ({"code": "authorization-code"}, HTTPStatus.OK)
+ service.authorize.assert_called_once_with(context, client_id="client-1")
-def test_oauth_account_returns_stable_account_id() -> None:
- api = OAuthServerUserAccountApi()
- method = unwrap(api.post)
- account = _make_account()
+def test_token_endpoint_delegates_grant_data() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.exchange_token.return_value = OAuthTokenSet(
+ access_token="access-1",
+ token_type="Bearer",
+ expires_in=43200,
+ refresh_token="refresh-1",
+ )
- response = method(api, _make_oauth_provider_app(), account)
+ with (
+ app.test_request_context(
+ json={
+ "client_id": "client-1",
+ "grant_type": "authorization_code",
+ "code": "code-1",
+ "client_secret": "secret",
+ "redirect_uri": "https://example.com/callback",
+ }
+ ),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ result = unwrap(OAuthServerUserTokenApi.post)(OAuthServerUserTokenApi())
+
+ assert result == (
+ {
+ "access_token": "access-1",
+ "token_type": "Bearer",
+ "expires_in": 43200,
+ "refresh_token": "refresh-1",
+ },
+ HTTPStatus.OK,
+ )
+ service.exchange_token.assert_called_once_with(
+ client_id="client-1",
+ grant_type="authorization_code",
+ code="code-1",
+ client_secret="secret",
+ redirect_uri="https://example.com/callback",
+ refresh_token=None,
+ )
- assert response["id"] == "account-1"
+
+def test_account_endpoint_parses_bearer_token_and_serializes_account() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.get_account.return_value = OAuthProviderAccount(
+ id="account-1",
+ name="Test User",
+ avatar="avatar",
+ interface_language="en-US",
+ timezone="UTC",
+ )
+
+ with (
+ app.test_request_context(
+ json={"client_id": "client-1"},
+ headers={"Authorization": "Bearer access-1"},
+ ),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ result = unwrap(OAuthServerUserAccountApi.post)(OAuthServerUserAccountApi())
+
+ assert result == (
+ {
+ "id": "account-1",
+ "name": "Test User",
+ "email": "[email protected]",
+ "avatar": "avatar",
+ "interface_language": "en-US",
+ "timezone": "UTC",
+ },
+ HTTPStatus.OK,
+ )
+ service.get_account.assert_called_once_with(client_id="client-1", access_token="access-1")
+
+
+def test_account_endpoint_serializes_nullable_account_preferences() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.get_account.return_value = OAuthProviderAccount(
+ id="account-1",
+ name="Test User",
+ avatar=None,
+ interface_language=None,
+ timezone=None,
+ )
+
+ with (
+ app.test_request_context(
+ json={"client_id": "client-1"},
+ headers={"Authorization": "Bearer access-1"},
+ ),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ result = unwrap(OAuthServerUserAccountApi.post)(OAuthServerUserAccountApi())
+
+ assert result == (
+ {
+ "id": "account-1",
+ "name": "Test User",
+ "email": "[email protected]",
+ "avatar": None,
+ "interface_language": None,
+ "timezone": None,
+ },
+ HTTPStatus.OK,
+ )
+
+
+def test_account_endpoint_returns_bearer_challenge_for_missing_header() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.get_account.side_effect = OAuthServerUnauthorizedError("access_token is required")
+
+ with (
+ app.test_request_context(json={"client_id": "client-1"}),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ response = unwrap(OAuthServerUserAccountApi.post)(OAuthServerUserAccountApi())
+
+ assert response.status_code == HTTPStatus.UNAUTHORIZED
+ assert response.get_json() == {"error": "Authorization header is required"}
+ assert response.headers["WWW-Authenticate"] == "Bearer"
+ service.get_account.assert_called_once_with(client_id="client-1", access_token=None)
+
+
+def test_account_endpoint_preserves_client_validation_before_bearer_challenge() -> None:
+ app = Flask(__name__)
+ service = MagicMock()
+ service.get_account.side_effect = OAuthServerClientNotFoundError("client_id is invalid")
+
+ with (
+ app.test_request_context(json={"client_id": "missing"}),
+ patch("controllers.console.auth.oauth_server.application_services", return_value=_services(service)),
+ ):
+ with pytest.raises(NotFound, match="client_id is invalid"):
+ unwrap(OAuthServerUserAccountApi.post)(OAuthServerUserAccountApi())
def test_oauth_provider_app_response_requires_auto_authorize() -> None:
@@ -68,17 +226,3 @@ def test_oauth_provider_app_response_requires_auto_authorize() -> None:
# an optional field would surface as `undefined` in the generated TS
# contract and silently disable silent authorization.
assert "auto_authorize" in OAuthProviderAppResponse.model_json_schema()["required"]
-
-
-def test_oauth_provider_returns_auto_authorize_flag() -> None:
- api = OAuthServerAppApi()
- method = unwrap(api.post)
- payload = OAuthProviderRequest(client_id="client-1", redirect_uri="https://example.com/callback")
-
- response = method(api, payload, _make_oauth_provider_app())
- assert response["auto_authorize"] is False
-
- opted_in = _make_oauth_provider_app()
- opted_in.auto_authorize = True
- response = method(api, payload, opted_in)
- assert response["auto_authorize"] is True
diff --git a/api/tests/unit_tests/core/helper/test_creators.py b/api/tests/unit_tests/core/helper/test_creators.py
index 6ac97240454..7734146770a 100644
--- a/api/tests/unit_tests/core/helper/test_creators.py
+++ b/api/tests/unit_tests/core/helper/test_creators.py
@@ -85,47 +85,39 @@ class TestUploadDSL:
class TestGetRedirectUrl:
- def test_without_oauth_client_id(self, config_overrides):
+ def test_without_oauth_code(self, config_overrides):
config_overrides(
CREATORS_PLATFORM_API_URL="https://creators.example.com",
- CREATORS_PLATFORM_OAUTH_CLIENT_ID="",
)
from core.helper.creators import get_redirect_url
- url = get_redirect_url("user-1", "claim-abc")
+ url = get_redirect_url("claim-abc")
assert "dsl_claim_code=claim-abc" in url
assert "oauth_code" not in url
assert url.startswith("https://creators.example.com")
- def test_with_oauth_client_id(self, config_overrides):
+ def test_with_oauth_code(self, config_overrides):
config_overrides(
CREATORS_PLATFORM_API_URL="https://creators.example.com",
- CREATORS_PLATFORM_OAUTH_CLIENT_ID="client-xyz",
)
- with patch(
- "services.oauth_server.OAuthServerService.sign_oauth_authorization_code",
- return_value="oauth-code-123",
- ) as mock_sign:
- from core.helper.creators import get_redirect_url
+ from core.helper.creators import get_redirect_url
- url = get_redirect_url("user-1", "claim-abc")
+ url = get_redirect_url("claim-abc", oauth_code="oauth-code-123")
- mock_sign.assert_called_once_with("client-xyz", "user-1")
- assert "dsl_claim_code=claim-abc" in url
- assert "oauth_code=oauth-code-123" in url
+ assert "dsl_claim_code=claim-abc" in url
+ assert "oauth_code=oauth-code-123" in url
def test_strips_trailing_slash(self, config_overrides):
config_overrides(
CREATORS_PLATFORM_API_URL="https://creators.example.com/",
- CREATORS_PLATFORM_OAUTH_CLIENT_ID="",
)
from core.helper.creators import get_redirect_url
- url = get_redirect_url("user-1", "claim-abc")
+ url = get_redirect_url("claim-abc")
assert url.startswith("https://creators.example.com?")
assert "creators.example.com/?" not in url
diff --git a/api/tests/unit_tests/extensions/test_ext_application_services.py b/api/tests/unit_tests/extensions/test_ext_application_services.py
index b8de66eb541..78a58828481 100644
--- a/api/tests/unit_tests/extensions/test_ext_application_services.py
+++ b/api/tests/unit_tests/extensions/test_ext_application_services.py
@@ -134,6 +134,7 @@ def test_build_application_services_configures_setup_policy(
)
assert services.setup.get_status().completed is setup_completed
+ assert services.oauth_server is not None
def test_build_application_services_wires_builtin_schema_definitions(
diff --git a/api/tests/unit_tests/repositories/test_data_source_oauth_binding_repository.py b/api/tests/unit_tests/repositories/test_data_source_oauth_binding_repository.py
new file mode 100644
index 00000000000..468c41f46fd
--- /dev/null
+++ b/api/tests/unit_tests/repositories/test_data_source_oauth_binding_repository.py
@@ -0,0 +1,120 @@
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from models.source import DataSourceOauthBinding
+from repositories.data_source_oauth_binding_repository import SQLAlchemyDataSourceOAuthBindingRepository
+from services.entities.data_source_oauth_entities import DataSourceOAuthAuthorization
+
+
+def test_upsert_authorization_inserts_and_reenables_existing_binding(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ repository = SQLAlchemyDataSourceOAuthBindingRepository(sqlite_session_factory)
+ authorization = DataSourceOAuthAuthorization(access_token="token", source_info={"pages": []})
+
+ repository.upsert_authorization(
+ workspace_id="workspace-1",
+ provider="notion",
+ authorization=authorization,
+ )
+ with sqlite_session_factory.begin() as session:
+ binding = session.scalar(select(DataSourceOauthBinding))
+ assert binding is not None
+ binding.disabled = True
+
+ updated_authorization = DataSourceOAuthAuthorization(
+ access_token="token",
+ source_info={"pages": [{"page_id": "page-1"}]},
+ )
+ repository.upsert_authorization(
+ workspace_id="workspace-1",
+ provider="notion",
+ authorization=updated_authorization,
+ )
+
+ with sqlite_session_factory() as session:
+ bindings = session.scalars(select(DataSourceOauthBinding)).all()
+ assert len(bindings) == 1
+ assert bindings[0].disabled is False
+ assert bindings[0].source_info == updated_authorization.source_info
+
+
+def test_get_enabled_scopes_by_workspace_provider_and_state(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ repository = SQLAlchemyDataSourceOAuthBindingRepository(sqlite_session_factory)
+ with sqlite_session_factory.begin() as session:
+ enabled = DataSourceOauthBinding(
+ tenant_id="workspace-1",
+ provider="notion",
+ access_token="enabled-token",
+ source_info={"pages": []},
+ disabled=False,
+ )
+ disabled = DataSourceOauthBinding(
+ tenant_id="workspace-1",
+ provider="notion",
+ access_token="disabled-token",
+ source_info={"pages": []},
+ disabled=True,
+ )
+ session.add_all([enabled, disabled])
+
+ record = repository.get_enabled(
+ workspace_id="workspace-1",
+ provider="notion",
+ binding_id=enabled.id,
+ )
+
+ assert record is not None
+ assert record.id == enabled.id
+ assert record.access_token == "enabled-token"
+ assert (
+ repository.get_enabled(
+ workspace_id="workspace-2",
+ provider="notion",
+ binding_id=enabled.id,
+ )
+ is None
+ )
+ assert (
+ repository.get_enabled(
+ workspace_id="workspace-1",
+ provider="notion",
+ binding_id=disabled.id,
+ )
+ is None
+ )
+
+
+def test_update_source_info_only_updates_matching_enabled_binding(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ repository = SQLAlchemyDataSourceOAuthBindingRepository(sqlite_session_factory)
+ with sqlite_session_factory.begin() as session:
+ binding = DataSourceOauthBinding(
+ tenant_id="workspace-1",
+ provider="notion",
+ access_token="token",
+ source_info={"pages": []},
+ disabled=False,
+ )
+ session.add(binding)
+
+ assert repository.update_source_info(
+ workspace_id="workspace-1",
+ provider="notion",
+ binding_id=binding.id,
+ source_info={"pages": [{"page_id": "page-1"}]},
+ )
+ assert not repository.update_source_info(
+ workspace_id="workspace-2",
+ provider="notion",
+ binding_id=binding.id,
+ source_info={"pages": []},
+ )
+
+ with sqlite_session_factory() as session:
+ updated = session.get(DataSourceOauthBinding, binding.id)
+ assert updated is not None
+ assert updated.source_info == {"pages": [{"page_id": "page-1"}]}
diff --git a/api/tests/unit_tests/repositories/test_oauth_server_repository.py b/api/tests/unit_tests/repositories/test_oauth_server_repository.py
new file mode 100644
index 00000000000..863e93e22ee
--- /dev/null
+++ b/api/tests/unit_tests/repositories/test_oauth_server_repository.py
@@ -0,0 +1,197 @@
+import uuid
+from unittest.mock import MagicMock, patch
+
+import pytest
+from sqlalchemy.orm import Session, sessionmaker
+
+from extensions.ext_redis import RedisClientWrapper
+from models import Account
+from models.account import AccountStatus
+from models.model import OAuthProviderApp
+from repositories.oauth_server_repository import RedisOAuthServerTokenRepository, SQLAlchemyOAuthServerRepository
+from services.entities.oauth_server_entities import (
+ OAuthProviderAccountRecord,
+ OAuthProviderAccountStatus,
+ OAuthProviderAppRecord,
+)
+from services.oauth_server_service import (
+ OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ OAUTH_AUTHORIZATION_CODE_EXPIRES_IN,
+ OAUTH_REFRESH_TOKEN_EXPIRES_IN,
+ OAuthServerRequestError,
+)
+
+
+def test_get_provider_app_by_client_id_maps_record(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ provider_app = OAuthProviderApp(
+ app_icon="icon",
+ client_id="client-1",
+ client_secret="secret",
+ app_label={"en-US": "Test App"},
+ redirect_uris=["https://example.com/callback"],
+ scope="read",
+ auto_authorize=True,
+ )
+ with sqlite_session_factory.begin() as session:
+ session.add(provider_app)
+
+ result = SQLAlchemyOAuthServerRepository(sqlite_session_factory).get_provider_app_by_client_id("client-1")
+
+ assert result == OAuthProviderAppRecord(
+ app_icon="icon",
+ client_id="client-1",
+ client_secret="secret",
+ app_label={"en-US": "Test App"},
+ redirect_uris=("https://example.com/callback",),
+ scope="read",
+ auto_authorize=True,
+ )
+
+
+def test_get_provider_app_by_client_id_returns_none_for_unknown_client(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ repository = SQLAlchemyOAuthServerRepository(sqlite_session_factory)
+
+ assert repository.get_provider_app_by_client_id("missing") is None
+
+
+ ("status", "expected_status"),
+ [
+ (AccountStatus.PENDING, OAuthProviderAccountStatus.PENDING),
+ (AccountStatus.UNINITIALIZED, OAuthProviderAccountStatus.UNINITIALIZED),
+ (AccountStatus.ACTIVE, OAuthProviderAccountStatus.ACTIVE),
+ (AccountStatus.BANNED, OAuthProviderAccountStatus.BANNED),
+ (AccountStatus.CLOSED, OAuthProviderAccountStatus.CLOSED),
+ ],
+)
+def test_get_account_by_id_maps_record(
+ sqlite_session_factory: sessionmaker[Session],
+ status: AccountStatus,
+ expected_status: OAuthProviderAccountStatus,
+) -> None:
+ account = Account(
+ name="Test User",
+ email=f"{status.value}@example.com",
+ avatar="avatar",
+ interface_language="en-US",
+ timezone="UTC",
+ status=status,
+ )
+ with sqlite_session_factory.begin() as session:
+ session.add(account)
+ session.flush()
+ account_id = account.id
+
+ result = SQLAlchemyOAuthServerRepository(sqlite_session_factory).get_account_by_id(account_id)
+
+ assert result == OAuthProviderAccountRecord(
+ id=account_id,
+ name="Test User",
+ email=f"{status.value}@example.com",
+ avatar="avatar",
+ interface_language="en-US",
+ timezone="UTC",
+ status=expected_status,
+ )
+
+
+def test_get_account_by_id_returns_none_for_unknown_account(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ repository = SQLAlchemyOAuthServerRepository(sqlite_session_factory)
+
+ assert repository.get_account_by_id("missing") is None
+
+
+def test_issue_authorization_code_stores_code_with_expiry() -> None:
+ redis = MagicMock(spec=RedisClientWrapper)
+ repository = RedisOAuthServerTokenRepository(redis)
+ deterministic_uuid = uuid.UUID("00000000-0000-0000-0000-000000000111")
+
+ with patch("repositories.oauth_server_repository.uuid.uuid4", return_value=deterministic_uuid):
+ code = repository.issue_authorization_code("client-1", "account-1")
+
+ assert code == str(deterministic_uuid)
+ redis.set.assert_called_once_with(
+ f"oauth_provider:client-1:authorization_code:{code}",
+ "account-1",
+ ex=OAUTH_AUTHORIZATION_CODE_EXPIRES_IN,
+ )
+
+
+def test_exchange_authorization_code_consumes_code_and_issues_tokens() -> None:
+ redis = MagicMock(spec=RedisClientWrapper)
+ redis.getdel.return_value = b"account-1"
+ repository = RedisOAuthServerTokenRepository(redis)
+ token_uuids = [
+ uuid.UUID("00000000-0000-0000-0000-000000000201"),
+ uuid.UUID("00000000-0000-0000-0000-000000000202"),
+ ]
+
+ with patch("repositories.oauth_server_repository.uuid.uuid4", side_effect=token_uuids):
+ access_token, refresh_token = repository.exchange_authorization_code("client-1", "code-1")
+
+ redis.getdel.assert_called_once_with("oauth_provider:client-1:authorization_code:code-1")
+ redis.set.assert_any_call(
+ f"oauth_provider:client-1:access_token:{access_token}",
+ "account-1",
+ ex=OAUTH_ACCESS_TOKEN_EXPIRES_IN,
+ )
+ redis.set.assert_any_call(
+ f"oauth_provider:client-1:refresh_token:{refresh_token}",
+ "account-1",
+ ex=OAUTH_REFRESH_TOKEN_EXPIRES_IN,
+ )
+
+
+def test_exchange_authorization_code_rejects_unknown_code() -> None:
+ redis = MagicMock(spec=RedisClientWrapper)
+ redis.getdel.return_value = None
+ repository = RedisOAuthServerTokenRepository(redis)
+
+ with pytest.raises(OAuthServerRequestError, match="invalid code"):
+ repository.exchange_authorization_code("client-1", "invalid")
+
+
+def test_refresh_access_token_issues_access_token_and_reuses_refresh_token() -> None:
+ redis = MagicMock(spec=RedisClientWrapper)
+ redis.get.return_value = "account-1"
+ repository = RedisOAuthServerTokenRepository(redis)
+ deterministic_uuid = uuid.UUID("00000000-0000-0000-0000-000000000301")
+
+ with patch("repositories.oauth_server_repository.uuid.uuid4", return_value=deterministic_uuid):
+ access_token, refresh_token = repository.refresh_access_token("client-1", "refresh-1")
+
+ assert access_token == str(deterministic_uuid)
+ assert refresh_token == "refresh-1"
+ redis.get.assert_called_once_with("oauth_provider:client-1:refresh_token:refresh-1")
+
+
+def test_refresh_access_token_rejects_unknown_token() -> None:
+ redis = MagicMock(spec=RedisClientWrapper)
+ redis.get.return_value = None
+ repository = RedisOAuthServerTokenRepository(redis)
+
+ with pytest.raises(OAuthServerRequestError, match="invalid refresh token"):
+ repository.refresh_access_token("client-1", "invalid")
+
+
+ ("stored_account_id", "expected"),
+ [
+ (b"account-1", "account-1"),
+ ("account-2", "account-2"),
+ (None, None),
+ ],
+)
+def test_resolve_account_id(stored_account_id: str | bytes | None, expected: str | None) -> None:
+ redis = MagicMock(spec=RedisClientWrapper)
+ redis.get.return_value = stored_account_id
+ repository = RedisOAuthServerTokenRepository(redis)
+
+ assert repository.resolve_account_id("client-1", "access-1") == expected
+ redis.get.assert_called_once_with("oauth_provider:client-1:access_token:access-1")
diff --git a/api/tests/unit_tests/services/test_data_source_oauth_service.py b/api/tests/unit_tests/services/test_data_source_oauth_service.py
new file mode 100644
index 00000000000..f2f0189da8a
--- /dev/null
+++ b/api/tests/unit_tests/services/test_data_source_oauth_service.py
@@ -0,0 +1,182 @@
+from unittest.mock import MagicMock, call
+
+import pytest
+
+from machinery.context import RequestContext
+from services.data_source_oauth_service import (
+ DataSourceOAuthConfigurationError,
+ DataSourceOAuthError,
+ DataSourceOAuthService,
+ InvalidDataSourceOAuthCodeError,
+)
+from services.entities.data_source_oauth_entities import (
+ DataSourceOAuthAuthorization,
+ DataSourceOAuthBindingRecord,
+ DataSourceOAuthCallback,
+)
+
+
+def request_context() -> RequestContext:
+ return RequestContext(
+ request_id="request-1",
+ trace_id="trace-1",
+ account_id="account-1",
+ active_workspace_id="workspace-1",
+ )
+
+
+def test_start_authorization_delegates_to_external_provider(request_context: RequestContext) -> None:
+ provider = MagicMock()
+ provider.get_authorization_url.return_value = "https://notion.example/authorize"
+ bindings = MagicMock()
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=provider,
+ bindings=bindings,
+ )
+
+ result = service.start_authorization(request_context)
+
+ assert result == "https://notion.example/authorize"
+ provider.get_authorization_url.assert_called_once_with()
+ bindings.upsert_authorization.assert_not_called()
+
+
+def test_start_authorization_persists_internal_authorization(request_context: RequestContext) -> None:
+ authorization = DataSourceOAuthAuthorization(access_token="secret", source_info={"pages": []})
+ provider = MagicMock()
+ provider.authorize_internal.return_value = authorization
+ bindings = MagicMock()
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=provider,
+ bindings=bindings,
+ is_internal_provider=True,
+ internal_access_token="secret",
+ )
+
+ result = service.start_authorization(request_context)
+
+ assert result == "internal"
+ provider.authorize_internal.assert_called_once_with("secret", "workspace-1")
+ bindings.upsert_authorization.assert_called_once_with(
+ workspace_id="workspace-1",
+ provider="notion",
+ authorization=authorization,
+ )
+
+
+def test_start_authorization_rejects_missing_internal_secret(request_context: RequestContext) -> None:
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=MagicMock(),
+ bindings=MagicMock(),
+ is_internal_provider=True,
+ )
+
+ with pytest.raises(DataSourceOAuthConfigurationError, match="Internal secret is not set"):
+ service.start_authorization(request_context)
+
+
+ ("code", "error", "expected_error"),
+ [
+ ("auth/code", None, None),
+ (None, "access denied", "access denied"),
+ (None, None, "Access denied"),
+ ],
+)
+def test_complete_callback_returns_framework_neutral_result(
+ code: str | None,
+ error: str | None,
+ expected_error: str | None,
+) -> None:
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=MagicMock(),
+ bindings=MagicMock(),
+ )
+
+ assert service.complete_callback(code=code, error=error) == DataSourceOAuthCallback(
+ provider="notion",
+ code=code,
+ error=expected_error,
+ )
+
+
+def test_bind_authorizes_before_persisting(request_context: RequestContext) -> None:
+ events = MagicMock()
+ authorization = DataSourceOAuthAuthorization(access_token="token", source_info={"pages": []})
+ provider = MagicMock()
+ provider.authorize.side_effect = lambda code: (events.authorize(code), authorization)[1]
+ bindings = MagicMock()
+ bindings.upsert_authorization.side_effect = lambda **kwargs: events.persist(**kwargs)
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=provider,
+ bindings=bindings,
+ )
+
+ service.bind(request_context, code="code-1")
+
+ assert events.mock_calls == [
+ call.authorize("code-1"),
+ call.persist(workspace_id="workspace-1", provider="notion", authorization=authorization),
+ ]
+
+
+def test_bind_rejects_empty_code(request_context: RequestContext) -> None:
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=MagicMock(),
+ bindings=MagicMock(),
+ )
+
+ with pytest.raises(InvalidDataSourceOAuthCodeError):
+ service.bind(request_context, code="")
+
+
+def test_sync_reads_before_provider_io_and_persists_afterward(request_context: RequestContext) -> None:
+ events = MagicMock()
+ binding = DataSourceOAuthBindingRecord(id="binding-1", access_token="token", source_info={"pages": []})
+ refreshed_source_info = {"pages": [{"page_id": "page-1"}]}
+ bindings = MagicMock()
+ bindings.get_enabled.side_effect = lambda **kwargs: (events.read(**kwargs), binding)[1]
+ bindings.update_source_info.side_effect = lambda **kwargs: (events.update(**kwargs), True)[1]
+ provider = MagicMock()
+ provider.refresh.side_effect = lambda *args: (events.refresh(*args), refreshed_source_info)[1]
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=provider,
+ bindings=bindings,
+ )
+
+ service.sync(request_context, binding_id="binding-1")
+
+ assert events.mock_calls == [
+ call.read(workspace_id="workspace-1", provider="notion", binding_id="binding-1"),
+ call.refresh("token", {"pages": []}),
+ call.update(
+ workspace_id="workspace-1",
+ provider="notion",
+ binding_id="binding-1",
+ source_info=refreshed_source_info,
+ ),
+ ]
+
+
+def test_sync_rejects_missing_binding_without_provider_io(request_context: RequestContext) -> None:
+ provider = MagicMock()
+ bindings = MagicMock()
+ bindings.get_enabled.return_value = None
+ service = DataSourceOAuthService(
+ provider_name="notion",
+ provider_gateway=provider,
+ bindings=bindings,
+ )
+
+ with pytest.raises(DataSourceOAuthError, match="Data source binding not found"):
+ service.sync(request_context, binding_id="missing")
+
+ provider.refresh.assert_not_called()
diff --git a/api/tests/unit_tests/services/test_notion_data_source_gateway.py b/api/tests/unit_tests/services/test_notion_data_source_gateway.py
new file mode 100644
index 00000000000..b3754eb799b
--- /dev/null
+++ b/api/tests/unit_tests/services/test_notion_data_source_gateway.py
@@ -0,0 +1,156 @@
+import json
+from unittest.mock import MagicMock
+
+import httpx
+import pytest
+
+from core.helper.ssrf_proxy import SSRFProxy
+from core.tools.errors import ToolSSRFError
+from services.data_source_oauth_service import DataSourceOAuthError
+from services.notion_data_source_gateway import NotionDataSourceGateway
+
+
+def _gateway(handler: httpx.MockTransport) -> tuple[NotionDataSourceGateway, httpx.Client]:
+ client = httpx.Client(transport=handler)
+ return (
+ NotionDataSourceGateway(
+ client_id="client-id",
+ client_secret="client-secret",
+ redirect_uri="https://api.example/oauth/data-source/callback/notion",
+ http_client=client,
+ ),
+ client,
+ )
+
+
+def test_get_authorization_url_encodes_configured_values() -> None:
+ gateway, client = _gateway(httpx.MockTransport(lambda _request: httpx.Response(500)))
+ try:
+ result = gateway.get_authorization_url()
+ finally:
+ client.close()
+
+ assert result == (
+ "https://api.notion.com/v1/oauth/authorize?client_id=client-id&response_type=code&"
+ "redirect_uri=https%3A%2F%2Fapi.example%2Foauth%2Fdata-source%2Fcallback%2Fnotion&owner=user"
+ )
+
+
+def test_authorize_exchanges_code_and_builds_source_info_without_persistence() -> None:
+ search_types: list[str] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ if request.url.path == "/v1/oauth/token":
+ assert request.headers["authorization"].startswith("Basic ")
+ return httpx.Response(
+ 200,
+ json={
+ "access_token": "notion-token",
+ "workspace_name": "Notion Workspace",
+ "workspace_icon": "https://example/icon.png",
+ "workspace_id": "notion-workspace",
+ },
+ )
+ if request.url.path == "/v1/search":
+ payload = json.loads(request.content)
+ search_types.append(payload["filter"]["value"])
+ return httpx.Response(200, json={"results": [], "has_more": False})
+ raise AssertionError(f"Unexpected Notion request: {request.url}")
+
+ gateway, client = _gateway(httpx.MockTransport(handler))
+ try:
+ authorization = gateway.authorize("authorization-code")
+ finally:
+ client.close()
+
+ assert authorization.access_token == "notion-token"
+ assert authorization.source_info == {
+ "workspace_name": "Notion Workspace",
+ "workspace_icon": "https://example/icon.png",
+ "workspace_id": "notion-workspace",
+ "pages": [],
+ "total": 0,
+ }
+ assert search_types == ["page", "database"]
+
+
+def test_refresh_preserves_workspace_metadata() -> None:
+ gateway, client = _gateway(
+ httpx.MockTransport(
+ lambda request: (
+ httpx.Response(200, json={"results": [], "has_more": False})
+ if request.url.path == "/v1/search"
+ else httpx.Response(404)
+ )
+ )
+ )
+ try:
+ source_info = gateway.refresh(
+ "notion-token",
+ {
+ "workspace_name": "Workspace",
+ "workspace_icon": None,
+ "workspace_id": "notion-workspace",
+ "pages": [
+ {
+ "page_id": "old",
+ "page_name": "Old page",
+ "page_icon": None,
+ "parent_id": "root",
+ "type": "page",
+ }
+ ],
+ "total": 1,
+ },
+ )
+ finally:
+ client.close()
+
+ assert source_info == {
+ "workspace_name": "Workspace",
+ "workspace_icon": None,
+ "workspace_id": "notion-workspace",
+ "pages": [],
+ "total": 0,
+ }
+
+
+def test_search_rejects_missing_cursor_when_more_results_are_declared() -> None:
+ requests = 0
+
+ def handler(_request: httpx.Request) -> httpx.Response:
+ nonlocal requests
+ requests += 1
+ return httpx.Response(200, json={"results": [], "has_more": True, "next_cursor": None})
+
+ gateway, client = _gateway(httpx.MockTransport(handler))
+ try:
+ with pytest.raises(DataSourceOAuthError, match="valid next_cursor"):
+ gateway.notion_page_search("notion-token")
+ finally:
+ client.close()
+
+ assert requests == 1
+
+
+def test_provider_http_failure_is_translated_to_application_boundary_error() -> None:
+ gateway, client = _gateway(httpx.MockTransport(lambda _request: httpx.Response(503, text="unavailable")))
+ try:
+ with pytest.raises(DataSourceOAuthError, match="unsuccessful response"):
+ gateway.authorize("authorization-code")
+ finally:
+ client.close()
+
+
+def test_ssrf_rejection_is_translated_to_application_boundary_error() -> None:
+ client = MagicMock(spec=SSRFProxy)
+ client.post.side_effect = ToolSSRFError("blocked")
+ gateway = NotionDataSourceGateway(
+ client_id="client-id",
+ client_secret="client-secret",
+ redirect_uri="https://api.example/oauth/data-source/callback/notion",
+ http_client=client,
+ )
+
+ with pytest.raises(DataSourceOAuthError, match="Notion request failed"):
+ gateway.authorize("authorization-code")
diff --git a/api/tests/unit_tests/services/test_oauth_server_service.py b/api/tests/unit_tests/services/test_oauth_server_service.py
index 5e11cfce0c6..4eb1b8e5eb2 100644
--- a/api/tests/unit_tests/services/test_oauth_server_service.py
+++ b/api/tests/unit_tests/services/test_oauth_server_service.py
@@ -1,190 +1,250 @@
-"""Unit tests for OAuthServerService with SQLite-backed database access."""
-
-from __future__ import annotations
-
-import uuid
-from collections.abc import Iterator
-from typing import cast
-from unittest.mock import patch
-from uuid import uuid4
+from unittest.mock import MagicMock
import pytest
-from flask import Flask
-from sqlalchemy import Engine
-from sqlalchemy.orm import Session
-from werkzeug.exceptions import BadRequest
-
-from models.account import Account
-from models.engine import db
-from models.model import OAuthProviderApp
-from services.oauth_server import (
- OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- OAUTH_ACCESS_TOKEN_REDIS_KEY,
- OAUTH_AUTHORIZATION_CODE_REDIS_KEY,
- OAUTH_REFRESH_TOKEN_EXPIRES_IN,
- OAUTH_REFRESH_TOKEN_REDIS_KEY,
- OAuthGrantType,
+
+from machinery.context import RequestContext
+from services.entities.oauth_server_entities import (
+ OAuthProviderAccount,
+ OAuthProviderAccountRecord,
+ OAuthProviderAccountStatus,
+ OAuthProviderAppRecord,
+)
+from services.oauth_server_service import (
+ OAuthServerClientNotFoundError,
+ OAuthServerRequestError,
OAuthServerService,
+ OAuthServerUnauthorizedError,
)
@pytest.fixture
-def oauth_db() -> Iterator[Session]:
- """Provide the production database extension with an isolated SQLite provider table."""
- app = Flask(__name__)
- app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
- db.init_app(app)
-
- with app.app_context():
- OAuthProviderApp.__table__.create(db.engine)
- with Session(db.engine, expire_on_commit=False) as session:
- yield session
-
-
-class TestOAuthServerServiceGetProviderApp:
- """Verify provider lookup against a real SQLAlchemy database."""
-
- def test_get_oauth_provider_app_returns_app_when_exists(self, oauth_db: Session) -> None:
- client_id = f"client-{uuid4()}"
- app = OAuthProviderApp(
- app_icon="icon.png",
- client_id=client_id,
- client_secret=str(uuid4()),
- app_label={"en-US": "Test OAuth App"},
- redirect_uris=["https://example.com/callback"],
- scope="read",
- )
- oauth_db.add(app)
- oauth_db.commit()
-
- result = OAuthServerService.get_oauth_provider_app(client_id)
-
- assert result is not None
- assert result.client_id == client_id
- assert result.id == app.id
-
- def test_get_oauth_provider_app_returns_none_when_not_exists(self, oauth_db: Session) -> None:
- result = OAuthServerService.get_oauth_provider_app(f"nonexistent-{uuid4()}")
-
- assert result is None
-
-
-class TestOAuthServerServiceTokenOperations:
- """Verify Redis-backed token signing and validation branches."""
-
- @pytest.fixture
- def mock_redis(self):
- with patch("services.oauth_server.redis_client") as mock:
- yield mock
+def provider_app() -> OAuthProviderAppRecord:
+ return OAuthProviderAppRecord(
+ app_icon="icon",
+ client_id="client-1",
+ client_secret="secret",
+ app_label={"en-US": "Test App"},
+ redirect_uris=("https://example.com/callback",),
+ scope="read",
+ auto_authorize=True,
+ )
- def test_sign_authorization_code_stores_and_returns_code(self, mock_redis) -> None:
- deterministic_uuid = uuid.UUID("00000000-0000-0000-0000-000000000111")
- with patch("services.oauth_server.uuid.uuid4", return_value=deterministic_uuid):
- code = OAuthServerService.sign_oauth_authorization_code("client-1", "user-1")
- assert code == str(deterministic_uuid)
- mock_redis.set.assert_called_once_with(
- OAUTH_AUTHORIZATION_CODE_REDIS_KEY.format(client_id="client-1", code=code),
- "user-1",
- ex=600,
- )
-
- def test_sign_access_token_raises_bad_request_for_invalid_code(self, mock_redis) -> None:
- mock_redis.get.return_value = None
-
- with pytest.raises(BadRequest, match="invalid code"):
- OAuthServerService.sign_oauth_access_token(
- grant_type=OAuthGrantType.AUTHORIZATION_CODE,
- code="bad-code",
- client_id="client-1",
- )
-
- def test_sign_access_token_issues_tokens_for_valid_code(self, mock_redis) -> None:
- token_uuids = [
- uuid.UUID("00000000-0000-0000-0000-000000000201"),
- uuid.UUID("00000000-0000-0000-0000-000000000202"),
- ]
- with patch("services.oauth_server.uuid.uuid4", side_effect=token_uuids):
- mock_redis.get.return_value = b"user-1"
- access_token, refresh_token = OAuthServerService.sign_oauth_access_token(
- grant_type=OAuthGrantType.AUTHORIZATION_CODE,
- code="code-1",
- client_id="client-1",
- )
-
- assert access_token == str(token_uuids[0])
- assert refresh_token == str(token_uuids[1])
- code_key = OAUTH_AUTHORIZATION_CODE_REDIS_KEY.format(client_id="client-1", code="code-1")
- mock_redis.delete.assert_called_once_with(code_key)
- mock_redis.set.assert_any_call(
- OAUTH_ACCESS_TOKEN_REDIS_KEY.format(client_id="client-1", token=access_token),
- b"user-1",
- ex=OAUTH_ACCESS_TOKEN_EXPIRES_IN,
- )
- mock_redis.set.assert_any_call(
- OAUTH_REFRESH_TOKEN_REDIS_KEY.format(client_id="client-1", token=refresh_token),
- b"user-1",
- ex=OAUTH_REFRESH_TOKEN_EXPIRES_IN,
- )
-
- def test_sign_access_token_raises_bad_request_for_invalid_refresh_token(self, mock_redis) -> None:
- mock_redis.get.return_value = None
-
- with pytest.raises(BadRequest, match="invalid refresh token"):
- OAuthServerService.sign_oauth_access_token(
- grant_type=OAuthGrantType.REFRESH_TOKEN,
- refresh_token="stale-token",
- client_id="client-1",
- )
-
- def test_sign_access_token_issues_new_token_for_valid_refresh(self, mock_redis) -> None:
- deterministic_uuid = uuid.UUID("00000000-0000-0000-0000-000000000301")
- with patch("services.oauth_server.uuid.uuid4", return_value=deterministic_uuid):
- mock_redis.get.return_value = b"user-1"
- access_token, returned_refresh = OAuthServerService.sign_oauth_access_token(
- grant_type=OAuthGrantType.REFRESH_TOKEN,
- refresh_token="refresh-1",
- client_id="client-1",
- )
-
- assert access_token == str(deterministic_uuid)
- assert returned_refresh == "refresh-1"
-
- def test_sign_access_token_returns_none_for_unknown_grant_type(self, mock_redis) -> None:
- grant_type = cast(OAuthGrantType, "invalid-grant-type")
-
- result = OAuthServerService.sign_oauth_access_token(grant_type=grant_type, client_id="client-1")
-
- assert result is None
-
- def test_sign_refresh_token_stores_with_expected_expiry(self, mock_redis) -> None:
- deterministic_uuid = uuid.UUID("00000000-0000-0000-0000-000000000401")
- with patch("services.oauth_server.uuid.uuid4", return_value=deterministic_uuid):
- refresh_token = OAuthServerService._sign_oauth_refresh_token("client-2", "user-2")
-
- assert refresh_token == str(deterministic_uuid)
- mock_redis.set.assert_called_once_with(
- OAUTH_REFRESH_TOKEN_REDIS_KEY.format(client_id="client-2", token=refresh_token),
- "user-2",
- ex=OAUTH_REFRESH_TOKEN_EXPIRES_IN,
+def service(provider_app: OAuthProviderAppRecord) -> tuple[OAuthServerService, MagicMock, MagicMock]:
+ repository = MagicMock()
+ repository.get_provider_app_by_client_id.return_value = provider_app
+ tokens = MagicMock()
+ return (
+ OAuthServerService(
+ repository=repository,
+ tokens=tokens,
+ access_token_expires_in=43200,
+ ),
+ repository,
+ tokens,
+ )
+
+
+def test_get_provider_validates_redirect_and_hides_credentials(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, repository, _tokens = service
+
+ result = oauth_server.get_provider(
+ client_id="client-1",
+ redirect_uri="https://example.com/callback",
+ )
+
+ assert result.app_icon == "icon"
+ assert result.app_label == {"en-US": "Test App"}
+ assert result.scope == "read"
+ assert result.auto_authorize is True
+ repository.get_provider_app_by_client_id.assert_called_once_with("client-1")
+
+
+def test_get_provider_rejects_invalid_redirect(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, _provider_apps, _tokens = service
+
+ with pytest.raises(OAuthServerRequestError, match="redirect_uri is invalid"):
+ oauth_server.get_provider(client_id="client-1", redirect_uri="https://invalid.example/callback")
+
+
+def test_missing_client_is_reported_by_application_boundary(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, repository, _tokens = service
+ repository.get_provider_app_by_client_id.return_value = None
+
+ with pytest.raises(OAuthServerClientNotFoundError, match="client_id is invalid"):
+ oauth_server.authorize(
+ RequestContext("request-1", None, "account-1", "workspace-1"),
+ client_id="missing",
)
- def test_validate_access_token_returns_none_when_not_found(self, mock_redis, sqlite_engine: Engine) -> None:
- mock_redis.get.return_value = None
- with Session(sqlite_engine) as session:
- result = OAuthServerService.validate_oauth_access_token("client-1", "missing-token", session)
-
- assert result is None
-
- def test_validate_access_token_loads_user_when_exists(self, mock_redis, sqlite_engine: Engine) -> None:
- mock_redis.get.return_value = b"user-88"
- expected_user = Account(name="Test User", email="[email protected]")
- expected_user.id = "user-88"
-
- with Session(sqlite_engine) as session:
- with patch("services.oauth_server.AccountService.load_user", return_value=expected_user) as mock_load:
- result = OAuthServerService.validate_oauth_access_token("client-1", "access-token", session)
- mock_load.assert_called_once_with("user-88", session)
-
- assert result is expected_user
+def test_authorize_uses_stable_request_context(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, _provider_apps, tokens = service
+ tokens.issue_authorization_code.return_value = "code-1"
+ context = RequestContext("request-1", "trace-1", "account-1", "workspace-1")
+
+ result = oauth_server.authorize(context, client_id="client-1")
+
+ assert result.code == "code-1"
+ tokens.issue_authorization_code.assert_called_once_with("client-1", "account-1")
+
+
+def test_exchange_authorization_code_validates_client_and_returns_token_contract(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, _provider_apps, tokens = service
+ tokens.exchange_authorization_code.return_value = ("access-1", "refresh-1")
+
+ result = oauth_server.exchange_token(
+ client_id="client-1",
+ grant_type="authorization_code",
+ code="code-1",
+ client_secret="secret",
+ redirect_uri="https://example.com/callback",
+ refresh_token=None,
+ )
+
+ assert result.access_token == "access-1"
+ assert result.refresh_token == "refresh-1"
+ assert result.token_type == "Bearer"
+ assert result.expires_in == 43200
+ tokens.exchange_authorization_code.assert_called_once_with("client-1", "code-1")
+
+
+ ("overrides", "message"),
+ [
+ ({"grant_type": "invalid"}, "invalid grant_type"),
+ ({"code": None}, "code is required"),
+ ({"client_secret": "invalid"}, "client_secret is invalid"),
+ ({"redirect_uri": "https://invalid.example/callback"}, "redirect_uri is invalid"),
+ ],
+)
+def test_exchange_authorization_code_rejects_invalid_requests(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+ overrides: dict[str, str | None],
+ message: str,
+) -> None:
+ oauth_server, _provider_apps, tokens = service
+ values = {
+ "client_id": "client-1",
+ "grant_type": "authorization_code",
+ "code": "code-1",
+ "client_secret": "secret",
+ "redirect_uri": "https://example.com/callback",
+ "refresh_token": None,
+ **overrides,
+ }
+
+ with pytest.raises(OAuthServerRequestError, match=message):
+ oauth_server.exchange_token(**values) # type: ignore[arg-type]
+
+ tokens.exchange_authorization_code.assert_not_called()
+
+
+def test_refresh_token_grant_delegates_to_repository(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, _provider_apps, tokens = service
+ tokens.refresh_access_token.return_value = ("access-2", "refresh-1")
+
+ result = oauth_server.exchange_token(
+ client_id="client-1",
+ grant_type="refresh_token",
+ code=None,
+ client_secret=None,
+ redirect_uri=None,
+ refresh_token="refresh-1",
+ )
+
+ assert result.access_token == "access-2"
+ assert result.refresh_token == "refresh-1"
+ tokens.refresh_access_token.assert_called_once_with("client-1", "refresh-1")
+
+
+def test_get_account_returns_active_account_without_requiring_workspace_membership(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, repository, tokens = service
+ tokens.resolve_account_id.return_value = "account-1"
+ repository.get_account_by_id.return_value = OAuthProviderAccountRecord(
+ id="account-1",
+ name="Test User",
+ avatar=None,
+ interface_language="en-US",
+ timezone="UTC",
+ status=OAuthProviderAccountStatus.ACTIVE,
+ )
+
+ assert oauth_server.get_account(client_id="client-1", access_token="access-1") == OAuthProviderAccount(
+ id="account-1",
+ name="Test User",
+ avatar=None,
+ interface_language="en-US",
+ timezone="UTC",
+ )
+ tokens.resolve_account_id.assert_called_once_with("client-1", "access-1")
+ repository.get_account_by_id.assert_called_once_with("account-1")
+
+
+def test_get_account_rejects_invalid_token(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, _provider_apps, tokens = service
+ tokens.resolve_account_id.return_value = None
+
+ with pytest.raises(OAuthServerUnauthorizedError, match="access_token or client_id is invalid"):
+ oauth_server.get_account(client_id="client-1", access_token="invalid")
+
+
+def test_get_account_validates_client_before_missing_token(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+) -> None:
+ oauth_server, repository, tokens = service
+ repository.get_provider_app_by_client_id.return_value = None
+
+ with pytest.raises(OAuthServerClientNotFoundError, match="client_id is invalid"):
+ oauth_server.get_account(client_id="missing", access_token=None)
+
+ tokens.resolve_account_id.assert_not_called()
+
+
+ ("status", "message"),
+ [
+ (OAuthProviderAccountStatus.PENDING, "Account is not active"),
+ (OAuthProviderAccountStatus.UNINITIALIZED, "Account is not active"),
+ (OAuthProviderAccountStatus.BANNED, "Account is banned"),
+ (OAuthProviderAccountStatus.CLOSED, "Account is not active"),
+ ],
+)
+def test_get_account_rejects_inactive_account(
+ service: tuple[OAuthServerService, MagicMock, MagicMock],
+ status: OAuthProviderAccountStatus,
+ message: str,
+) -> None:
+ oauth_server, repository, tokens = service
+ tokens.resolve_account_id.return_value = "account-1"
+ repository.get_account_by_id.return_value = OAuthProviderAccountRecord(
+ id="account-1",
+ name="Test User",
+ avatar=None,
+ interface_language=None,
+ timezone=None,
+ status=status,
+ )
+
+ with pytest.raises(OAuthServerUnauthorizedError, match=message):
+ oauth_server.get_account(client_id="client-1", access_token="access-1")
diff --git a/packages/contracts/generated/api/console/oauth/types.gen.ts b/packages/contracts/generated/api/console/oauth/types.gen.ts
index 2120c2a3283..d42b16324b5 100644
--- a/packages/contracts/generated/api/console/oauth/types.gen.ts
+++ b/packages/contracts/generated/api/console/oauth/types.gen.ts
@@ -42,9 +42,9 @@ export type OAuthProviderAccountResponse = {
avatar?: string | null
email: string
id: string
- interface_language: string
+ interface_language: string | null
name: string
- timezone: string
+ timezone: string | null
}
export type OAuthProviderAuthorizeResponse = {
diff --git a/packages/contracts/generated/api/console/oauth/zod.gen.ts b/packages/contracts/generated/api/console/oauth/zod.gen.ts
index d96544bcdc0..23bde3c453e 100644
--- a/packages/contracts/generated/api/console/oauth/zod.gen.ts
+++ b/packages/contracts/generated/api/console/oauth/zod.gen.ts
@@ -62,9 +62,9 @@ export const zOAuthProviderAccountResponse = z.object({
avatar: z.string().nullish(),
email: z.string(),
id: z.string(),
- interface_language: z.string(),
+ interface_language: z.string().nullable(),
name: z.string(),
- timezone: z.string(),
+ timezone: z.string().nullable(),
})
/**