package io.vertx.ext.auth.authentication;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
@DataObject(generateConverter = true, publicConverter = false)
public class TokenCredentials implements Credentials {
private String token;
protected TokenCredentials() {}
public TokenCredentials(String token) {
this.token = token;
}
public TokenCredentials(JsonObject jsonObject) {
TokenCredentialsConverter.fromJson(jsonObject, this);
}
public String getToken() {
return token;
}
public TokenCredentials setToken(String token) {
this.token = token;
return this;
}
@Override
public <V> void checkValid(V arg) throws CredentialValidationException {
if (token == null || token.length() == 0) {
throw new CredentialValidationException("token cannot be null or empty");
}
}
public JsonObject toJson() {
JsonObject result = new JsonObject();
TokenCredentialsConverter.toJson(this, result);
return result;
}
@Override
public String toString() {
return toJson().encode();
}
@Override
public TokenCredentials applyHttpChallenge(String challenge, HttpMethod method, String uri, Integer nc, String cnonce) throws CredentialValidationException {
if (challenge != null) {
int spc = challenge.indexOf(' ');
if (!"Bearer".equalsIgnoreCase(challenge.substring(0, spc))) {
throw new IllegalArgumentException("Only 'Bearer' auth-scheme is supported");
}
}
checkValid(null);
return this;
}
@Override
public String toHttpAuthorization() {
return "Bearer " + token;
}
}