Skip to content

Commit b857227

Browse files
committed
Merge pull request #1 from auth0/authentication-api
Authentication api
2 parents ce87538 + 36a1f66 commit b857227

47 files changed

Lines changed: 5160 additions & 2 deletions

Some content is hidden

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

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
allprojects {
2-
version = 'com.auth0.java'
3-
group = '0.1-SNAPSHOT'
2+
group = 'com.auth0'
3+
version = '0.0.1-SNAPSHOT'
44

55
repositories {
66
mavenCentral()

core/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
apply plugin: 'java'
22

33
dependencies {
4+
compile 'com.squareup.okhttp:okhttp:2.5.0'
5+
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.1'
6+
compile 'com.fasterxml.jackson.core:jackson-core:2.4.1'
7+
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.1'
8+
compile 'org.json:json:20151123'
9+
410
testCompile 'junit:junit:4.12'
11+
testCompile 'org.hamcrest:hamcrest-integration:1.3'
12+
testCompile 'org.hamcrest:hamcrest-core:1.3'
13+
testCompile 'org.hamcrest:hamcrest-library:1.3'
14+
testCompile 'org.mockito:mockito-core:1.10.19'
15+
testCompile 'com.squareup.okhttp:mockwebserver:2.5.0'
16+
testCompile 'com.jayway.awaitility:awaitility:1.6.4'
517
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
* Application.java
3+
*
4+
* Copyright (c) 2015 Auth0 (http://auth0.com)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.auth0;
26+
27+
import com.fasterxml.jackson.annotation.JsonCreator;
28+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
29+
import com.fasterxml.jackson.annotation.JsonProperty;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
import static com.auth0.authentication.api.util.CheckHelper.checkArgument;
35+
36+
/**
37+
* Class with your Auth0's application information and the list of enabled connections (DB, Social, Enterprise, Passwordless).
38+
*/
39+
@JsonIgnoreProperties(ignoreUnknown = true)
40+
public class Application {
41+
42+
private String id;
43+
private String tenant;
44+
private String authorizeURL;
45+
private String callbackURL;
46+
private String subscription;
47+
private boolean hasAllowedOrigins;
48+
private List<Strategy> strategies;
49+
private List<Strategy> socialStrategies;
50+
private List<Strategy> enterpriseStrategies;
51+
private Strategy databaseStrategy;
52+
53+
public Application(Application application) {
54+
id = application.id;
55+
tenant = application.tenant;
56+
authorizeURL = application.authorizeURL;
57+
callbackURL = application.callbackURL;
58+
subscription = application.subscription;
59+
hasAllowedOrigins = application.hasAllowedOrigins;
60+
strategies = application.strategies;
61+
socialStrategies = application.socialStrategies;
62+
enterpriseStrategies = application.enterpriseStrategies;
63+
databaseStrategy = application.databaseStrategy;
64+
}
65+
66+
/**
67+
* Creates a new application instance
68+
* @param id app id.
69+
* @param tenant name of the tenant who owns the app.
70+
* @param authorizeURL url used to authorize during oauth flow.
71+
* @param callbackURL url used after a oauth flow.
72+
* @param subscription type of subscription.
73+
* @param hasAllowedOrigins if the app allows other origins
74+
* @param strategies list of the strategies enabled for the app (Social, DB, etc).
75+
*/
76+
@JsonCreator
77+
public Application(@JsonProperty(value = "id") String id,
78+
@JsonProperty(value = "tenant") String tenant,
79+
@JsonProperty(value = "authorize") String authorizeURL,
80+
@JsonProperty(value = "callback") String callbackURL,
81+
@JsonProperty(value = "subscription") String subscription,
82+
@JsonProperty(value = "hasAllowedOrigins") boolean hasAllowedOrigins,
83+
@JsonProperty(value = "strategies") List<Strategy> strategies) {
84+
checkArgument(id != null, "id must be non-null");
85+
checkArgument(tenant != null, "tenant must be non-null");
86+
checkArgument(authorizeURL != null, "authorize must be non-null");
87+
checkArgument(strategies != null && strategies.size() > 0, "Must have at least 1 strategy");
88+
this.id = id;
89+
this.tenant = tenant;
90+
this.authorizeURL = authorizeURL;
91+
this.callbackURL = callbackURL;
92+
this.subscription = subscription;
93+
this.hasAllowedOrigins = hasAllowedOrigins;
94+
this.strategies = strategies;
95+
this.socialStrategies = new ArrayList<>();
96+
this.enterpriseStrategies = new ArrayList<>();
97+
for(Strategy strategy: strategies) {
98+
if (Strategies.Auth0.getName().equals(strategy.getName())) {
99+
this.databaseStrategy = strategy;
100+
} else {
101+
switch (strategy.getType()) {
102+
case SOCIAL:
103+
this.socialStrategies.add(strategy);
104+
break;
105+
case ENTERPRISE:
106+
this.enterpriseStrategies.add(strategy);
107+
break;
108+
}
109+
}
110+
}
111+
}
112+
113+
/**
114+
* Returns the id of the application.
115+
* @return an ID
116+
*/
117+
public String getId() {
118+
return id;
119+
}
120+
121+
/**
122+
* Returns the name of the tenant who owns the app.
123+
* @return name of the tenant
124+
*/
125+
public String getTenant() {
126+
return tenant;
127+
}
128+
129+
/**
130+
* Returns url used to authorize during oauth flow.
131+
* @return a url string
132+
*/
133+
public String getAuthorizeURL() {
134+
return authorizeURL;
135+
}
136+
137+
/**
138+
* Returns url used after a oauth flow.
139+
* @return a url string
140+
*/
141+
public String getCallbackURL() {
142+
return callbackURL;
143+
}
144+
145+
/**
146+
* Returns the type of subscription
147+
* @return type of subscription
148+
*/
149+
public String getSubscription() {
150+
return subscription;
151+
}
152+
153+
/**
154+
* Returns if the app allows other origins.
155+
* @return hasAllowedOrigins flag
156+
*/
157+
public boolean isHasAllowedOrigins() {
158+
return hasAllowedOrigins;
159+
}
160+
161+
/**
162+
* Returns all available auth strategies for the app.
163+
* @return
164+
*/
165+
public List<Strategy> getStrategies() {
166+
return new ArrayList<>(strategies);
167+
}
168+
169+
/**
170+
* Returns the Database strategy of the app.
171+
* @return DB strategy
172+
*/
173+
public Strategy getDatabaseStrategy() {
174+
return databaseStrategy;
175+
}
176+
177+
/**
178+
* Returns the social strategies of the app.
179+
* @return list of social strategies
180+
*/
181+
public List<Strategy> getSocialStrategies() {
182+
return new ArrayList<>(socialStrategies);
183+
}
184+
185+
/**
186+
* Returns the social enterprise of the app.
187+
* @return list of enterprise strategies
188+
*/
189+
public List<Strategy> getEnterpriseStrategies() {
190+
return new ArrayList<>(enterpriseStrategies);
191+
}
192+
193+
/**
194+
* Returns a {@link Strategy} by its name
195+
* @param name strategy name
196+
* @return a {@link Strategy}
197+
*/
198+
public Strategy strategyForName(String name) {
199+
for (Strategy strategy: this.strategies) {
200+
if (strategy.getName().equals(name)) {
201+
return strategy;
202+
}
203+
}
204+
return null;
205+
}
206+
207+
/**
208+
* Returns the strategy by one of its connections
209+
* @param connection a connection
210+
* @return a {@link Strategy}
211+
*/
212+
public Strategy strategyForConnection(Connection connection) {
213+
for (Strategy strategy : this.strategies) {
214+
for (Connection conn : strategy.getConnections()) {
215+
if (conn.getName().equals(connection.getName())) {
216+
return strategy;
217+
}
218+
}
219+
}
220+
return null;
221+
}
222+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Auth0.java
3+
*
4+
* Copyright (c) 2015 Auth0 (http://auth0.com)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.auth0;
26+
27+
import com.auth0.authentication.AuthenticationAPIClient;
28+
import com.squareup.okhttp.HttpUrl;
29+
30+
public class Auth0 {
31+
32+
private static final String AUTH0_US_CDN_URL = "https://cdn.auth0.com";
33+
private static final String DOT_AUTH0_DOT_COM = ".auth0.com";
34+
35+
protected final String clientId;
36+
protected final String domainUrl;
37+
protected final String configurationUrl;
38+
39+
public Auth0(String clientId, String domain) {
40+
this(clientId, domain, null);
41+
}
42+
43+
public Auth0(String clientId, String domain, String configurationDomain) {
44+
this.clientId = clientId;
45+
this.domainUrl = ensureUrlString(domain);
46+
this.configurationUrl = resolveConfiguration(configurationDomain, this.domainUrl);
47+
}
48+
49+
private String resolveConfiguration(String configurationDomain, String domainUrl) {
50+
String url = ensureUrlString(configurationDomain);
51+
if (configurationDomain == null && domainUrl != null) {
52+
final HttpUrl domainUri = HttpUrl.parse(domainUrl);
53+
final String host = domainUri.host();
54+
if (host.endsWith(DOT_AUTH0_DOT_COM)) {
55+
String[] parts = host.split("\\.");
56+
if (parts.length > 3) {
57+
url = "https://cdn." + parts[parts.length-3] + DOT_AUTH0_DOT_COM;
58+
} else {
59+
url = AUTH0_US_CDN_URL;
60+
}
61+
} else {
62+
url = domainUrl;
63+
}
64+
}
65+
return url;
66+
}
67+
68+
private String ensureUrlString(String url) {
69+
String safeUrl = null;
70+
if (url != null) {
71+
safeUrl = url.startsWith("http") ? url : "https://" + url;
72+
}
73+
return safeUrl;
74+
}
75+
76+
public String getClientId() {
77+
return clientId;
78+
}
79+
80+
public String getDomainUrl() {
81+
return domainUrl;
82+
}
83+
84+
public String getConfigurationUrl() {
85+
return configurationUrl;
86+
}
87+
88+
public AuthenticationAPIClient newAuthenticationAPIClient() {
89+
return new AuthenticationAPIClient(this);
90+
}
91+
92+
public String getAuthorizeUrl() {
93+
return HttpUrl.parse(domainUrl).newBuilder()
94+
.addEncodedPathSegment("authorize")
95+
.build()
96+
.toString();
97+
}
98+
}

0 commit comments

Comments
 (0)