Skip to content

Commit e200fa2

Browse files
committed
feat: support idtoken credentials
1 parent e0405a5 commit e200fa2

File tree

6 files changed

+146
-67
lines changed

6 files changed

+146
-67
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/RoaAcsRequest.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,33 +184,40 @@ public HttpRequest signRequest(Signer signer, AlibabaCloudCredentials credential
184184
imutableMap.put("RegionId", getSysRegionId());
185185
}
186186
if (null != signer && null != credentials && !(credentials instanceof AnonymousCredentials)) {
187-
String accessKeyId = credentials.getAccessKeyId();
188-
if (credentials instanceof BasicSessionCredentials) {
189-
String sessionToken = ((BasicSessionCredentials) credentials).getSessionToken();
190-
if (null != sessionToken) {
191-
imutableMap.put("x-acs-accesskey-id", accessKeyId);
192-
imutableMap.put("x-acs-security-token", sessionToken);
193-
}
194-
}
195187
if (credentials instanceof BearerTokenCredentials) {
196188
String bearerToken = ((BearerTokenCredentials) credentials).getBearerToken();
197-
if (null != ((BearerTokenCredentials) credentials).getBearerToken()) {
189+
if (null != bearerToken) {
198190
imutableMap.put("x-acs-bearer-token", bearerToken);
191+
imutableMap.put("x-acs-signature-type", "BEARERTOKEN");
199192
}
193+
} else if (credentials instanceof IDTokenCredentials) {
194+
String idToken = ((IDTokenCredentials) credentials).getIDToken();
195+
if (null != idToken) {
196+
imutableMap.put("x-acs-zero-trust-idtoken", idToken);
197+
}
198+
} else {
199+
String accessKeyId = credentials.getAccessKeyId();
200+
if (credentials instanceof BasicSessionCredentials) {
201+
String sessionToken = ((BasicSessionCredentials) credentials).getSessionToken();
202+
if (null != sessionToken) {
203+
imutableMap.put("x-acs-accesskey-id", accessKeyId);
204+
imutableMap.put("x-acs-security-token", sessionToken);
205+
}
206+
}
207+
if (signer.getContent() != null && hashedRequestPayload != null) {
208+
imutableMap.put(signer.getContent(), hashedRequestPayload);
209+
}
210+
String strToSign = this.composer.composeStringToSign(this.getSysMethod(), this.getSysUriPattern(), signer,
211+
this.getSysQueryParameters(), imutableMap, this.getPathParameters());
212+
if (this.getSysSignatureVersion() == SignatureVersion.V3) {
213+
strToSign += "\n" + hashedRequestPayload;
214+
strToSign = signer.getSignerName() + "\n" + hexEncode(signer.hash(strToSign.getBytes("UTF-8")));
215+
}
216+
this.strToSign = strToSign;
217+
String signature = signer.signString(strToSign, credentials);
218+
imutableMap.put("Authorization", this.composer.getAuthorization(signer, accessKeyId, signature)
219+
+ (this.getSysSignatureVersion() == SignatureVersion.V3 ? ",SignedHeaders=" + this.getSysSignedHeaders(imutableMap) : ""));
200220
}
201-
if (signer.getContent() != null && hashedRequestPayload != null) {
202-
imutableMap.put(signer.getContent(), hashedRequestPayload);
203-
}
204-
String strToSign = this.composer.composeStringToSign(this.getSysMethod(), this.getSysUriPattern(), signer,
205-
this.getSysQueryParameters(), imutableMap, this.getPathParameters());
206-
if (this.getSysSignatureVersion() == SignatureVersion.V3) {
207-
strToSign += "\n" + hashedRequestPayload;
208-
strToSign = signer.getSignerName() + "\n" + hexEncode(signer.hash(strToSign.getBytes("UTF-8")));
209-
}
210-
this.strToSign = strToSign;
211-
String signature = signer.signString(strToSign, credentials);
212-
imutableMap.put("Authorization", this.composer.getAuthorization(signer, accessKeyId, signature)
213-
+ (this.getSysSignatureVersion() == SignatureVersion.V3 ? ",SignedHeaders=" + this.getSysSignedHeaders(imutableMap) : ""));
214221
}
215222
this.setSysUrl(this.composeUrl(domain.getDomainName(), this.getSysQueryParameters()));
216223
this.headers = imutableMap;

aliyun-java-sdk-core/src/main/java/com/aliyuncs/RpcAcsRequest.java

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -172,51 +172,58 @@ public HttpRequest signRequest(Signer signer, AlibabaCloudCredentials credential
172172
}
173173
}
174174
if (null != signer && null != credentials && !(credentials instanceof AnonymousCredentials)) {
175-
String accessKeyId = credentials.getAccessKeyId();
176-
String accessSecret = credentials.getAccessKeySecret();
177-
if (credentials instanceof BasicSessionCredentials) {
178-
String sessionToken = ((BasicSessionCredentials) credentials).getSessionToken();
179-
if (null != sessionToken) {
180-
imutableMap.put("SecurityToken", sessionToken);
181-
headerMap.put("x-acs-security-token", sessionToken);
182-
}
183-
}
184175
if (credentials instanceof BearerTokenCredentials) {
185176
String bearerToken = ((BearerTokenCredentials) credentials).getBearerToken();
186-
if (null != ((BearerTokenCredentials) credentials).getBearerToken()) {
177+
if (null != bearerToken) {
187178
imutableMap.put("BearerToken", bearerToken);
179+
imutableMap.put("SignatureType", "BEARERTOKEN");
188180
headerMap.put("x-acs-bearer-token", bearerToken);
189181
}
190-
}
191-
if (signer.getContent() != null && hashedRequestPayload != null) {
192-
headerMap.put(signer.getContent(), hashedRequestPayload);
193-
}
194-
imutableMap.put("AccessKeyId", accessKeyId);
195-
if (this.getSysSignatureVersion() == SignatureVersion.V3) {
196-
String strToSign = this.composer.composeStringToSign(this.getSysMethod(), null, signer,
197-
this.getSysQueryParameters(), headerMap, null) + "\n" + hashedRequestPayload;
198-
this.strToSign = strToSign;
199-
strToSign = signer.getSignerName() + "\n" + hexEncode(signer.hash(strToSign.getBytes("UTF-8")));
200-
String signature = signer.signString(strToSign, accessSecret);
201-
headerMap.put("Authorization", this.composer.getAuthorization(signer, accessKeyId, signature)
202-
+ ",SignedHeaders=" + this.getSysSignedHeaders(headerMap));
203-
imutableMap = this.getSysQueryParameters();
182+
} else if (credentials instanceof IDTokenCredentials) {
183+
String idToken = ((IDTokenCredentials) credentials).getIDToken();
184+
if (null != idToken) {
185+
headerMap.put("x-acs-zero-trust-idtoken", idToken);
186+
}
204187
} else {
205-
Map<String, String> paramsToSign = new HashMap<String, String>();
206-
paramsToSign.putAll(bodyParams);
207-
paramsToSign.putAll(imutableMap);
208-
String strToSign = this.composer.composeStringToSign(
209-
this.getSysMethod(), null, signer, paramsToSign, null, null);
210-
this.strToSign = strToSign;
211-
String signature;
212-
if (credentials instanceof KeyPairCredentials) {
213-
signature = signer.signString(strToSign, credentials);
188+
String accessKeyId = credentials.getAccessKeyId();
189+
String accessSecret = credentials.getAccessKeySecret();
190+
if (credentials instanceof BasicSessionCredentials) {
191+
String sessionToken = ((BasicSessionCredentials) credentials).getSessionToken();
192+
if (null != sessionToken) {
193+
imutableMap.put("SecurityToken", sessionToken);
194+
headerMap.put("x-acs-security-token", sessionToken);
195+
}
196+
}
197+
if (signer.getContent() != null && hashedRequestPayload != null) {
198+
headerMap.put(signer.getContent(), hashedRequestPayload);
199+
}
200+
imutableMap.put("AccessKeyId", accessKeyId);
201+
if (this.getSysSignatureVersion() == SignatureVersion.V3) {
202+
String strToSign = this.composer.composeStringToSign(this.getSysMethod(), null, signer,
203+
this.getSysQueryParameters(), headerMap, null) + "\n" + hashedRequestPayload;
204+
this.strToSign = strToSign;
205+
strToSign = signer.getSignerName() + "\n" + hexEncode(signer.hash(strToSign.getBytes("UTF-8")));
206+
String signature = signer.signString(strToSign, accessSecret);
207+
headerMap.put("Authorization", this.composer.getAuthorization(signer, accessKeyId, signature)
208+
+ ",SignedHeaders=" + this.getSysSignedHeaders(headerMap));
209+
imutableMap = this.getSysQueryParameters();
214210
} else {
215-
signature = signer.signString(strToSign, accessSecret + "&");
211+
Map<String, String> paramsToSign = new HashMap<String, String>();
212+
paramsToSign.putAll(bodyParams);
213+
paramsToSign.putAll(imutableMap);
214+
String strToSign = this.composer.composeStringToSign(
215+
this.getSysMethod(), null, signer, paramsToSign, null, null);
216+
this.strToSign = strToSign;
217+
String signature;
218+
if (credentials instanceof KeyPairCredentials) {
219+
signature = signer.signString(strToSign, credentials);
220+
} else {
221+
signature = signer.signString(strToSign, accessSecret + "&");
222+
}
223+
imutableMap.put("Signature", this.composer.getAuthorization(signer, accessKeyId, signature));
224+
headerMap.clear();
225+
headerMap.putAll(this.getSysHeaders());
216226
}
217-
imutableMap.put("Signature", this.composer.getAuthorization(signer, accessKeyId, signature));
218-
headerMap.clear();
219-
headerMap.putAll(this.getSysHeaders());
220227
}
221228
}
222229
this.setSysUrl(this.composeUrl(domain.getDomainName(), imutableMap));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.aliyuncs.auth;
2+
3+
public class IDTokenCredentials implements AlibabaCloudCredentials {
4+
private String idToken;
5+
6+
public IDTokenCredentials(String idToken) {
7+
this.idToken = idToken;
8+
}
9+
10+
@Override
11+
public String getAccessKeyId() {
12+
return null;
13+
}
14+
15+
@Override
16+
public String getAccessKeySecret() {
17+
return null;
18+
}
19+
20+
public String getIDToken() {
21+
return idToken;
22+
}
23+
24+
public void setIDToken(String idToken) {
25+
this.idToken = idToken;
26+
}
27+
}

aliyun-java-sdk-core/src/test/java/com/aliyuncs/CommonRoaRequestTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77
import java.util.Map;
88

9+
import com.aliyuncs.auth.*;
910
import org.junit.Assert;
1011
import org.junit.Test;
1112
import org.junit.runner.RunWith;
@@ -15,11 +16,7 @@
1516
import org.powermock.core.classloader.annotations.PrepareForTest;
1617
import org.powermock.modules.junit4.PowerMockRunner;
1718

18-
import com.aliyuncs.auth.AlibabaCloudCredentials;
19-
import com.aliyuncs.auth.BasicSessionCredentials;
20-
import com.aliyuncs.auth.BearerTokenCredentials;
2119
import com.aliyuncs.auth.signers.HmacSHA1Signer;
22-
import com.aliyuncs.auth.RoaSignatureComposer;
2320
import com.aliyuncs.http.FormatType;
2421
import com.aliyuncs.http.HttpRequest;
2522
import com.aliyuncs.http.MethodType;
@@ -180,6 +177,14 @@ public void testSignRequest()
180177
Mockito.when(bearerCredentials.getBearerToken()).thenReturn("bearerToken");
181178
Assert.assertTrue(
182179
request.signRequest(signer, bearerCredentials, FormatType.JSON, productDomain) instanceof HttpRequest);
180+
181+
IDTokenCredentials idTokenCredentials = Mockito.mock(IDTokenCredentials.class);
182+
Mockito.when(idTokenCredentials.getIDToken()).thenReturn(null);
183+
Assert.assertTrue(
184+
request.signRequest(signer, idTokenCredentials, FormatType.JSON, productDomain) instanceof HttpRequest);
185+
Mockito.when(idTokenCredentials.getIDToken()).thenReturn("token");
186+
Assert.assertTrue(
187+
request.signRequest(signer, idTokenCredentials, FormatType.JSON, productDomain) instanceof HttpRequest);
183188
}
184189

185190
}

aliyun-java-sdk-core/src/test/java/com/aliyuncs/CommonRpcRequestTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.aliyuncs;
22

3-
import com.aliyuncs.auth.BasicSessionCredentials;
4-
import com.aliyuncs.auth.BearerTokenCredentials;
5-
import com.aliyuncs.auth.KeyPairCredentials;
6-
import com.aliyuncs.auth.Signer;
3+
import com.aliyuncs.auth.*;
74
import com.aliyuncs.http.FormatType;
85
import com.aliyuncs.http.ProtocolType;
96
import com.aliyuncs.http.UserAgentConfig;
@@ -245,6 +242,8 @@ public void signRequestTest() throws UnsupportedEncodingException, InvalidKeyExc
245242
commonRpcRequest.setHttpContentType(FormatType.JSON);
246243
commonRpcRequest.signRequest(signer, bearerTokenCredentials, FormatType.JSON, domain);
247244
Assert.assertTrue(commonRpcRequest.getSysUrl().contains("BearerToken=token"));
245+
Assert.assertTrue(commonRpcRequest.getSysUrl().contains("SignatureType=BEARERTOKEN"));
246+
Assert.assertEquals("token", commonRpcRequest.getSysHeaders().get("x-acs-bearer-token"));
248247
Map<String, String> map = commonRpcRequest.getSysBodyParameters();
249248
Assert.assertEquals("test", map.get("test"));
250249

