Skip to content

Commit 4955882

Browse files
aeneasrvinckr
andauthored
docs: clarify CORS (#2283)
* docs: clarify CORS Closes ory/hydra#3795 * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * Update docs/hydra/guides/cors.mdx Co-authored-by: Vincent <[email protected]> * chore: synchronize workspaces * Modify CORS settings to use wildcard for origins Updated CORS configuration to allow all origins for public OAuth2 client registration. --------- Co-authored-by: Vincent <[email protected]>
1 parent 199486a commit 4955882

File tree

1 file changed

+112
-29
lines changed

1 file changed

+112
-29
lines changed

docs/hydra/guides/cors.mdx

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
22
id: cors
3-
title: Setting up cross-origin resource sharing (CORS)
3+
title: Configure cross-origin resource sharing (CORS)
44
---
55

6-
Both Ory Hydra's Admin and Public endpoints support CORS. For detailed information, head over to the exemplary
7-
[config file](https://github.com/ory/hydra/blob/master/.schema/config.schema.json).
6+
Ory services support cross-origin resource sharing (CORS). For the full schema, see the
7+
[configuration file](https://github.com/ory/hydra/blob/master/.schema/config.schema.json).
88

9-
For CORS to work properly, we encourage to set the following values:
9+
## Configure CORS in Ory Kratos
10+
11+
Enable CORS for specific origins in your configuration file:
1012

1113
```yaml
1214
serve:
@@ -15,38 +17,43 @@ serve:
1517
enabled: true
1618
allowed_origins:
1719
- https://example.com
18-
- https://*.example.com
19-
allowed_methods:
20-
- POST
21-
- GET
22-
- PUT
23-
- PATCH
24-
- DELETE
25-
allowed_headers:
26-
- Authorization
27-
exposed_headers:
28-
- Content-Type
20+
- https://*.example.com # Wildcards are supported
2921
public:
3022
cors:
3123
enabled: true
3224
allowed_origins:
3325
- https://example.com
3426
- https://*.example.com
35-
allowed_methods:
36-
- POST
37-
- GET
38-
- PUT
39-
- PATCH
40-
- DELETE
41-
allowed_headers:
42-
- Authorization
43-
exposed_headers:
44-
- Content-Type
4527
```
4628
47-
Keep in mind that the OAuth 2.0 Authorization Endpoint (`/oauth2/auth`) doesn't expose CORS by design. This endpoint should never
48-
be consumed in a CORS-fashion. Some endpoints (`/oauth2/token`, `/userinfo`, `/oauth2/revoke`) also include URLs listed in field
49-
`allowed_cors_origins` of the OAuth 2.0 Client that is making the request. For example, OAuth 2.0 Client
29+
## Configure CORS in Ory Hydra
30+
31+
We recommend the following base configuration:
32+
33+
```yaml
34+
serve:
35+
admin:
36+
cors:
37+
enabled: true
38+
allowed_origins:
39+
- https://example.com
40+
- https://*.example.com
41+
public:
42+
cors:
43+
enabled: true
44+
allowed_origins:
45+
- * # Use wildcard for using Ory Hydra in 3rd party scenarios (public OAuth2 client registration), otherwise fixed domains.
46+
```
47+
48+
### OAuth 2.0 authorization endpoint
49+
50+
The authorization endpoint (`/oauth2/auth`) never supports CORS. Browsers call this endpoint directly, not through AJAX, so CORS
51+
is unnecessary and unsafe.
52+
53+
### OAuth 2.0 token endpoint
54+
55+
The token, userinfo, and revocation endpoints (`/oauth2/token`, `/userinfo`, `/oauth2/revoke`) allow requests from origins defined
56+
in the OAuth 2.0 client’s `allowed_cors_origins` field. Example:
5057

5158
```json
5259
{
@@ -55,5 +62,81 @@ be consumed in a CORS-fashion. Some endpoints (`/oauth2/token`, `/userinfo`, `/o
5562
}
5663
```
5764

58-
is allowed to make CORS request to `/oauth2/token` from origin `https://foo-bar.com/` even if that origin isn't listed in
65+
This client can make CORS requests to `/oauth2/token` from `https://foo-bar.com/`, even if that origin isn't listed in
5966
`public.cors.allowed_origins`.
67+
68+
::: note
69+
70+
For preflight (OPTIONS) requests, you must also configure the origin in the global CORS settings. OPTIONS requests don’t include
71+
authorization headers, so Hydra can't resolve which OAuth 2.0 client is making the request.
72+
73+
:::
74+
75+
## Configure CORS in Ory Keto
76+
77+
```yaml
78+
serve:
79+
read:
80+
cors:
81+
enabled: true
82+
allowed_origins:
83+
- https://example.com
84+
- https://*.example.com
85+
write:
86+
cors:
87+
enabled: true
88+
allowed_origins:
89+
- https://example.com
90+
- https://*.example.com
91+
metrics:
92+
cors:
93+
enabled: true
94+
allowed_origins:
95+
- https://example.com
96+
- https://*.example.com
97+
```
98+
99+
## Configure CORS in Ory Oathkeeper
100+
101+
```yaml
102+
serve:
103+
proxy:
104+
cors:
105+
enabled: true
106+
allowed_origins:
107+
- https://example.com
108+
- https://*.example.com
109+
api:
110+
cors:
111+
enabled: true
112+
allowed_origins:
113+
- https://example.com
114+
- https://*.example.com
115+
```
116+
117+
## Advanced configuration
118+
119+
You can customize allowed methods, headers, and other CORS behavior:
120+
121+
```yaml
122+
cors:
123+
enabled: true
124+
allowed_origins:
125+
- https://example.com
126+
127+
allowed_methods:
128+
- GET
129+
- POST
130+
- PUT
131+
- PATCH
132+
- DELETE
133+
- OPTIONS
134+
allowed_headers:
135+
- Content-Type
136+
exposed_headers:
137+
- Content-Type
138+
- Date
139+
- Vary
140+
allow_credentials: true
141+
debug: true
142+
```

0 commit comments

Comments
 (0)