Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit fb7464e

Browse files
author
Mario
committed
Merge branch 'master' of github.com:stormpath/stormpath-sdk-java into 1158_handlers_fix_using_spring_security_in_front_stormpath
2 parents 085f4aa + 5db9794 commit fb7464e

File tree

84 files changed

+5095
-339
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+5095
-339
lines changed

api/src/main/java/com/stormpath/sdk/lang/Instants.java

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.stormpath.sdk.lang;
1717

18+
import java.text.SimpleDateFormat;
1819
import java.util.Calendar;
1920
import java.util.Date;
2021
import java.util.GregorianCalendar;
@@ -29,10 +30,34 @@ public class Instants {
2930

3031
public static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone("UTC");
3132

33+
/**
34+
* @since 1.3.0
35+
*/
36+
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
37+
@Override
38+
protected SimpleDateFormat initialValue() {
39+
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
40+
df.setTimeZone(UTC_TIMEZONE);
41+
return df;
42+
}
43+
};
44+
45+
/**
46+
* Returns the UTC-normalized ISO 8601 representation of the specified date instance.
47+
*
48+
* @param date the date to format.
49+
* @return the UTC-normalized ISO 8601 representation of the specified date instance.
50+
* @since 1.3.0
51+
*/
52+
public static String toUtcIso8601(Date date) {
53+
return DATE_FORMAT.get().format(date);
54+
}
55+
3256
/**
3357
* Converts a given time from UTC (Coordinated Universal Time) to the corresponding time in the specified {@link TimeZone}
58+
*
3459
* @param time the UTC instant to convert, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970
35-
* @param to the target {@link TimeZone} for the conversion
60+
* @param to the target {@link TimeZone} for the conversion
3661
* @return the long representation of the instant converted to the specified {@link TimeZone}
3762
*/
3863
public static long convertDateToLocalTime(long time, TimeZone to) {
@@ -41,6 +66,7 @@ public static long convertDateToLocalTime(long time, TimeZone to) {
4166

4267
/**
4368
* Converts a given time from the specified {@link TimeZone} to the corresponding UTC (Coordinated Universal Time) time
69+
*
4470
* @param time the instant to convert, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970
4571
* @param from the original {@link TimeZone}
4672
* @return the UTC instant, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970
@@ -51,9 +77,10 @@ public static long convertDateToUTC(long time, TimeZone from) {
5177

5278
/**
5379
* Converts a given time from a {@link TimeZone} to another
80+
*
5481
* @param time the instant to convert, represented as the number of milliseconds that have elapsed since midnight, January 1, 1970 in the {@code from} {@link TimeZone}
5582
* @param from the original {@link TimeZone}
56-
* @param to the target {@link TimeZone} for the conversion
83+
* @param to the target {@link TimeZone} for the conversion
5784
* @return the long representation of the instant converted to the specified {@code to} {@link TimeZone}
5885
*/
5986
public static long convertDate(long time, TimeZone from, TimeZone to) {
@@ -64,10 +91,10 @@ public static long convertDate(long time, TimeZone from, TimeZone to) {
6491
* Creates an UTC-based {@Link Date} using the provided {@code year}.
6592
* Uses the current date and time, sets the specified {@code year} and returns the Date converted to a UTC timestamp.
6693
*
67-
* @param year the year to represent
94+
* @param year the year to represent
6895
* @return the UTC-based {@link Date}
6996
*/
70-
public static Date of(int year){
97+
public static Date of(int year) {
7198
GregorianCalendar cal = new GregorianCalendar();
7299
cal.set(Calendar.YEAR, year);
73100
TimeZone fromTimeZone = cal.getTimeZone();
@@ -79,10 +106,10 @@ public static Date of(int year){
79106
* Uses the current date and time, sets the specified {@code year} and {@code month}, and returns the Date converted to a UTC timestamp.
80107
*
81108
* @param year the year to represent
82-
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
109+
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
83110
* @return the UTC-based {@link Date}
84111
*/
85-
public static Date of(int year, int month){
112+
public static Date of(int year, int month) {
86113
Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)");
87114
GregorianCalendar cal = new GregorianCalendar();
88115
cal.set(Calendar.YEAR, year);
@@ -96,11 +123,11 @@ public static Date of(int year, int month){
96123
* Uses the current date and time, sets the specified {@code year}, {@code month} and {@code day}, and returns the Date converted to a UTC timestamp.
97124
*
98125
* @param year the year to represent
99-
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
100-
* @param day the day-of-month to represent, from 1 to 31
126+
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
127+
* @param day the day-of-month to represent, from 1 to 31
101128
* @return the UTC-based {@link Date}
102129
*/
103-
public static Date of(int year, int month, int day){
130+
public static Date of(int year, int month, int day) {
104131
Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)");
105132
Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31");
106133
GregorianCalendar cal = new GregorianCalendar();
@@ -116,12 +143,12 @@ public static Date of(int year, int month, int day){
116143
* Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day} and {@code hour}, and returns the Date converted to a UTC timestamp.
117144
*
118145
* @param year the year to represent
119-
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
120-
* @param day the day-of-month to represent, from 1 to 31
146+
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
147+
* @param day the day-of-month to represent, from 1 to 31
121148
* @param hour the hour-of-day to represent, from 0 to 23
122149
* @return the UTC-based {@link Date}
123150
*/
124-
public static Date of(int year, int month, int day, int hour){
151+
public static Date of(int year, int month, int day, int hour) {
125152
Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)");
126153
Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31");
127154
Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 0 to 23");
@@ -138,14 +165,14 @@ public static Date of(int year, int month, int day, int hour){
138165
* Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day}, {@code hour} and {@code minute}.
139166
* Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day}, {@code hour} and {@code minute}, and returns the Date converted to a UTC timestamp.
140167
*
141-
* @param year the year to represent
168+
* @param year the year to represent
142169
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
143-
* @param day the day-of-month to represent, from 1 to 31
144-
* @param hour the hour-of-day to represent, from 0 to 23
145-
* @param minute the minute-of-hour to represent, from 0 to 59
170+
* @param day the day-of-month to represent, from 1 to 31
171+
* @param hour the hour-of-day to represent, from 0 to 23
172+
* @param minute the minute-of-hour to represent, from 0 to 59
146173
* @return the UTC-based {@link Date}
147174
*/
148-
public static Date of(int year, int month, int day, int hour, int minute){
175+
public static Date of(int year, int month, int day, int hour, int minute) {
149176
Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)");
150177
Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31");
151178
Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 0 to 23");
@@ -164,15 +191,15 @@ public static Date of(int year, int month, int day, int hour, int minute){
164191
* Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}.
165192
* Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}, and returns the Date converted to a UTC timestamp.
166193
*
167-
* @param year the year to represent
194+
* @param year the year to represent
168195
* @param month the month-of-year to represent, from 0 (January) to 11 (December)
169-
* @param day the day-of-month to represent, from 1 to 31
170-
* @param hour the hour-of-day to represent, from 0 to 23
171-
* @param minute the minute-of-hour to represent, from 0 to 59
172-
* @param second the second-of-hour to represent, from 0 to 59
196+
* @param day the day-of-month to represent, from 1 to 31
197+
* @param hour the hour-of-day to represent, from 0 to 23
198+
* @param minute the minute-of-hour to represent, from 0 to 59
199+
* @param second the second-of-hour to represent, from 0 to 59
173200
* @return the UTC-based {@link Date}
174201
*/
175-
public static Date of(int year, int month, int day, int hour, int minute, int second){
202+
public static Date of(int year, int month, int day, int hour, int minute, int second) {
176203
Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)");
177204
Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31");
178205
Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 1 to 23");
@@ -193,16 +220,16 @@ public static Date of(int year, int month, int day, int hour, int minute, int se
193220
* Creates an UTC-based {@Link Date} from the provided {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}.
194221
* Uses the current date and time, sets the specified {@code year}, {@code month}, {@code day}, {@code hour}, {@code minute} and {@code second}, and returns the Date converted to a UTC timestamp.
195222
*
196-
* @param year the YEAR to represent
197-
* @param month the MONTH to represent, from 0 (January) to 11 (December)
198-
* @param day the DAY_OF_MONTH to represent, from 1 to 31
199-
* @param hour the HOUR_OF_DAY to represent, from 0 to 23
200-
* @param minute the MINUTE to represent, from 0 to 59
201-
* @param second the SECOND to represent, from 0 to 59
223+
* @param year the YEAR to represent
224+
* @param month the MONTH to represent, from 0 (January) to 11 (December)
225+
* @param day the DAY_OF_MONTH to represent, from 1 to 31
226+
* @param hour the HOUR_OF_DAY to represent, from 0 to 23
227+
* @param minute the MINUTE to represent, from 0 to 59
228+
* @param second the SECOND to represent, from 0 to 59
202229
* @param millisecond the MILLISECOND to represent, from 0 to 59
203230
* @return the UTC-based {@link Date}
204231
*/
205-
public static Date of(int year, int month, int day, int hour, int minute, int second, int millisecond){
232+
public static Date of(int year, int month, int day, int hour, int minute, int second, int millisecond) {
206233
Assert.isTrue(0 <= month && month <= 11, "month param must be a value from 0 (January) to 11 (December)");
207234
Assert.isTrue(1 <= day && day <= 31, "day param must be a value from 1 to 31");
208235
Assert.isTrue(0 <= hour && hour <= 23, "hour param must be a value from 1 to 23");
@@ -225,18 +252,18 @@ private static long getTimeZoneOffset(long time, TimeZone from, TimeZone to) {
225252
int toOffset = to.getOffset(time);
226253
int diff = 0;
227254

228-
if (fromOffset >= 0){
229-
if (toOffset > 0){
230-
toOffset = -1*toOffset;
255+
if (fromOffset >= 0) {
256+
if (toOffset > 0) {
257+
toOffset = -1 * toOffset;
231258
} else {
232259
toOffset = Math.abs(toOffset);
233260
}
234-
diff = (fromOffset+toOffset)*-1;
261+
diff = (fromOffset + toOffset) * -1;
235262
} else {
236-
if (toOffset <= 0){
237-
toOffset = -1*Math.abs(toOffset);
263+
if (toOffset <= 0) {
264+
toOffset = -1 * Math.abs(toOffset);
238265
}
239-
diff = (Math.abs(fromOffset)+toOffset);
266+
diff = (Math.abs(fromOffset) + toOffset);
240267
}
241268
return diff;
242269
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.stormpath.sdk.provider;
2+
3+
4+
import com.stormpath.sdk.lang.Assert;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* A value specifying how to present the accessToken credential when interacting with the authorizationEndpoint and/or
11+
* the tokenEndpoint.
12+
* Possible values are bearer (token as part of authorization header), access_token and oauth_token (both of which are
13+
* query parameters).
14+
*
15+
* Different OAuth2 providers support different ways to present the accessToken. Please read the provider's "OAuth2"
16+
* implementation documentation in detail to identify and pick the type that they support. This allows us to support
17+
* any current OAuth2 provider.
18+
* Additionally, we can support new types as they emerge by updating this enum.
19+
*
20+
* @since 1.3.0
21+
*/
22+
public enum AccessTokenType {
23+
24+
BEARER("bearer"),
25+
ACCESS_TOKEN_PARAMETER("access_token"),
26+
OAUTH_TOKEN_PARAMETER("oauth_token");
27+
28+
private static final Map<String, AccessTokenType> TOKEN_TYPE_MAP;
29+
30+
static {
31+
TOKEN_TYPE_MAP = new HashMap<>();
32+
33+
for (AccessTokenType accessTokenType : AccessTokenType.values()) {
34+
TOKEN_TYPE_MAP.put(accessTokenType.nameKey, accessTokenType);
35+
}
36+
}
37+
38+
private String nameKey;
39+
40+
AccessTokenType(String nameKey) {
41+
this.nameKey = nameKey;
42+
}
43+
44+
public static AccessTokenType fromNameKey(String nameKey) {
45+
46+
Assert.notNull(nameKey, "accessTokenType is required.");
47+
48+
AccessTokenType accessTokenType = TOKEN_TYPE_MAP.get(nameKey.toLowerCase());
49+
50+
Assert.notNull(accessTokenType, "Invalid accessTokenType : " + nameKey);
51+
52+
return accessTokenType;
53+
}
54+
55+
public String getNameKey() {
56+
return this.nameKey;
57+
}
58+
59+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link CreateProviderRequestBuilder} interface for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2CreateProviderRequestBuilder extends CreateProviderRequestBuilder<GenericOAuth2CreateProviderRequestBuilder> {
9+
10+
/**
11+
* Setter for the authorizationEndpoint for the OAuth2 provider.
12+
*
13+
* @param authorizationEndpoint the authorizationEndpoint for the OAuth2 provider.
14+
* @return the builder instance for method chaining.
15+
*/
16+
GenericOAuth2CreateProviderRequestBuilder setAuthorizationEndpoint(String authorizationEndpoint);
17+
18+
/**
19+
* Setter for the tokenEndpoint for the OAuth2 provider.
20+
*
21+
* @param tokenEndpoint the tokenEndpoint for the OAuth2 provider.
22+
* @return the builder instance for method chaining.
23+
*/
24+
GenericOAuth2CreateProviderRequestBuilder setTokenEndpoint(String tokenEndpoint);
25+
26+
/**
27+
* Setter for the resourceEndpoint for the OAuth2 provider.
28+
*
29+
* @param resourceEndpoint the resourceEndpoint for the OAuth2 provider.
30+
* @return the builder instance for method chaining.
31+
*/
32+
GenericOAuth2CreateProviderRequestBuilder setResourceEndpoint(String resourceEndpoint);
33+
34+
/**
35+
* Setter for the accessTokenType for the OAuth2 provider.
36+
*
37+
* @param accessTokenType the accessTokenType for the OAuth2 provider.
38+
* @return the builder instance for method chaining.
39+
*/
40+
GenericOAuth2CreateProviderRequestBuilder setAccessTokenType(AccessTokenType accessTokenType);
41+
42+
/**
43+
* Setter for the provider id of the OAuth2 provider(e.g. "amazon").
44+
*
45+
* @param providerId the provider id of the OAuth2 provider.
46+
* @return the builder instance for method chaining.
47+
*/
48+
GenericOAuth2CreateProviderRequestBuilder setClientId(String providerId);
49+
50+
GenericOAuth2CreateProviderRequestBuilder setProviderId(String providerId);
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link Provider} Resource for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2Provider extends OAuthProvider {
9+
10+
GenericOAuth2Provider setProviderId(String providerId);
11+
12+
String getAuthorizationEndpoint();
13+
14+
String getTokenEndpoint();
15+
16+
String getResourceEndpoint();
17+
18+
AccessTokenType getAccessType();
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link ProviderAccountRequestBuilder} interface for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2ProviderAccountRequestBuilder extends ProviderAccountRequestBuilder<GenericOAuth2ProviderAccountRequestBuilder> {
9+
10+
/**
11+
* Setter for the provider id of the OAuth2 Provider (as provided when creating the provider directory).
12+
*
13+
* @param providerId the provider id of the OAuth2 Provider
14+
* @return the builder instance for method chaining.
15+
*/
16+
GenericOAuth2ProviderAccountRequestBuilder setProviderId(String providerId);
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link ProviderData} Resource for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2ProviderData extends ProviderData {
9+
10+
/**
11+
* Getter for the OAuth2 provider's access token.
12+
*
13+
* @return the OAuth2 provider's access token.
14+
*/
15+
String getAccessToken();
16+
17+
GenericOAuth2ProviderData setProviderId(String providerId);
18+
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link ProviderRequestFactory} interface for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2ProviderRequestFactory extends ProviderRequestFactory<GenericOAuth2ProviderAccountRequestBuilder, GenericOAuth2CreateProviderRequestBuilder> {
9+
}

0 commit comments

Comments
 (0)