|
| 1 | +package io.quarkus.test.oidc.client; |
| 2 | + |
| 3 | +import static org.awaitility.Awaitility.await; |
| 4 | + |
| 5 | +import java.net.URLEncoder; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 11 | + |
| 12 | +import io.vertx.core.MultiMap; |
| 13 | +import io.vertx.core.Vertx; |
| 14 | +import io.vertx.core.buffer.Buffer; |
| 15 | +import io.vertx.ext.web.client.WebClient; |
| 16 | + |
| 17 | +public class OidcTestClient { |
| 18 | + |
| 19 | + private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(10); |
| 20 | + private final static String CLIENT_AUTH_SERVER_URL_PROP = "client.quarkus.oidc.auth-server-url"; |
| 21 | + private final static String AUTH_SERVER_URL_PROP = "quarkus.oidc.auth-server-url"; |
| 22 | + private final static String CLIENT_ID_PROP = "quarkus.oidc.client-id"; |
| 23 | + private final static String CLIENT_SECRET_PROP = "quarkus.oidc.credentials.secret"; |
| 24 | + |
| 25 | + Vertx vertx = Vertx.vertx(); |
| 26 | + WebClient client = WebClient.create(vertx); |
| 27 | + |
| 28 | + private String authServerUrl; |
| 29 | + private String tokenUrl; |
| 30 | + |
| 31 | + /** |
| 32 | + * Get an access token a client_credentials grant. |
| 33 | + * Client id must be configured with the `quarkus.oidc.client-id` property. |
| 34 | + * Client secret must be configured with the `quarkus.oidc.credentials.secret` property. |
| 35 | + */ |
| 36 | + public String getClientAccessToken() { |
| 37 | + return getClientAccessToken(null); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get an access token a client_credentials grant with additional properties. |
| 42 | + * Client id must be configured with the `quarkus.oidc.client-id` property. |
| 43 | + * Client secret must be configured with the `quarkus.oidc.credentials.secret` property. |
| 44 | + */ |
| 45 | + public String getClientAccessToken(Map<String, String> extraProps) { |
| 46 | + return getClientAccessToken(getClientId(), getClientSecret(), extraProps); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get an access token from the default tenant realm using a client_credentials grant with a |
| 51 | + * the provided client id and secret. |
| 52 | + */ |
| 53 | + public String getClientAccessToken(String clientId, String clientSecret) { |
| 54 | + return getClientAccessToken(clientId, clientSecret, null); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get an access token using a client_credentials grant with the provided client id and secret, |
| 59 | + * and additional properties. |
| 60 | + */ |
| 61 | + public String getClientAccessToken(String clientId, String clientSecret, Map<String, String> extraProps) { |
| 62 | + MultiMap requestMap = MultiMap.caseInsensitiveMultiMap(); |
| 63 | + requestMap.add("grant_type", "client_credentials") |
| 64 | + .add("client_id", clientId); |
| 65 | + if (clientSecret != null && !clientSecret.isBlank()) { |
| 66 | + requestMap.add("client_secret", clientSecret); |
| 67 | + } |
| 68 | + return getAccessTokenInternal(requestMap, extraProps); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get an access token from the default tenant realm using a password grant with the provided user name, user secret. |
| 73 | + * Client id must be configured with the `quarkus.oidc.client-id` property. |
| 74 | + * Client secret must be configured with the `quarkus.oidc.credentials.secret` property. |
| 75 | + */ |
| 76 | + public String getAccessToken(String userName, String userSecret) { |
| 77 | + return getAccessToken(userName, userSecret, null); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get an access token from the default tenant realm using a password grant with the provided user name, user secret, |
| 82 | + * and additional properties. |
| 83 | + * Client id must be configured with the `quarkus.oidc.client-id` property. |
| 84 | + * Client secret must be configured with the `quarkus.oidc.credentials.secret` property. |
| 85 | + */ |
| 86 | + public String getAccessToken(String userName, String userSecret, Map<String, String> extraProps) { |
| 87 | + return getAccessToken(getClientId(), getClientSecret(), userName, userSecret, extraProps); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get an access token from the default tenant realm using a password grant with the provided client id, client secret, user |
| 92 | + * name, user secret, client |
| 93 | + * id and user secret. |
| 94 | + */ |
| 95 | + public String getAccessToken(String clientId, String clientSecret, String userName, String userSecret) { |
| 96 | + return getAccessToken(userName, userSecret, clientId, clientSecret, null); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get an access token using a password grant with the provided user name, user secret, client |
| 101 | + * id and secret, and scopes. |
| 102 | + */ |
| 103 | + public String getAccessToken(String clientId, String clientSecret, String userName, String userSecret, |
| 104 | + Map<String, String> extraProps) { |
| 105 | + |
| 106 | + MultiMap requestMap = MultiMap.caseInsensitiveMultiMap(); |
| 107 | + requestMap.add("grant_type", "password") |
| 108 | + .add("username", userName) |
| 109 | + .add("password", userSecret); |
| 110 | + |
| 111 | + requestMap.add("client_id", clientId); |
| 112 | + if (clientSecret != null && !clientSecret.isBlank()) { |
| 113 | + requestMap.add("client_secret", clientSecret); |
| 114 | + } |
| 115 | + return getAccessTokenInternal(requestMap, extraProps); |
| 116 | + } |
| 117 | + |
| 118 | + private String getAccessTokenInternal(MultiMap requestMap, Map<String, String> extraProps) { |
| 119 | + |
| 120 | + if (extraProps != null) { |
| 121 | + requestMap = requestMap.addAll(extraProps); |
| 122 | + } |
| 123 | + |
| 124 | + var result = client.postAbs(getTokenUrl()) |
| 125 | + .putHeader("Content-Type", "application/x-www-form-urlencoded") |
| 126 | + .sendBuffer(encodeForm(requestMap)); |
| 127 | + await().atMost(REQUEST_TIMEOUT).until(result::isComplete); |
| 128 | + |
| 129 | + return result.result().bodyAsJsonObject().getString("access_token"); |
| 130 | + } |
| 131 | + |
| 132 | + private String getClientId() { |
| 133 | + return getPropertyValue(CLIENT_ID_PROP); |
| 134 | + } |
| 135 | + |
| 136 | + private String getClientSecret() { |
| 137 | + return getPropertyValue(CLIENT_SECRET_PROP); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return URL string configured with a 'quarkus.oidc.auth-server' property. |
| 142 | + */ |
| 143 | + public String getAuthServerUrl() { |
| 144 | + if (authServerUrl == null) { |
| 145 | + authServerUrl = getOptionalPropertyValue(CLIENT_AUTH_SERVER_URL_PROP, AUTH_SERVER_URL_PROP); |
| 146 | + } |
| 147 | + return authServerUrl; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Return URL string configured with a 'quarkus.oidc.auth-server' property. |
| 152 | + */ |
| 153 | + public String getTokenUrl() { |
| 154 | + if (tokenUrl == null) { |
| 155 | + getAuthServerUrl(); |
| 156 | + var result = client.getAbs(authServerUrl + "/.well-known/openid-configuration") |
| 157 | + .send(); |
| 158 | + await().atMost(REQUEST_TIMEOUT).until(result::isComplete); |
| 159 | + tokenUrl = result.result().bodyAsJsonObject().getString("token_endpoint"); |
| 160 | + } |
| 161 | + return tokenUrl; |
| 162 | + } |
| 163 | + |
| 164 | + private String getPropertyValue(String prop) { |
| 165 | + return ConfigProvider.getConfig().getValue(prop, String.class); |
| 166 | + } |
| 167 | + |
| 168 | + private String getOptionalPropertyValue(String prop, String defaultProp) { |
| 169 | + return ConfigProvider.getConfig().getOptionalValue(prop, String.class) |
| 170 | + .orElseGet(() -> ConfigProvider.getConfig().getValue(defaultProp, String.class)); |
| 171 | + } |
| 172 | + |
| 173 | + public static Buffer encodeForm(MultiMap form) { |
| 174 | + Buffer buffer = Buffer.buffer(); |
| 175 | + for (Map.Entry<String, String> entry : form) { |
| 176 | + if (buffer.length() != 0) { |
| 177 | + buffer.appendByte((byte) '&'); |
| 178 | + } |
| 179 | + buffer.appendString(entry.getKey()); |
| 180 | + buffer.appendByte((byte) '='); |
| 181 | + buffer.appendString(urlEncode(entry.getValue())); |
| 182 | + } |
| 183 | + return buffer; |
| 184 | + } |
| 185 | + |
| 186 | + private static String urlEncode(String value) { |
| 187 | + try { |
| 188 | + return URLEncoder.encode(value, StandardCharsets.UTF_8.name()); |
| 189 | + } catch (Exception ex) { |
| 190 | + throw new RuntimeException(ex); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public void close() { |
| 195 | + if (client != null) { |
| 196 | + client.close(); |
| 197 | + client = null; |
| 198 | + } |
| 199 | + if (vertx != null) { |
| 200 | + vertx.close().toCompletionStage().toCompletableFuture().join(); |
| 201 | + vertx = null; |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments