1 /*
2 * Licensed to The Apereo Foundation under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 *
7 * The Apereo Foundation licenses this file to you under the Educational
8 * Community License, Version 2.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of the License
10 * at:
11 *
12 * http://opensource.org/licenses/ecl2.txt
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 *
20 */
21 package org.opencastproject.urlsigning.common;
22
23 import static java.util.Objects.requireNonNull;
24
25 import com.google.common.base.Optional;
26 import com.google.common.net.InetAddresses;
27
28 import org.apache.commons.lang3.StringUtils;
29 import org.joda.time.DateTime;
30
31 import java.net.InetAddress;
32
33 /**
34 * Represents a policy for a signed resource that looks like
35 *
36 */
37 public final class Policy {
38
39 /** The base URL for the resource being requested. */
40 private final String baseUrl;
41
42 /** The date and time when the resource expires. */
43 private final DateTime validUntil;
44
45 /** The date and time when the resource will become available. */
46 private final Optional<DateTime> validFrom;
47
48 /** An optional client IP address that made the original request. */
49 private final Optional<InetAddress> clientIpAddress;
50
51 /** The required strategy to convert a base url to the resource url. */
52 private ResourceStrategy resourceStrategy = new BasicResourceStrategyImpl();
53
54 /**
55 * Create a new Policy.
56 *
57 * @param baseUrl
58 * The base url that points to the resource that is being made available.
59 * @param validUntil
60 * The date and time the resource is available until.
61 * @param validFrom
62 * An optional date and time the resource will first become available.
63 * @param clientIpAddress
64 * An optional client IP address to restrict the viewing of the resource to.
65 * @param resourceStrategy
66 * The strategy for getting the resource from the policy.
67 */
68 private Policy(String baseUrl, DateTime validUntil, DateTime validFrom, String clientIpAddress) {
69 requireNonNull(baseUrl);
70 requireNonNull(validUntil);
71
72 this.baseUrl = baseUrl;
73 this.validUntil = validUntil;
74 this.validFrom = Optional.fromNullable(validFrom);
75 if (StringUtils.isNotBlank(clientIpAddress)) {
76 this.clientIpAddress = Optional.of(InetAddresses.forString(clientIpAddress));
77 } else {
78 this.clientIpAddress = Optional.absent();
79 }
80 }
81
82 /**
83 * Create a {@link Policy} with only the required properties.
84 *
85 * @param baseUrl
86 * The url to the resource that will be signed.
87 * @param validUntil
88 * The date and time the resource will be available until
89 * @return A new {@link Policy} with the parameters set.
90 */
91 public static Policy mkSimplePolicy(String baseUrl, DateTime validUntil) {
92 return new Policy(baseUrl, validUntil, null, null);
93 }
94
95 /**
96 * Create a {@link Policy} with a date and time the resource will become available.
97 *
98 * @param baseUrl
99 * The url to the resource being signed.
100 * @param validUntil
101 * The date and time the resource is available until.
102 * @param validFrom
103 * The date and time the resource will become available.
104 * @return A new {@link Policy} for limiting access to the resource.
105 */
106 public static Policy mkPolicyValidFrom(String baseUrl, DateTime validUntil, DateTime validFrom) {
107 return new Policy(baseUrl, validUntil, validFrom, null);
108 }
109
110 /**
111 * Create a {@link Policy} with the only ip address that will be allowed to view the resource.
112 *
113 * @param baseUrl
114 * The url to the resource being signed.
115 * @param validUntil
116 * The date the resource will be available until.
117 * @param ipAddress
118 * The ip of the client that will be allowed to view the resource.
119 * @return A new {@link Policy} for limiting access to the resource.
120 */
121 public static Policy mkPolicyValidWithIP(String baseUrl, DateTime validUntil, String ipAddress) {
122 return new Policy(baseUrl, validUntil, null, ipAddress);
123 }
124
125 /**
126 * Create a {@link Policy} with both a date and time the resource will become available and a client ip address to
127 * restrict it to.
128 *
129 * @param baseUrl
130 * The url to the resource that is being signed.
131 * @param validUntil
132 * The date and time the resource will be available until.
133 * @param validFrom
134 * The date and time the resource will become available.
135 * @param ipAddress
136 * The ip of the client that will be allowed to view the resource.
137 * @return A new {@link Policy} for limiting access to the resource.
138 */
139 public static Policy mkPolicyValidFromWithIP(String baseUrl, DateTime validUntil, DateTime validFrom,
140 String ipAddress) {
141 return new Policy(baseUrl, validUntil, validFrom, ipAddress);
142 }
143
144 /**
145 * @return Get the url to the resource that is being signed with this policy.
146 */
147 public String getBaseUrl() {
148 return baseUrl;
149 }
150
151 /**
152 * @return Get the date this resource is valid until.
153 */
154 public DateTime getValidUntil() {
155 return validUntil;
156 }
157
158 /**
159 * @return Get the url for the resource in this {@link Policy}.
160 */
161 public String getResource() {
162 return resourceStrategy.getResource(baseUrl);
163 }
164
165 /**
166 * Set a new {@link ResourceStrategy} to transform the base url to a resource url.
167 *
168 * @param resourceStrategy
169 * The resource strategy to apply to transform the base url.
170 */
171 public void setResourceStrategy(ResourceStrategy resourceStrategy) {
172 this.resourceStrategy = resourceStrategy;
173 }
174
175 /**
176 * @return Get the optional ip address of the client that this resource will be restricted to.
177 */
178 public Optional<InetAddress> getClientIpAddress() {
179 return clientIpAddress;
180 }
181
182 /**
183 * @return Get the optional date and time this resource will become available.
184 */
185 public Optional<DateTime> getValidFrom() {
186 return validFrom;
187 }
188
189 }