Skip to content

Commit 9af8939

Browse files
authored
Merge pull request #3091 from AthenZ/openid-issuer
expose openid_issuer field for access tokens in zts java client
2 parents be15974 + b240310 commit 9af8939

File tree

4 files changed

+383
-22
lines changed

4 files changed

+383
-22
lines changed

clients/java/zts/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<description>ZTS Java Client Library</description>
2828

2929
<properties>
30-
<code.coverage.min>0.8035</code.coverage.min>
30+
<code.coverage.min>0.8183</code.coverage.min>
3131
</properties>
3232

3333
<dependencyManagement>

clients/java/zts/src/main/java/com/yahoo/athenz/zts/ZTSClient.java

Lines changed: 168 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ public RoleToken getRoleToken(String domainName, String roleNames, Integer minEx
12311231
* @return ZTS generated Access Token Response object. ZTSClientException will be thrown in case of failure
12321232
*/
12331233
public AccessTokenResponse getAccessToken(String domainName, List<String> roleNames, long expiryTime) {
1234-
return getAccessToken(domainName, roleNames, null, null, null, null, null, null, expiryTime, false);
1234+
return getAccessToken(domainName, roleNames, null, null, null, null, null, null, expiryTime, false, false);
12351235
}
12361236

12371237
/**
@@ -1250,7 +1250,7 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
12501250
*/
12511251
public AccessTokenResponse getAccessToken(String domainName, String roleName, String authorizationDetails, long expiryTime) {
12521252
return getAccessToken(domainName, Collections.singletonList(roleName), null, null,
1253-
authorizationDetails, null, null, null, expiryTime, false);
1253+
authorizationDetails, null, null, null, expiryTime, false, false);
12541254
}
12551255

12561256
/**
@@ -1269,7 +1269,8 @@ public AccessTokenResponse getAccessToken(String domainName, String roleName, St
12691269
*/
12701270
public AccessTokenResponse getAccessToken(String domainName, List<String> roleNames,
12711271
String idTokenServiceName, long expiryTime, boolean ignoreCache) {
1272-
return getAccessToken(domainName, roleNames, idTokenServiceName, null, null, null, null, null, expiryTime, ignoreCache);
1272+
return getAccessToken(domainName, roleNames, idTokenServiceName, null, null, null, null, null,
1273+
expiryTime, false, ignoreCache);
12731274
}
12741275

