com.arangodb.shaded.vertx.ext.auth.impl.UserConverter Maven / Gradle / Ivy
/********************************************************************************
* Copyright (c) 2019 Stephane Bastian
*
* This program and the accompanying materials are made available under the 2
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0 3
*
* Contributors: 4
* Stephane Bastian - initial API and implementation
********************************************************************************/
package com.arangodb.shaded.vertx.ext.auth.impl;
import java.util.Objects;
import com.arangodb.shaded.vertx.core.json.JsonArray;
import com.arangodb.shaded.vertx.core.json.JsonObject;
import com.arangodb.shaded.vertx.ext.auth.authorization.Authorization;
import com.arangodb.shaded.vertx.ext.auth.User;
import com.arangodb.shaded.vertx.ext.auth.authorization.impl.AuthorizationConverter;
public class UserConverter {
private final static String FIELD_PRINCIPAL = "principal";
private final static String FIELD_AUTHORIZATIONS = "authorizations";
private final static String FIELD_ATTRIBUTES = "attributes";
public static JsonObject encode(User value) throws IllegalArgumentException {
Objects.requireNonNull(value);
JsonObject json = new JsonObject();
json.put(FIELD_PRINCIPAL, value.principal());
JsonObject jsonAuthorizations = new JsonObject();
for (String providerId: value.authorizations().getProviderIds()) {
JsonArray jsonAuthorizationByProvider = new JsonArray();
jsonAuthorizations.put(providerId, jsonAuthorizationByProvider);
for (Authorization authorization : value.authorizations().get(providerId)) {
jsonAuthorizationByProvider.add(AuthorizationConverter.encode(authorization));
}
}
json.put(FIELD_AUTHORIZATIONS, jsonAuthorizations);
json.put(FIELD_ATTRIBUTES, value.attributes());
return json;
}
public static User decode(JsonObject json) throws IllegalArgumentException {
Objects.requireNonNull(json);
JsonObject principal = json.getJsonObject(FIELD_PRINCIPAL);
User user = User.create(principal);
// authorizations
JsonObject jsonAuthorizations = json.getJsonObject(FIELD_AUTHORIZATIONS);
for (String fieldName: jsonAuthorizations.fieldNames()) {
JsonArray jsonAuthorizationByProvider = jsonAuthorizations.getJsonArray(fieldName);
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy