|
60 | 60 | import java.io.InputStream;
|
61 | 61 | import java.nio.charset.Charset;
|
62 | 62 | import java.security.PrivateKey;
|
| 63 | +import java.text.DateFormat; |
63 | 64 | import java.text.SimpleDateFormat;
|
| 65 | +import java.time.temporal.ChronoUnit; |
64 | 66 | import java.util.ArrayList;
|
65 | 67 | import java.util.Arrays;
|
66 | 68 | import java.util.Calendar;
|
67 | 69 | import java.util.Date;
|
68 | 70 | import java.util.List;
|
69 | 71 | import java.util.Map;
|
70 | 72 | import java.util.Set;
|
| 73 | +import java.util.TimeZone; |
71 | 74 | import org.junit.jupiter.api.BeforeEach;
|
72 | 75 | import org.junit.jupiter.api.Test;
|
73 | 76 |
|
@@ -118,7 +121,7 @@ class ImpersonatedCredentialsTest extends BaseSerializationTest {
|
118 | 121 | private static final int INVALID_LIFETIME = 43210;
|
119 | 122 | private static JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
120 | 123 |
|
121 |
| - private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; |
| 124 | + private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ssX"; |
122 | 125 | public static final String DEFAULT_IMPERSONATION_URL =
|
123 | 126 | "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/"
|
124 | 127 | + IMPERSONATED_CLIENT_EMAIL
|
@@ -562,6 +565,56 @@ void refreshAccessToken_delegates_success() throws IOException, IllegalStateExce
|
562 | 565 | assertEquals(ACCESS_TOKEN, targetCredentials.refreshAccessToken().getTokenValue());
|
563 | 566 | }
|
564 | 567 |
|
| 568 | + @Test |
| 569 | + void refreshAccessToken_GMT_dateParsedCorrectly() throws IOException, IllegalStateException { |
| 570 | + Calendar c = Calendar.getInstance(); |
| 571 | + c.add(Calendar.SECOND, VALID_LIFETIME); |
| 572 | + |
| 573 | + mockTransportFactory.transport.setTargetPrincipal(IMPERSONATED_CLIENT_EMAIL); |
| 574 | + mockTransportFactory.transport.setAccessToken(ACCESS_TOKEN); |
| 575 | + mockTransportFactory.transport.setExpireTime(getFormattedTime(c.getTime())); |
| 576 | + ImpersonatedCredentials targetCredentials = |
| 577 | + ImpersonatedCredentials.create( |
| 578 | + sourceCredentials, |
| 579 | + IMPERSONATED_CLIENT_EMAIL, |
| 580 | + null, |
| 581 | + IMMUTABLE_SCOPES_LIST, |
| 582 | + VALID_LIFETIME, |
| 583 | + mockTransportFactory) |
| 584 | + .createWithCustomCalendar( |
| 585 | + // Set system timezone to GMT |
| 586 | + Calendar.getInstance(TimeZone.getTimeZone("GMT"))); |
| 587 | + |
| 588 | + assertEquals( |
| 589 | + c.getTime().toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli(), |
| 590 | + targetCredentials.refreshAccessToken().getExpirationTimeMillis()); |
| 591 | + } |
| 592 | + |
| 593 | + @Test |
| 594 | + void refreshAccessToken_nonGMT_dateParsedCorrectly() throws IOException, IllegalStateException { |
| 595 | + Calendar c = Calendar.getInstance(); |
| 596 | + c.add(Calendar.SECOND, VALID_LIFETIME); |
| 597 | + |
| 598 | + mockTransportFactory.transport.setTargetPrincipal(IMPERSONATED_CLIENT_EMAIL); |
| 599 | + mockTransportFactory.transport.setAccessToken(ACCESS_TOKEN); |
| 600 | + mockTransportFactory.transport.setExpireTime(getFormattedTime(c.getTime())); |
| 601 | + ImpersonatedCredentials targetCredentials = |
| 602 | + ImpersonatedCredentials.create( |
| 603 | + sourceCredentials, |
| 604 | + IMPERSONATED_CLIENT_EMAIL, |
| 605 | + null, |
| 606 | + IMMUTABLE_SCOPES_LIST, |
| 607 | + VALID_LIFETIME, |
| 608 | + mockTransportFactory) |
| 609 | + .createWithCustomCalendar( |
| 610 | + // Set system timezone to one different than GMT |
| 611 | + Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"))); |
| 612 | + |
| 613 | + assertEquals( |
| 614 | + c.getTime().toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli(), |
| 615 | + targetCredentials.refreshAccessToken().getExpirationTimeMillis()); |
| 616 | + } |
| 617 | + |
565 | 618 | @Test
|
566 | 619 | void refreshAccessToken_invalidDate() throws IllegalStateException {
|
567 | 620 |
|
@@ -926,7 +979,19 @@ void serialize() throws IOException, ClassNotFoundException {
|
926 | 979 | public static String getDefaultExpireTime() {
|
927 | 980 | Calendar c = Calendar.getInstance();
|
928 | 981 | c.add(Calendar.SECOND, VALID_LIFETIME);
|
929 |
| - return new SimpleDateFormat(RFC3339).format(c.getTime()); |
| 982 | + return getFormattedTime(c.getTime()); |
| 983 | + } |
| 984 | + |
| 985 | + /** |
| 986 | + * Given a {@link Date}, it will return a string of the date formatted like |
| 987 | + * <b>yyyy-MM-dd'T'HH:mm:ss'Z'</b> |
| 988 | + */ |
| 989 | + private static String getFormattedTime(final Date date) { |
| 990 | + // Set timezone to GMT since that's the TZ used in the response from the service impersonation |
| 991 | + // token exchange |
| 992 | + final DateFormat formatter = new SimpleDateFormat(RFC3339); |
| 993 | + formatter.setTimeZone(TimeZone.getTimeZone("GMT")); |
| 994 | + return formatter.format(date); |
930 | 995 | }
|
931 | 996 |
|
932 | 997 | private String generateErrorJson(
|
|
0 commit comments