UsernamePasswordAuthProvider extends CredentialsApiAuthProvider<UsernamePasswordCredentials> { @Override public UsernamePasswordCredentials getCredentials(final JsonObject authInfo) { final String username = JsonHelper.getValue(authInfo, "username", String.class, null); final String password = JsonHelper.getValue(authInfo, "password", String.class, null); if (username == null || password == null) { return null; } else if (password.isEmpty()) { return tryGetCredentialsEncodedInUsername(username); } else { return UsernamePasswordCredentials.create(username, password); } } @Autowired  UsernamePasswordAuthProvider(final CredentialsClientFactory credentialsClientFactory, final Tracer tracer); @Autowired  UsernamePasswordAuthProvider(
            final CredentialsClientFactory credentialsClientFactory,
            final HonoPasswordEncoder pwdEncoder,
            final Tracer tracer); @Override UsernamePasswordCredentials getCredentials(final JsonObject authInfo);  }
@Test public void testGetCredentialsRetrievesCredentialsFromUsername() { final String usernameIncludingPassword = "device" + "@" + Constants.DEFAULT_TENANT + ":" + "the-secret"; final String encodedUsername = Base64.getEncoder().encodeToString(usernameIncludingPassword.getBytes(StandardCharsets.UTF_8)); final JsonObject originalCredentials = (new JsonObject()).put("username", encodedUsername).put("password", ""); final UsernamePasswordCredentials credentials = provider.getCredentials(originalCredentials); assertThat(credentials.getAuthId()).isEqualTo("device"); assertThat(credentials.getTenantId()).isEqualTo(Constants.DEFAULT_TENANT); assertThat(credentials.getPassword()).isEqualTo("the-secret"); }
@Test public void testGetCredentialsHandlesEmptyAuthId() { final String usernameIncludingPassword = ":" + "the-secret"; final String encodedUsername = Base64.getEncoder().encodeToString(usernameIncludingPassword.getBytes(StandardCharsets.UTF_8)); final JsonObject originalCredentials = (new JsonObject()).put("username", encodedUsername).put("password", ""); final UsernamePasswordCredentials credentials = provider.getCredentials(originalCredentials); assertThat(credentials).isNull(); }
@Test public void testGetCredentialsHandlesEmptyPassword() { final String usernameIncludingPassword = "device" + "@" + Constants.DEFAULT_TENANT + ":"; final String encodedUsername = Base64.getEncoder().encodeToString(usernameIncludingPassword.getBytes(StandardCharsets.UTF_8)); final JsonObject originalCredentials = (new JsonObject()).put("username", encodedUsername).put("password", ""); final UsernamePasswordCredentials credentials = provider.getCredentials(originalCredentials); assertThat(credentials.getAuthId()).isEqualTo("device"); assertThat(credentials.getTenantId()).isEqualTo(Constants.DEFAULT_TENANT); assertThat(credentials.getPassword()).isEqualTo(""); }
@Test public void testGetCredentialsHandlesMalformedBase64InUsername() { final String usernameIncludingPassword = "device" + "@" + Constants.DEFAULT_TENANT + ":" + "the-secret"; final String encodedUsername = Base64.getEncoder().encodeToString(usernameIncludingPassword.getBytes(StandardCharsets.UTF_8)) + "not Base64"; final JsonObject originalCredentials = (new JsonObject()).put("username", encodedUsername).put("password", ""); final UsernamePasswordCredentials credentials = provider.getCredentials(originalCredentials); assertThat(credentials).isNull(); }