12751276
/**
@@ -1291,7 +1292,7 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
12911292
public AccessTokenResponse getAccessToken(String domainName, List<String> roleNames, String idTokenServiceName,
12921293
String proxyForPrincipal, String authorizationDetails, long expiryTime, boolean ignoreCache) {
12931294
return getAccessToken(domainName, roleNames, idTokenServiceName, proxyForPrincipal, authorizationDetails,
1294-
null, null, null, expiryTime, ignoreCache);
1295+
null, null, null, expiryTime, false, ignoreCache);
12951296
}
12961297

12971298
/**
@@ -1316,7 +1317,7 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
13161317
String proxyForPrincipal, String authorizationDetails, String proxyPrincipalSpiffeUris, long expiryTime,
13171318
boolean ignoreCache) {
13181319
return getAccessToken(domainName, roleNames, idTokenServiceName, proxyForPrincipal, authorizationDetails,
1319-
proxyPrincipalSpiffeUris, null, null, expiryTime, ignoreCache);
1320+
proxyPrincipalSpiffeUris, null, null, expiryTime, false, ignoreCache);
13201321
}
13211322

13221323
/**
@@ -1342,6 +1343,28 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
13421343
public AccessTokenResponse getAccessToken(String domainName, List<String> roleNames, String idTokenServiceName,
13431344
String proxyForPrincipal, String authorizationDetails, String proxyPrincipalSpiffeUris,
13441345
String clientAssertionType, String clientAssertion, long expiryTime, boolean ignoreCache) {
1346+
return getAccessToken(domainName, roleNames, idTokenServiceName, proxyForPrincipal, authorizationDetails,
1347+
proxyPrincipalSpiffeUris, clientAssertionType, clientAssertion, expiryTime, false, ignoreCache);
1348+
1349+
}
1350+
1351+
/**
1352+
* For the specified requester(user/service) return the corresponding Access Token using
1353+
* the builder pattern to construct the request parameters.
1354+
* @param builder AccessTokenRequestBuilder containing all request parameters
1355+
* @return ZTS generated Access Token Response object. ZTSClientException will be thrown in case of failure
1356+
*/
1357+
public AccessTokenResponse getAccessToken(AccessTokenRequestBuilder builder) {
1358+
return getAccessToken(builder.domainName, builder.roleNames, builder.idTokenServiceName,
1359+
builder.proxyForPrincipal, builder.authorizationDetails, builder.proxyPrincipalSpiffeUris,
1360+
builder.clientAssertionType, builder.clientAssertion, builder.expiryTime, builder.openIdIssuer,
1361+
builder.ignoreCache);
1362+
}
1363+
1364+
AccessTokenResponse getAccessToken(String domainName, List<String> roleNames, String idTokenServiceName,
1365+
String proxyForPrincipal, String authorizationDetails, String proxyPrincipalSpiffeUris,
1366+
String clientAssertionType, String clientAssertion, long expiryTime, boolean openIdIssuer,
1367+
boolean ignoreCache) {
13451368

13461369
AccessTokenResponse accessTokenResponse = null;
13471370

@@ -1386,8 +1409,8 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
13861409
updateServicePrincipal();
13871410
try {
13881411
final String requestBody = generateAccessTokenRequestBody(domainName, roleNames,
1389-
idTokenServiceName, proxyForPrincipal, authorizationDetails,
1390-
proxyPrincipalSpiffeUris, clientAssertionType, clientAssertion, expiryTime);
1412+
idTokenServiceName, proxyForPrincipal, authorizationDetails, proxyPrincipalSpiffeUris,
1413+
clientAssertionType, clientAssertion, expiryTime, openIdIssuer);
13911414
accessTokenResponse = ztsClient.postAccessTokenRequest(requestBody);
13921415
} catch (ClientResourceException ex) {
13931416
if (cacheKey != null && !ignoreCache) {
@@ -1408,7 +1431,6 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
14081431
}
14091432
}
14101433

1411-
14121434
// need to add the token to our cache. If our principal was
14131435
// updated then we need to retrieve a new cache key
14141436

