Java Code Examples for co.nstant.in.cbor.model.Map#get()

The following examples show how to use co.nstant.in.cbor.model.Map#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidSafetyNetAttestationStatement.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a cbor representation of an AndroidSafetyNetAttestationStatement into the object
 * representation
 *
 * @param attStmt Cbor DataItem representation of the attestation statement to decode
 * @return Decoded AndroidSafetyNetAttestationStatement
 * @throws ResponseException Input was not a valid AndroidSafetyNetAttestationStatement DataItem
 */
public static AndroidSafetyNetAttestationStatement decode(DataItem attStmt)
    throws ResponseException {
  AndroidSafetyNetAttestationStatement result = new AndroidSafetyNetAttestationStatement();
  Map given = (Map) attStmt;
  for (DataItem data : given.getKeys()) {
    if (data instanceof UnicodeString) {
      if (((UnicodeString) data).getString().equals("ver")) {
        UnicodeString version = (UnicodeString) given.get(data);
        result.ver = version.getString();
      } else if (((UnicodeString) data).getString().equals("response")) {
        result.response = ((ByteString) (given.get(data))).getBytes();
      }
    }
  }
  if (result.response == null || result.ver == null)
    throw new ResponseException("Invalid JWT Cbor");
  return result;
}
 
Example 2
Source File: MapDecoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
private Map decodeInfinitiveLength() throws CborException {
    Map map = new Map();
    map.setChunked(true);
    if (decoder.isAutoDecodeInfinitiveMaps()) {
        for (;;) {
            DataItem key = decoder.decodeNext();
            if (Special.BREAK.equals(key)) {
                break;
            }
            DataItem value = decoder.decodeNext();
            if (key == null || value == null) {
                throw new CborException("Unexpected end of stream");
            }
            if (decoder.isRejectDuplicateKeys() && map.get(key) != null) {
                throw new CborException("Duplicate key found in map");
            }
            map.put(key, value);
        }
    }
    return map;
}
 
Example 3
Source File: AttestationObject.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
/**
 * @param attestationObject
 * @return AttestationObject created from the provided byte array
 * @throws CborException
 * @throws ResponseException
 */
public static AttestationObject decode(byte[] attestationObject)
    throws CborException, ResponseException {
  AttestationObject result = new AttestationObject();
  List<DataItem> dataItems = CborDecoder.decode(attestationObject);

  if (dataItems.size() == 1 && dataItems.get(0) instanceof Map) {
    DataItem attStmt = null;
    Map attObjMap = (Map) dataItems.get(0);
    for (DataItem key : attObjMap.getKeys()) {
      if (key instanceof UnicodeString) {
        switch(((UnicodeString) key).getString()) {
          case "fmt":
            UnicodeString value = (UnicodeString) attObjMap.get(key);
            result.fmt = value.getString();
            break;
          case "authData":
            byte[] authData = ((ByteString) attObjMap.get(key)).getBytes();
            result.authData = AuthenticatorData.decode(authData);
            break;
          case "attStmt":
            attStmt = attObjMap.get(key);
            break;
            
        }
      }
    }

    if (attStmt != null) {
      result.attStmt = AttestationStatement.decode(result.fmt, attStmt);
    }

  }
  return result;
}
 
Example 4
Source File: MapDecoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
private Map decodeFixedLength(long length) throws CborException {
    Map map = new Map(getPreallocationSize(length));
    for (long i = 0; i < length; i++) {
        DataItem key = decoder.decodeNext();
        DataItem value = decoder.decodeNext();
        if (key == null || value == null) {
            throw new CborException("Unexpected end of stream");
        }
        if (decoder.isRejectDuplicateKeys() && map.get(key) != null) {
            throw new CborException("Duplicate key found in map");
        }
        map.put(key, value);
    }
    return map;
}