package examples;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.*;
import io.vertx.ext.auth.authentication.AuthenticationProvider;
import io.vertx.ext.auth.authorization.AuthorizationProvider;
import io.vertx.ext.auth.authorization.PermissionBasedAuthorization;
import io.vertx.ext.auth.authorization.RoleBasedAuthorization;
public class AuthCommonExamples {
public void example1(AuthenticationProvider authProvider) {
JsonObject authInfo = new JsonObject()
.put("username", "tim").put("password", "mypassword");
authProvider.authenticate(authInfo)
.onSuccess(user -> {
System.out.println("User " + user.principal() + " is now authenticated");
})
.onFailure(Throwable::printStackTrace);
}
public void example2(User user, AuthorizationProvider authorizationProvider) {
authorizationProvider.getAuthorizations(user)
.onSuccess(done -> {
if (PermissionBasedAuthorization.create("printer1234").match(user)) {
System.out.println("User has the authority");
} else {
System.out.println("User does not have the authority");
}
});
}
public void example3(User user, AuthorizationProvider authorizationProvider) {
authorizationProvider.getAuthorizations(user)
.onSuccess(done -> {
if (RoleBasedAuthorization.create("admin").match(user)) {
System.out.println("User has the authority");
} else {
System.out.println("User does not have the authority");
}
});
}
public void example4(Vertx vertx) {
String token = VertxContextPRNG.current(vertx).nextString(32);
int randomInt = VertxContextPRNG.current(vertx).nextInt();
}
public void example5() {
KeyStoreOptions options = new KeyStoreOptions()
.setPath("/path/to/keystore/file")
.setType("pkcs8")
.setPassword("keystore-password")
.putPasswordProtection("key-alias", "alias-password");
}
public void example6(Vertx vertx) {
PubSecKeyOptions options = new PubSecKeyOptions()
.setAlgorithm("RS256")
.setBuffer(
vertx.fileSystem()
.readFileBlocking("/path/to/pem/file")
.toString());
}
public void example7(Vertx vertx, AuthenticationProvider ldapAuthProvider, AuthenticationProvider propertiesAuthProvider) {
ChainAuth.any()
.add(ldapAuthProvider)
.add(propertiesAuthProvider);
}
public void example8(Vertx vertx, AuthenticationProvider ldapAuthProvider, AuthenticationProvider propertiesAuthProvider) {
ChainAuth.all()
.add(ldapAuthProvider)
.add(propertiesAuthProvider);
}
public void example9(User user) {
if (user.containsKey("sub")) {
String sub = user.get("sub");
}
}
}