@@ -253,6 +252,15 @@ public void signRequestTest() throws UnsupportedEncodingException, InvalidKeyExc
253252
commonRpcRequest.signRequest(signer, bearerTokenCredentials, FormatType.JSON, domain);
254253
Assert.assertFalse(commonRpcRequest.getSysUrl().contains("BearerToken=token"));
255254

255+
commonRpcRequest = new CommonRpcRequest("test");
256+
commonRpcRequest.setSysProtocol(ProtocolType.HTTP);
257+
IDTokenCredentials idTokenCredentials = mock(IDTokenCredentials.class);
258+
when(idTokenCredentials.getIDToken()).thenReturn("token");
259+
commonRpcRequest.putBodyParameter("test", "test");
260+
commonRpcRequest.setHttpContentType(FormatType.JSON);
261+
commonRpcRequest.signRequest(signer, idTokenCredentials, FormatType.JSON, domain);
262+
Assert.assertEquals("token", commonRpcRequest.getSysHeaders().get("x-acs-zero-trust-idtoken"));
263+
256264
commonRpcRequest = new CommonRpcRequest("test");
257265
commonRpcRequest.setSysProtocol(ProtocolType.HTTP);
258266
KeyPairCredentials keyPairCredentials = mock(KeyPairCredentials.class);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.aliyuncs.auth;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class IDTokenCredentialsTest {
7+
@Test
8+
public void constructorTest() {
9+
String token = "token";
10+
IDTokenCredentials credentials = new IDTokenCredentials(token);
11+
Assert.assertEquals(token, credentials.getIDToken());
12+
Assert.assertNull(credentials.getAccessKeyId());
13+
Assert.assertNull(credentials.getAccessKeySecret());
14+
}
15+
16+
@Test
17+
public void setIDTokenTest() {
18+
String token = "token";
19+
IDTokenCredentials credentials = new IDTokenCredentials(token);
20+
Assert.assertEquals(token, credentials.getIDToken());
21+
String newToken = "new Token";
22+
credentials.setIDToken(newToken);
23+
Assert.assertEquals(newToken, credentials.getIDToken());
24+
}
25+
}

0 commit comments

Comments
 (0)