Skip to content

Commit 5b4da38

Browse files
committed
add cors middleware
1 parent 698da07 commit 5b4da38

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

exordos_core/cmd/user_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from exordos_core.common import constants as c
3232
from exordos_core.common import log as infra_log
3333
from exordos_core.common import utils
34+
from exordos_core.common.api.middlewares import cors as cors_mw
3435
from exordos_core.user_api.api import app
3536
from exordos_core.user_api.iam import drivers as iam_drivers
3637

@@ -75,6 +76,7 @@
7576
CONF.register_cli_opts(iam_cli_opts, DOMAIN_IAM)
7677
ra_config_opts.register_posgresql_db_opts(CONF)
7778
sdk_opts.register_event_opts(CONF)
79+
cors_mw.register_cors_opts(CONF)
7880

7981

8082
def main():
@@ -119,6 +121,7 @@ def main():
119121
wsgi_app=app.build_wsgi_application(
120122
context_storage=context_storage,
121123
iam_engine_driver=iam_engine_driver,
124+
allowed_origins=CONF["cors"].allowed_origins,
122125
),
123126
host=CONF[DOMAIN].bind_host,
124127
port=CONF[DOMAIN].bind_port,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2026 Genesis Corporation.
2+
#
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from oslo_config import cfg
18+
from webob import dec
19+
20+
from restalchemy.api import middlewares
21+
22+
ALLOWED_ORIGINS_OPT = cfg.ListOpt(
23+
"allowed_origins",
24+
default=["*"],
25+
help="List of allowed CORS origins",
26+
)
27+
28+
CORS_OPT_GROUP = cfg.OptGroup("cors")
29+
CORS_OPTS = [ALLOWED_ORIGINS_OPT]
30+
BASE_RESPONSE_HEADERS = {
31+
"Access-Control-Allow-Credentials": "true",
32+
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
33+
"Access-Control-Allow-Headers": (
34+
"Authorization, Content-Type, X-OTP-Token, X-Requested-With, "
35+
"Accept, Origin"
36+
),
37+
"Access-Control-Max-Age": "3600",
38+
}
39+
40+
41+
def register_cors_opts(conf):
42+
conf.register_group(CORS_OPT_GROUP)
43+
conf.register_opts(CORS_OPTS, group=CORS_OPT_GROUP)
44+
45+
46+
class CORSMiddleware(middlewares.Middleware):
47+
48+
def __init__(self, application, allowed_origins=None):
49+
super().__init__(application)
50+
self.allowed_origins = allowed_origins or []
51+
52+
@dec.wsgify
53+
def __call__(self, req):
54+
origin = req.headers.get("Origin", "")
55+
56+
if req.method == "OPTIONS" and self._is_origin_allowed(origin):
57+
return req.ResponseClass(
58+
status=200,
59+
headers=self._cors_headers(origin),
60+
)
61+
62+
response = req.get_response(self.application)
63+
64+
if self._is_origin_allowed(origin):
65+
for key, value in self._cors_headers(origin).items():
66+
response.headers.add(key, value)
67+
68+
return response
69+
70+
def _is_origin_allowed(self, origin):
71+
if not origin:
72+
return False
73+
return origin in self.allowed_origins or "*" in self.allowed_origins
74+
75+
@staticmethod
76+
def _cors_headers(origin):
77+
headers = BASE_RESPONSE_HEADERS.copy()
78+
headers["Access-Control-Allow-Origin"] = origin
79+
return headers

exordos_core/user_api/api/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from exordos_core import version
2626
from exordos_core.common import contexts as common_contexts
27+
from exordos_core.common.api.middlewares import cors as cors_mw
2728
from exordos_core.common.api.middlewares import errors as errors_mw
2829
from exordos_core.user_api.api import middlewares as user_api_mw
2930
from exordos_core.user_api.api import routes as app_routes
@@ -60,13 +61,17 @@ def get_openapi_engine():
6061
return openapi_engine
6162

6263

63-
def build_wsgi_application(context_storage, iam_engine_driver):
64+
def build_wsgi_application(context_storage, iam_engine_driver, allowed_origins=None):
6465
return middlewares.attach_middlewares(
6566
applications.OpenApiApplication(
6667
route_class=get_api_application(),
6768
openapi_engine=get_openapi_engine(),
6869
),
6970
[
71+
middlewares.configure_middleware(
72+
cors_mw.CORSMiddleware,
73+
allowed_origins=allowed_origins or [],
74+
),
7075
user_api_mw.SecurityRulesMiddleware,
7176
middlewares.configure_middleware(
7277
iam_mw.GenesisCoreAuthMiddleware,

0 commit comments

Comments
 (0)