private void processAuthentication(RequestContext context, AuthCredentialsServiceState authState) { if (authState != null) { String authorizationHeaderValue = AuthUtils.createAuthorizationHeader(authState); if (authorizationHeaderValue != null) { context.request.customProperties.put(AUTHORIZATION_HEADER, authorizationHeaderValue); } } }
@Test public void testCreateAuthorizationHeader() { // No credentials assertNull(AuthUtils.createAuthorizationHeader(null)); // Non-password credentials AuthCredentialsServiceState credentials = new AuthCredentialsServiceState(); credentials.type = AuthCredentialsType.PublicKey.toString(); assertNull(AuthUtils.createAuthorizationHeader(credentials)); // Password credentials String email = "test@test.test"; String password = "test"; String expectedHeader = String.format("Basic %s", new String(Base64.getEncoder() .encode(String.format("%s:%s", email, password).getBytes()))); credentials = new AuthCredentialsServiceState(); credentials.type = AuthCredentialsType.Password.toString(); credentials.userEmail = email; credentials.privateKey = password; assertEquals(expectedHeader, AuthUtils.createAuthorizationHeader(credentials)); // Bearer token String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL"; expectedHeader = String.format("Bearer %s", token); credentials = new AuthCredentialsServiceState(); credentials.type = "Bearer"; credentials.privateKey = token; assertEquals(expectedHeader, AuthUtils.createAuthorizationHeader(credentials)); }
private void prepareRequest(Operation op, KubernetesContext context) { String authorizationHeaderValue = AuthUtils.createAuthorizationHeader(context.credentials); if (authorizationHeaderValue != null) { op.addRequestHeader(Operation.AUTHORIZATION_HEADER, authorizationHeaderValue); } createOrUpdateTargetSsl(context); op.setReferer(host.getPublicUri()); op.forceRemote(); setConnectionTag(context.credentials, op); if (op.getExpirationMicrosUtc() == 0) { long timeout = TimeUnit.SECONDS.toMicros(REQUEST_TIMEOUT_SECONDS); op.setExpiration(ServiceUtils.getExpirationTimeFromNowInMicros(timeout)); } }