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

org.xipki.ca.gateway.acme.AcmeAuthz Maven / Gradle / Ivy

There is a newer version: 6.5.1
Show newest version
// Copyright (c) 2013-2023 xipki. All rights reserved.
// License Apache License 2.0

package org.xipki.ca.gateway.acme;

import org.xipki.ca.gateway.acme.msg.AuthzResponse;
import org.xipki.ca.gateway.acme.msg.ChallengeResponse;
import org.xipki.ca.gateway.acme.type.AuthzStatus;
import org.xipki.util.Args;
import org.xipki.util.CompareUtil;
import org.xipki.util.JSON;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Lijun Liao (xipki)
 */
public class AcmeAuthz {

  private final int subId;

  private final AcmeIdentifier identifier;

  private AuthzStatus status;
  private Instant expires;

  private List challenges;

  private AcmeOrder order;

  public AcmeAuthz(int subId, AcmeIdentifier identifier) {
    this.subId = Args.notNull(subId, "subId");
    this.identifier = Args.notNull(identifier, "identifier");
  }

  public Map encode() {
    Map map = new HashMap<>();
    map.put("subId", subId);
    map.put("identifier", identifier.encode());
    if (status != null) {
      map.put("status", status.name());
    }
    if (expires != null) {
      map.put("expires", expires.getEpochSecond());
    }

    if (challenges != null) {
      List> challMaps = new ArrayList<>(challenges.size());
      for (AcmeChallenge m : challenges) {
        challMaps.add(m.encode());
      }
      map.put("challenges", challMaps);
    }
    return map;
  }

  public static AcmeAuthz decode(Map encoded) {
    int subId = AcmeUtils.getInt(encoded, "subId");
    AcmeIdentifier identifier = AcmeIdentifier.decode((Map) encoded.get("identifier"));
    String str = (String) encoded.get("status");
    AuthzStatus status = (str == null) ? null : AuthzStatus.valueOf(str);
    Long l = AcmeUtils.getLong(encoded, "expires");
    Instant expires = (l == null) ? null : Instant.ofEpochSecond(l);
    List> challMaps = (List>) encoded.get("challenges");

    AcmeAuthz authz = new AcmeAuthz(subId, identifier);
    List challenges = null;
    if (challMaps != null) {
      challenges = new ArrayList<>(challMaps.size());
      for (Map m : challMaps) {
        AcmeChallenge chall = AcmeChallenge.decode(m);
        chall.setAuthz(authz);
        challenges.add(chall);
      }
    }

    authz.status = status;
    authz.expires = expires;
    authz.challenges = challenges;
    return authz;
  }

  public AcmeOrder getOrder() {
    return order;
  }

  public void setOrder(AcmeOrder order) {
    this.order = order;
  }

  public int getSubId() {
    return subId;
  }

  public AuthzStatus getStatus() {
    return status;
  }

  public void setStatus(AuthzStatus status) {
    markOrder();
    this.status = status;
  }

  public Instant getExpires() {
    return expires;
  }

  public void setExpires(Instant expires) {
    markOrder();
    this.expires = expires;
  }

  public AcmeIdentifier getIdentifier() {
    return identifier;
  }

  public List getChallenges() {
    return challenges;
  }

  public void setChallenges(List challenges) {
    markOrder();
    this.challenges = challenges;
    if (challenges != null) {
      for (AcmeChallenge chall : challenges) {
        chall.setAuthz(this);
      }
    }
  }

  void markOrder() {
    if (order != null) {
      order.markMe();
    }
  }

  public AuthzResponse toResponse(String baseUrl, long orderId) {
    AuthzResponse resp = new AuthzResponse();
    resp.setExpires(expires.toString());
    resp.setStatus(status);
    resp.setIdentifier(identifier.toIdentifier());
    List challResps = new ArrayList<>(challenges.size());
    resp.setChallenges(challResps);
    for (AcmeChallenge chall : challenges) {
      challResps.add(chall.toChallengeResponse(baseUrl, orderId, subId));
    }
    return resp;
  }

  public String getUrl(String baseUrl) {
    AuthzId authzId = new AuthzId(order.getId(), subId);
    return baseUrl + "authz/" + authzId.toIdText();
  }

  @Override
  public boolean equals(Object other) {
    if (!(other instanceof AcmeAuthz)) {
      return false;
    }

    AcmeAuthz b = (AcmeAuthz) other;
    return status == b.status && subId == b.subId
        && CompareUtil.equalsObject(expires, b.expires)
        && CompareUtil.equalsObject(identifier, b.identifier)
        && CompareUtil.equalsObject(challenges, b.challenges);
  }

  public AcmeAuthz copy() {
    AcmeAuthz copy = new AcmeAuthz(subId, identifier);
    copy.status = status;
    copy.expires = expires;
    if (challenges != null) {
      copy.challenges = new ArrayList<>(challenges.size());
      for (AcmeChallenge chall : challenges) {
        copy.challenges.add(chall.copy());
      }
    }

    return copy;
  }

  public static String encodeAuthzs(List authzs) {
    List> maps = new ArrayList<>(authzs.size());
    for (AcmeAuthz m : authzs) {
      maps.add(m.encode());
    }
    return JSON.toJson(maps);
  }

  public static List decodeAuthzs(String encoded) {
    List> list = JSON.parseObject(encoded, List.class);
    List ret = new ArrayList<>(list.size());
    for (Map map : list) {
      ret.add(AcmeAuthz.decode(map));
    }
    return ret;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy