|
| 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 | +} |
0 commit comments