-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathCors.java
More file actions
54 lines (46 loc) · 1.66 KB
/
Cors.java
File metadata and controls
54 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package io.openruntimes.java.src;
import io.openruntimes.java.RuntimeContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
public class Cors {
/**
* Returns true if the origin is allowed to make requests to this endpoint
*
* Parameters:
* context: Context object
*
* Returns:
* (boolean): True if the origin is allowed, False otherwise
*/
public static boolean isOriginPermitted(RuntimeContext context) {
String allowedOrigins = System.getenv("ALLOWED_ORIGINS");
if (allowedOrigins == null || allowedOrigins.equals("*")) {
return true;
}
List<String> allowedOriginsList = Arrays.asList(allowedOrigins.split(","));
String originHeader = context.getReq().getHeaders().get("origin");
return originHeader != null && allowedOriginsList.contains(originHeader);
}
/**
* Returns the CORS headers for the request
*
* Parameters:
* context: Context object
*
* Returns:
* (Map<String, String>): CORS headers
*/
public static Map<String, String> getCorsHeaders(RuntimeContext context) {
if (!context.getReq().getHeaders().containsKey("origin")) {
return new HashMap<>();
}
String allowedOrigins = System.getenv("ALLOWED_ORIGINS");
if (allowedOrigins == null || allowedOrigins.equals("*")) {
return new HashMap<>(Map.of("Access-Control-Allow-Origin", "*"));
}
String originHeader = context.getReq().getHeaders().get("origin");
return new HashMap<>(Map.of("Access-Control-Allow-Origin", originHeader));
}
}