All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.shaded.vertx.ext.auth.impl.UserConverter Maven / Gradle / Ivy

There is a newer version: 7.17.0
Show newest version
/********************************************************************************
 * 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 com.arangodb.shaded.vertx.core.json.JsonArray;
import com.arangodb.shaded.vertx.core.json.JsonObject;
import com.arangodb.shaded.vertx.ext.auth.User;
import com.arangodb.shaded.vertx.ext.auth.authorization.Authorization;
import com.arangodb.shaded.vertx.ext.auth.authorization.Authorizations;
import com.arangodb.shaded.vertx.ext.auth.authorization.impl.AuthorizationConverter;

import java.util.Objects;

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());
    Authorizations authorizations = value.authorizations();
    if (authorizations != null) {
      JsonObject jsonAuthorizations = new JsonObject();
      for (String providerId : authorizations.getProviderIds()) {
        JsonArray jsonAuthorizationByProvider = new JsonArray();
        jsonAuthorizations.put(providerId, jsonAuthorizationByProvider);
        for (Authorization authorization : 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