package examples;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.VertxContextPRNG;
public class AuthCommonExamples {
public void example1(AuthProvider authProvider) {
JsonObject authInfo = new JsonObject().put("username", "tim").put("password", "mypassword");
authProvider.authenticate(authInfo, res -> {
if (res.succeeded()) {
User user = res.result();
System.out.println("User " + user.principal() + " is now authenticated");
} else {
res.cause().printStackTrace();
}
});
}
public void example2(User user) {
user.isAuthorized("printers:printer1234", res -> {
if (res.succeeded()) {
boolean hasAuthority = res.result();
if (hasAuthority) {
System.out.println("User has the authority");
} else {
System.out.println("User does not have the authority");
}
} else {
res.cause().printStackTrace();
}
});
}
public void example3(User user) {
user.isAuthorized("role:admin", res -> {
if (res.succeeded()) {
boolean hasAuthority = res.result();
if (hasAuthority) {
System.out.println("User has the authority to the role of admin");
} else {
System.out.println("User does not have the authority");
}
} else {
res.cause().printStackTrace();
}
});
}
public void example4(Vertx vertx) {
String token = VertxContextPRNG.current(vertx).nextString(32);
int randomInt = VertxContextPRNG.current(vertx).nextInt();
}
}