package io.vertx.ext.auth.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.impl.ClusterSerializable;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.authorization.Authorization;
import io.vertx.ext.auth.authorization.AuthorizationContext;
import io.vertx.ext.auth.authorization.Authorizations;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.authorization.impl.AuthorizationContextImpl;
import io.vertx.ext.auth.authorization.impl.AuthorizationsImpl;
import java.util.Objects;
public class UserImpl implements User, ClusterSerializable {
private Authorizations authorizations;
private JsonObject attributes;
private JsonObject principal;
public UserImpl() {
}
public UserImpl(JsonObject principal) {
this(principal, new JsonObject());
}
public UserImpl(JsonObject principal, JsonObject attributes) {
this.principal = Objects.requireNonNull(principal);
this.attributes = attributes;
this.authorizations = new AuthorizationsImpl();
}
@Override
public Authorizations authorizations() {
return authorizations;
}
@Override
public JsonObject attributes() {
return attributes;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserImpl other = (UserImpl) obj;
return
Objects.equals(authorizations, other.authorizations) &&
Objects.equals(principal, other.principal) &&
Objects.equals(attributes, other.attributes);
}
@Override
public int hashCode() {
return Objects.hash(authorizations, principal, attributes);
}
@Override
public User isAuthorized(Authorization authorization, Handler<AsyncResult<Boolean>> resultHandler) {
Objects.requireNonNull(authorization);
Objects.requireNonNull(resultHandler);
AuthorizationContext context = new AuthorizationContextImpl(this);
resultHandler.handle(Future.succeededFuture(authorization.match(context)));
return this;
}
@Override
public JsonObject principal() {
return principal;
}
@Override
public void setAuthProvider(AuthProvider authProvider) {
}
@Override
public void writeToBuffer(Buffer buffer) {
UserConverter.encode(this).writeToBuffer(buffer);
}
@Override
public int readFromBuffer(int pos, Buffer buffer) {
JsonObject jsonObject = new JsonObject();
int read = jsonObject.readFromBuffer(pos, buffer);
User readUser = UserConverter.decode(jsonObject);
this.principal = readUser.principal();
this.authorizations = readUser.authorizations();
this.attributes = readUser.attributes();
return read;
}
}