1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * * Neither the name of Google LLC nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package com.google.auth.oauth2; 33 34 import static com.google.auth.oauth2.OAuth2Credentials.DEFAULT_EXPIRATION_MARGIN; 35 import static com.google.auth.oauth2.OAuth2Credentials.DEFAULT_REFRESH_MARGIN; 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.fail; 38 39 import com.google.auth.TestUtils; 40 import java.io.IOException; 41 import java.net.URI; 42 import java.time.Duration; 43 import java.time.temporal.ChronoUnit; 44 import java.util.Date; 45 import java.util.List; 46 import java.util.Map; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.junit.runners.JUnit4; 50 51 /** Tests for {@link OAuth2CredentialsWithRefresh}. */ 52 @RunWith(JUnit4.class) 53 public class OAuth2CredentialsWithRefreshTest { 54 private static final AccessToken ACCESS_TOKEN = new AccessToken("accessToken", new Date()); 55 56 @Test builder()57 public void builder() { 58 OAuth2CredentialsWithRefresh.OAuth2RefreshHandler refreshHandler = 59 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 60 @Override 61 public AccessToken refreshAccessToken() { 62 return null; 63 } 64 }; 65 OAuth2CredentialsWithRefresh credential = 66 OAuth2CredentialsWithRefresh.newBuilder() 67 .setAccessToken(ACCESS_TOKEN) 68 .setRefreshHandler(refreshHandler) 69 .build(); 70 71 assertEquals(ACCESS_TOKEN, credential.getAccessToken()); 72 assertEquals(refreshHandler, credential.getRefreshHandler()); 73 } 74 75 @Test builder_withRefreshAndExpirationMargins()76 public void builder_withRefreshAndExpirationMargins() { 77 OAuth2CredentialsWithRefresh.OAuth2RefreshHandler refreshHandler = 78 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 79 @Override 80 public AccessToken refreshAccessToken() { 81 return null; 82 } 83 }; 84 85 Duration refreshMargin = Duration.of(10, ChronoUnit.SECONDS); 86 Duration expirationMargin = Duration.of(12, ChronoUnit.SECONDS); 87 88 OAuth2CredentialsWithRefresh credential = 89 (OAuth2CredentialsWithRefresh) 90 OAuth2CredentialsWithRefresh.newBuilder() 91 .setAccessToken(ACCESS_TOKEN) 92 .setRefreshHandler(refreshHandler) 93 .setRefreshMargin(refreshMargin) 94 .setExpirationMargin(expirationMargin) 95 .build(); 96 97 assertEquals(ACCESS_TOKEN, credential.getAccessToken()); 98 assertEquals(refreshMargin, credential.getRefreshMargin()); 99 assertEquals(expirationMargin, credential.getExpirationMargin()); 100 assertEquals(refreshHandler, credential.getRefreshHandler()); 101 } 102 103 @Test builder_onlyRefreshMarginSet()104 public void builder_onlyRefreshMarginSet() { 105 OAuth2CredentialsWithRefresh.OAuth2RefreshHandler refreshHandler = 106 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 107 @Override 108 public AccessToken refreshAccessToken() { 109 return null; 110 } 111 }; 112 113 Duration refreshMargin = Duration.of(10, ChronoUnit.SECONDS); 114 115 OAuth2CredentialsWithRefresh credential = 116 (OAuth2CredentialsWithRefresh) 117 OAuth2CredentialsWithRefresh.newBuilder() 118 .setAccessToken(ACCESS_TOKEN) 119 .setRefreshHandler(refreshHandler) 120 .setRefreshMargin(refreshMargin) 121 .build(); 122 123 assertEquals(ACCESS_TOKEN, credential.getAccessToken()); 124 assertEquals(refreshMargin, credential.getRefreshMargin()); 125 assertEquals(DEFAULT_EXPIRATION_MARGIN, credential.getExpirationMargin()); 126 assertEquals(refreshHandler, credential.getRefreshHandler()); 127 } 128 129 @Test builder_onlyExpirationMarginSet()130 public void builder_onlyExpirationMarginSet() { 131 OAuth2CredentialsWithRefresh.OAuth2RefreshHandler refreshHandler = 132 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 133 @Override 134 public AccessToken refreshAccessToken() { 135 return null; 136 } 137 }; 138 139 Duration expirationMargin = Duration.of(12, ChronoUnit.SECONDS); 140 OAuth2CredentialsWithRefresh credential = 141 (OAuth2CredentialsWithRefresh) 142 OAuth2CredentialsWithRefresh.newBuilder() 143 .setAccessToken(ACCESS_TOKEN) 144 .setRefreshHandler(refreshHandler) 145 .setExpirationMargin(expirationMargin) 146 .build(); 147 148 assertEquals(ACCESS_TOKEN, credential.getAccessToken()); 149 assertEquals(DEFAULT_REFRESH_MARGIN, credential.getRefreshMargin()); 150 assertEquals(expirationMargin, credential.getExpirationMargin()); 151 assertEquals(refreshHandler, credential.getRefreshHandler()); 152 } 153 154 @Test builder_noAccessToken()155 public void builder_noAccessToken() { 156 OAuth2CredentialsWithRefresh.newBuilder() 157 .setRefreshHandler( 158 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 159 @Override 160 public AccessToken refreshAccessToken() { 161 return null; 162 } 163 }) 164 .build(); 165 } 166 167 @Test builder_noRefreshHandler_throws()168 public void builder_noRefreshHandler_throws() { 169 try { 170 OAuth2CredentialsWithRefresh.newBuilder().setAccessToken(ACCESS_TOKEN).build(); 171 fail("Should fail as a refresh handler must be provided."); 172 } catch (NullPointerException e) { 173 // Expected. 174 } 175 } 176 177 @Test builder_noExpirationTimeInAccessToken_throws()178 public void builder_noExpirationTimeInAccessToken_throws() { 179 try { 180 OAuth2CredentialsWithRefresh.newBuilder() 181 .setAccessToken(new AccessToken("accessToken", null)) 182 .build(); 183 fail("Should fail as a refresh handler must be provided."); 184 } catch (IllegalArgumentException e) { 185 // Expected. 186 } 187 } 188 189 @Test refreshAccessToken_delegateToRefreshHandler()190 public void refreshAccessToken_delegateToRefreshHandler() throws IOException { 191 final AccessToken refreshedToken = new AccessToken("refreshedAccessToken", new Date()); 192 OAuth2CredentialsWithRefresh credentials = 193 OAuth2CredentialsWithRefresh.newBuilder() 194 .setAccessToken(ACCESS_TOKEN) 195 .setRefreshHandler( 196 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 197 @Override 198 public AccessToken refreshAccessToken() { 199 return refreshedToken; 200 } 201 }) 202 .build(); 203 204 AccessToken accessToken = credentials.refreshAccessToken(); 205 206 assertEquals(refreshedToken, accessToken); 207 } 208 209 @Test getRequestMetadata()210 public void getRequestMetadata() throws IOException { 211 URI uri = URI.create("http://googleapis.com/testapi/v1/foo"); 212 final AccessToken refreshedToken = new AccessToken("refreshedAccessToken", new Date()); 213 OAuth2CredentialsWithRefresh credentials = 214 OAuth2CredentialsWithRefresh.newBuilder() 215 .setAccessToken(ACCESS_TOKEN) 216 .setRefreshHandler( 217 new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { 218 @Override 219 public AccessToken refreshAccessToken() { 220 return refreshedToken; 221 } 222 }) 223 .build(); 224 225 Map<String, List<String>> metadata = credentials.getRequestMetadata(uri); 226 227 TestUtils.assertContainsBearerToken(metadata, refreshedToken.getTokenValue()); 228 } 229 } 230