1
1
---
2
2
id : cors
3
- title : Setting up cross-origin resource sharing (CORS)
3
+ title : Configure cross-origin resource sharing (CORS)
4
4
---
5
5
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 ) .
8
8
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:
10
12
11
13
``` yaml
12
14
serve :
@@ -15,38 +17,43 @@ serve:
15
17
enabled : true
16
18
allowed_origins :
17
19
- 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
29
21
public :
30
22
cors :
31
23
enabled : true
32
24
allowed_origins :
33
25
- https://example.com
34
26
- 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
45
27
` ` `
46
28
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:
50
57
51
58
``` json
52
59
{
@@ -55,5 +62,81 @@ be consumed in a CORS-fashion. Some endpoints (`/oauth2/token`, `/userinfo`, `/o
55
62
}
56
63
```
57
64
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
59
66
` 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