1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.opencastproject.urlsigning.utils;
22
23 import org.opencastproject.urlsigning.common.Policy;
24
25 import org.apache.commons.codec.binary.Base64;
26 import org.joda.time.DateTime;
27 import org.joda.time.DateTimeZone;
28 import org.json.simple.JSONObject;
29 import org.json.simple.parser.JSONParser;
30 import org.json.simple.parser.ParseException;
31
32 import java.nio.charset.StandardCharsets;
33 import java.util.Map;
34 import java.util.TreeMap;
35
36
37
38
39 public final class PolicyUtils {
40
41 private static final String CONDITION_KEY = "Condition";
42
43 private static final String DATE_GREATER_THAN_KEY = "DateGreaterThan";
44
45 private static final String DATE_LESS_THAN_KEY = "DateLessThan";
46
47 private static final String IP_ADDRESS_KEY = "IpAddress";
48
49 private static final String RESOURCE_KEY = "Resource";
50
51 private static final String STATEMENT_KEY = "Statement";
52
53 private PolicyUtils() {
54
55 }
56
57
58
59
60
61
62
63
64 public static String base64Encode(String value) {
65 return Base64.encodeBase64URLSafeString(value.getBytes());
66 }
67
68
69
70
71
72
73
74
75 public static String base64Decode(String value) {
76 return new String(Base64.decodeBase64(value), StandardCharsets.UTF_8);
77 }
78
79
80
81
82
83
84
85
86 public static Policy fromJson(String policyJson) {
87 JSONObject jsonPolicy = null;
88 JSONParser jsonParser = new JSONParser();
89 try {
90 jsonPolicy = (JSONObject) jsonParser.parse(policyJson);
91 } catch (ParseException e) {
92 e.printStackTrace();
93 }
94 JSONObject statement = (JSONObject) jsonPolicy.get(STATEMENT_KEY);
95 String resource = statement.get(RESOURCE_KEY).toString();
96 JSONObject condition = (JSONObject) statement.get(CONDITION_KEY);
97
98 final String lessThanString = condition.get(DATE_LESS_THAN_KEY).toString();
99 final DateTime dateLessThan = new DateTime(Long.parseLong(lessThanString), DateTimeZone.UTC);
100
101 final DateTime dateGreaterThan;
102 Object greaterThanString = condition.get(DATE_GREATER_THAN_KEY);
103 if (greaterThanString != null) {
104 dateGreaterThan = new DateTime(Long.parseLong(greaterThanString.toString()), DateTimeZone.UTC);
105 } else {
106 dateGreaterThan = null;
107 }
108
109 return Policy.mkPolicyValidFromWithIP(resource, dateLessThan, dateGreaterThan,
110 (String) condition.get(IP_ADDRESS_KEY));
111 }
112
113
114
115
116
117
118
119
120 @SuppressWarnings("unchecked")
121 public static JSONObject toJson(Policy policy) {
122 JSONObject policyJSON = new JSONObject();
123
124 Map<String, Object> conditions = new TreeMap<String, Object>();
125 conditions.put(DATE_LESS_THAN_KEY, new Long(policy.getValidUntil().getMillis()));
126 if (policy.getValidFrom().isPresent()) {
127 conditions.put(DATE_GREATER_THAN_KEY, new Long(policy.getValidFrom().get().getMillis()));
128 }
129 if (policy.getClientIpAddress().isPresent()) {
130 conditions.put(IP_ADDRESS_KEY, policy.getClientIpAddress().get().getHostAddress());
131 }
132 JSONObject conditionsJSON = new JSONObject();
133 conditionsJSON.putAll(conditions);
134
135 JSONObject statement = new JSONObject();
136 statement.put(RESOURCE_KEY, policy.getResource());
137 statement.put(CONDITION_KEY, conditions);
138
139 policyJSON.put(STATEMENT_KEY, statement);
140
141 return policyJSON;
142 }
143
144
145
146
147
148
149
150
151 public static Policy fromBase64EncodedPolicy(String encodedPolicy) {
152 String decodedPolicyString = base64Decode(encodedPolicy);
153 return fromJson(decodedPolicyString);
154 }
155
156
157
158
159
160
161
162
163 public static String toBase64EncodedPolicy(Policy policy) {
164 return base64Encode(PolicyUtils.toJson(policy).toJSONString());
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public static String getPolicySignature(Policy policy, String encryptionKey) throws Exception {
180 return SHA256Util.digest(PolicyUtils.toJson(policy).toJSONString(), encryptionKey);
181 }
182 }