@@ -1427,7 +1449,7 @@ public AccessTokenResponse getAccessToken(String domainName, List<String> roleNa
14271449

14281450
String generateAccessTokenRequestBody(String domainName, List<String> roleNames, String idTokenServiceName,
14291451
String proxyForPrincipal, String authorizationDetails, String proxyPrincipalSpiffeUris,
1430-
String clientAssertionType, String clientAssertion, long expiryTime) {
1452+
String clientAssertionType, String clientAssertion, long expiryTime, boolean openIdIssuer) {
14311453

14321454
StringBuilder body = new StringBuilder(256);
14331455
body.append("grant_type=client_credentials");
@@ -1472,6 +1494,10 @@ String generateAccessTokenRequestBody(String domainName, List<String> roleNames,
14721494
body.append("&client_assertion=").append(URLEncoder.encode(clientAssertion, StandardCharsets.UTF_8));
14731495
}
14741496

1497+
if (openIdIssuer) {
1498+
body.append("&openid_issuer=true");
1499+
}
1500+
14751501
return body.toString();
14761502
}
14771503

@@ -4160,4 +4186,137 @@ public CustomRequestRetryStrategy(int maxRetries, TimeValue defaultRetryInterval
41604186
Arrays.asList(429, 503));
41614187
}
41624188
}
4189+
4190+
/**
4191+
* Builder class for constructing access token requests with a fluent API.
4192+
*/
4193+
public static class AccessTokenRequestBuilder {
4194+
private final String domainName;
4195+
private List<String> roleNames;
4196+
private String idTokenServiceName;
4197+
private String proxyForPrincipal;
4198+
private String authorizationDetails;
4199+
private String proxyPrincipalSpiffeUris;
4200+
private String clientAssertionType;
4201+
private String clientAssertion;
4202+
private long expiryTime = 0;
4203+
private boolean ignoreCache = false;
4204+
private boolean openIdIssuer = false;
4205+
4206+
/**
4207+
* Set the list of role names for the access token request.
4208+
* @param roleNames list of role names
4209+
* @return this builder instance
4210+
*/
4211+
public AccessTokenRequestBuilder roleNames(List<String> roleNames) {
4212+
this.roleNames = roleNames;
4213+
return this;
4214+
}
4215+
4216+
/**
4217+
* Set the ID token service name for the access token request.
4218+
* @param idTokenServiceName the ID token service name
4219+
* @return this builder instance
4220+
*/
4221+
public AccessTokenRequestBuilder idTokenServiceName(String idTokenServiceName) {
4222+
this.idTokenServiceName = idTokenServiceName;
4223+
return this;
4224+
}
4225+
4226+
/**
4227+
* Set the proxy for principal for the access token request.
4228+
* @param proxyForPrincipal the proxy for principal
4229+
* @return this builder instance
4230+
*/
4231+
public AccessTokenRequestBuilder proxyForPrincipal(String proxyForPrincipal) {
4232+
this.proxyForPrincipal = proxyForPrincipal;
4233+
return this;
4234+
}
4235+
4236+
/**
4237+
* Set the authorization details for the access token request.
4238+
* @param authorizationDetails the authorization details
4239+
* @return this builder instance
4240+
*/
4241+
public AccessTokenRequestBuilder authorizationDetails(String authorizationDetails) {
4242+
this.authorizationDetails = authorizationDetails;
4243+
return this;
4244+
}
4245+
4246+
/**
4247+
* Set the proxy principal SPIFFE URIs for the access token request.
4248+
* @param proxyPrincipalSpiffeUris the proxy principal SPIFFE URIs
4249+
* @return this builder instance
4250+
*/
4251+
public AccessTokenRequestBuilder proxyPrincipalSpiffeUris(String proxyPrincipalSpiffeUris) {
4252+
this.proxyPrincipalSpiffeUris = proxyPrincipalSpiffeUris;
4253+
return this;
4254+
}
4255+
4256+
/**
4257+
* Set the client assertion type for the access token request.
4258+
* @param clientAssertionType the client assertion type
4259+
* @return this builder instance
4260+
*/
4261+
public AccessTokenRequestBuilder clientAssertionType(String clientAssertionType) {
4262+
this.clientAssertionType = clientAssertionType;
4263+
return this;
4264+
}
4265+
4266+
/**
4267+
* Set the client assertion for the access token request.
4268+
* @param clientAssertion the client assertion
4269+
* @return this builder instance
4270+
*/
4271+
public AccessTokenRequestBuilder clientAssertion(String clientAssertion) {
4272+
this.clientAssertion = clientAssertion;
4273+
return this;
4274+
}
4275+
4276+
/**
4277+
* Set the expiry time for the access token request.
4278+
* @param expiryTime expiry time in seconds (0 for server default)
4279+
* @return this builder instance
4280+
*/
4281+
public AccessTokenRequestBuilder expiryTime(long expiryTime) {
4282+
this.expiryTime = expiryTime;
4283+
return this;
4284+
}
4285+
4286+
/**
4287+
* Set whether to ignore cache for the access token request.
4288+
* @param ignoreCache true to ignore cache, false otherwise
4289+
* @return this builder instance
4290+
*/
4291+
public AccessTokenRequestBuilder ignoreCache(boolean ignoreCache) {
4292+
this.ignoreCache = ignoreCache;
4293+
return this;
4294+
}
4295+
4296+
/**
4297+
* Set whether to set the configured OpenID issuer for the access token request.
4298+
* @param openIdIssuer true to use the OpenID issuer, false otherwise
4299+
* @return this builder instance
4300+
*/
4301+
public AccessTokenRequestBuilder openIdIssuer(boolean openIdIssuer) {
4302+
this.openIdIssuer = openIdIssuer;
4303+
return this;
4304+
}
4305+
4306+
/**
4307+
* Create a new AccessTokenRequestBuilder instance.
4308+
* @param domainName the domain name (required)
4309+
* @return new builder instance
4310+
*/
4311+
public static AccessTokenRequestBuilder newBuilder(String domainName) {
4312+
return new AccessTokenRequestBuilder(domainName);
4313+
}
4314+
4315+
private AccessTokenRequestBuilder(String domainName) {
4316+
if (isEmpty(domainName)) {
4317+
throw new ZTSClientException(ClientResourceException.BAD_REQUEST, "Domain Name cannot be empty");
4318+
}
4319+
this.domainName = domainName;
4320+
}
4321+
}
41634322
}

0 commit comments

Comments
 (0)