|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Okta, Inc. and/or its affiliates. All rights reserved. |
| 3 | + * The Okta software accompanied by this notice is provided pursuant to the Apache License, |
| 4 | + * Version 2.0 (the "License.") |
| 5 | + * |
| 6 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * |
| 12 | + * See the License for the specific language governing permissions and limitations under the |
| 13 | + * License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.okta.oidc; |
| 17 | + |
| 18 | +import android.text.TextUtils; |
| 19 | + |
| 20 | +import androidx.annotation.NonNull; |
| 21 | + |
| 22 | +/** |
| 23 | + * Used for authorization servers that does not provide a discovery endpoint. |
| 24 | + * It is recommended to use {@link com.okta.oidc.OIDCConfig.Builder#discoveryUri(String)} |
| 25 | + * to fetch the Open ID provider configuration instead. |
| 26 | + * |
| 27 | + * @see "OpenID provider issuer discovery <https://openid.net/specs/openid-connect-discovery-1_0.html#IssuerDiscovery" |
| 28 | + */ |
| 29 | +@SuppressWarnings("unused") |
| 30 | +public class CustomConfiguration { |
| 31 | + private String mAuthorizationEndpoint; |
| 32 | + private String mTokenEndpoint; |
| 33 | + private String mUserInfoEndpoint; |
| 34 | + private String mJwksUri; |
| 35 | + private String mRegistrationEndpoint; |
| 36 | + private String mIntrospectionEndpoint; |
| 37 | + private String mRevocationEndpoint; |
| 38 | + private String mEndSessionEndpoint; |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets authorization endpoint. |
| 42 | + * |
| 43 | + * @return the authorization endpoint |
| 44 | + */ |
| 45 | + public String getAuthorizationEndpoint() { |
| 46 | + return mAuthorizationEndpoint; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets token endpoint. |
| 51 | + * |
| 52 | + * @return the token endpoint |
| 53 | + */ |
| 54 | + public String getTokenEndpoint() { |
| 55 | + return mTokenEndpoint; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Gets user info endpoint. |
| 60 | + * |
| 61 | + * @return the user info endpoint |
| 62 | + */ |
| 63 | + public String getUserInfoEndpoint() { |
| 64 | + return mUserInfoEndpoint; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets jwks uri. |
| 69 | + * |
| 70 | + * @return the jwks uri |
| 71 | + */ |
| 72 | + public String getJwksUri() { |
| 73 | + return mJwksUri; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets registration endpoint. |
| 78 | + * |
| 79 | + * @return the registration endpoint |
| 80 | + */ |
| 81 | + public String getRegistrationEndpoint() { |
| 82 | + return mRegistrationEndpoint; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets introspection endpoint. |
| 87 | + * |
| 88 | + * @return the introspection endpoint |
| 89 | + */ |
| 90 | + public String getIntrospectionEndpoint() { |
| 91 | + return mIntrospectionEndpoint; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Gets revocation endpoint. |
| 96 | + * |
| 97 | + * @return the revocation endpoint |
| 98 | + */ |
| 99 | + public String getRevocationEndpoint() { |
| 100 | + return mRevocationEndpoint; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets end session endpoint. |
| 105 | + * |
| 106 | + * @return the end session endpoint |
| 107 | + */ |
| 108 | + public String getEndSessionEndpoint() { |
| 109 | + return mEndSessionEndpoint; |
| 110 | + } |
| 111 | + |
| 112 | + private CustomConfiguration() { |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The CustomConfiguration Builder for setting endpoints for Okta OpenID provider. |
| 117 | + * |
| 118 | + * @see "OpenID Connect & OAuth 2.0 API <https://developer.okta.com/docs/reference/api/oidc/#endpoints" |
| 119 | + */ |
| 120 | + public static class Builder { |
| 121 | + private CustomConfiguration mConfiguration; |
| 122 | + |
| 123 | + /** |
| 124 | + * Instantiates a new Builder for CustomConfiguration. |
| 125 | + */ |
| 126 | + public Builder() { |
| 127 | + mConfiguration = new CustomConfiguration(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Required authorization endpoint. |
| 132 | + * |
| 133 | + * @param authorizationEndpoint the authorization endpoint |
| 134 | + * @return the builder |
| 135 | + */ |
| 136 | + public Builder authorizationEndpoint(@NonNull String authorizationEndpoint) { |
| 137 | + mConfiguration.mAuthorizationEndpoint = authorizationEndpoint; |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Required token endpoint. |
| 143 | + * |
| 144 | + * @param tokenEndpoint the token endpoint |
| 145 | + * @return the builder |
| 146 | + */ |
| 147 | + public Builder tokenEndpoint(@NonNull String tokenEndpoint) { |
| 148 | + mConfiguration.mTokenEndpoint = tokenEndpoint; |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Optional user info endpoint. |
| 154 | + * |
| 155 | + * @param userInfoEndpoint the user info endpoint |
| 156 | + * @return the builder |
| 157 | + */ |
| 158 | + public Builder userInfoEndpoint(@NonNull String userInfoEndpoint) { |
| 159 | + mConfiguration.mUserInfoEndpoint = userInfoEndpoint; |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Optional Jwks uri. |
| 165 | + * |
| 166 | + * @param jwksUri the jwks uri |
| 167 | + * @return the builder |
| 168 | + */ |
| 169 | + public Builder jwksUri(@NonNull String jwksUri) { |
| 170 | + mConfiguration.mJwksUri = jwksUri; |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Optional registration endpoint. |
| 176 | + * |
| 177 | + * @param registrationEndpoint the registration endpoint |
| 178 | + * @return the builder |
| 179 | + */ |
| 180 | + public Builder registrationEndpoint(@NonNull String registrationEndpoint) { |
| 181 | + mConfiguration.mRegistrationEndpoint = registrationEndpoint; |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Optional introspection endpoint. |
| 187 | + * |
| 188 | + * @param introspectionEndpoint the introspection endpoint |
| 189 | + * @return the builder |
| 190 | + */ |
| 191 | + public Builder introspectionEndpoint(@NonNull String introspectionEndpoint) { |
| 192 | + mConfiguration.mIntrospectionEndpoint = introspectionEndpoint; |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Optional revocation endpoint. |
| 198 | + * |
| 199 | + * @param revocationEndpoint the revocation endpoint |
| 200 | + * @return the builder |
| 201 | + */ |
| 202 | + public Builder revocationEndpoint(@NonNull String revocationEndpoint) { |
| 203 | + mConfiguration.mRevocationEndpoint = revocationEndpoint; |
| 204 | + return this; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Optional end session endpoint. |
| 209 | + * |
| 210 | + * @param endSessionEndpoint the end session endpoint |
| 211 | + * @return the builder |
| 212 | + */ |
| 213 | + public Builder endSessionEndpoint(@NonNull String endSessionEndpoint) { |
| 214 | + mConfiguration.mEndSessionEndpoint = endSessionEndpoint; |
| 215 | + return this; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Create the custom configuration. |
| 220 | + * |
| 221 | + * @return Custom configuration |
| 222 | + * @throws IllegalStateException - If missing required endpoints. |
| 223 | + */ |
| 224 | + public CustomConfiguration create() throws IllegalStateException { |
| 225 | + validate(); |
| 226 | + return mConfiguration; |
| 227 | + } |
| 228 | + |
| 229 | + private void validate() { |
| 230 | + if (TextUtils.isEmpty(mConfiguration.getAuthorizationEndpoint())) { |
| 231 | + throw new IllegalStateException("No authorization endpoint specified"); |
| 232 | + } |
| 233 | + if (TextUtils.isEmpty(mConfiguration.getTokenEndpoint())) { |
| 234 | + throw new IllegalStateException("No token endpoint specified"); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments