Java Code Examples for javax.json.JsonNumber#longValue()

The following examples show how to use javax.json.JsonNumber#longValue() . 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: RawClaimTypeProducer.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
@Produces
@Claim("")
Long getClaimAsLong(InjectionPoint ip) {
    CDILogging.log.getClaimAsLong(ip);
    if (currentToken == null) {
        return null;
    }

    String name = getName(ip);
    Optional<Object> optValue = currentToken.claim(name);
    Long returnValue = null;
    if (optValue.isPresent()) {
        Object value = optValue.get();
        if (value instanceof JsonNumber) {
            JsonNumber jsonValue = (JsonNumber) value;
            returnValue = jsonValue.longValue();
        } else {
            returnValue = Long.parseLong(value.toString());
        }
    }
    return returnValue;
}
 
Example 2
Source File: JwtClaimsBuilderImpl.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
private static Object convertJsonValue(JsonValue jsonValue) {
    if (jsonValue instanceof JsonString) {
        String jsonString = jsonValue.toString();
        return jsonString.toString().substring(1, jsonString.length() - 1);
    } else if (jsonValue instanceof JsonNumber) {
        JsonNumber jsonNumber = (JsonNumber) jsonValue;
        if (jsonNumber.isIntegral()) {
            return jsonNumber.longValue();
        } else {
            return jsonNumber.doubleValue();
        }
    } else if (jsonValue == JsonValue.TRUE) {
        return true;
    } else if (jsonValue == JsonValue.FALSE) {
        return false;
    } else {
        return null;
    }
}
 
Example 3
Source File: JsonUnmarshaller.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
private Object unmarshalAttribute(JsonValue value) {
    if (value instanceof JsonNumber) {
        JsonNumber num = (JsonNumber)value;
        return num.isIntegral() ? num.longValue() : num.bigDecimalValue();
    } else if (value instanceof JsonString) {
        return ((JsonString)value).getString();
    } else if (value instanceof JsonObject) {
        return build((JsonObject)value);
    } else if (value instanceof JsonArray) {
        return build((JsonArray)value);
    } else {
        return null;
    }
}
 
Example 4
Source File: AndroidSafetynetAttestationStatement.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public Boolean verifySignature(String browserDataBase64, FIDO2AuthenticatorData authData) {
    try {
        //Verify JWT timestamp is valid
        JsonNumber timestampMs = jwt.getBody().getJsonNumber("timestampMs");
        Date now = new Date();
        if (timestampMs == null //timestampMS is missing
                || timestampMs.longValue() > now.getTime() + (30 * 1000)        //timestampMS is in the future (some hardcoded buffer)  (TODO fix hardcode)
                || timestampMs.longValue() < now.getTime() - (60 * 1000)) {     //timestampMS is older than 1 minute
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-0015",
                    "JWT time stamp = " + timestampMs.longValue() + ", current time = " + now.getTime());
            throw new IllegalArgumentException("JWT has invalid timestampMs");
        }

        //Verify JWT certificate chain
        JsonArray x5c = jwt.getHeader().getJsonArray("x5c");
        if (x5c == null || x5c.isEmpty()) {
            throw new IllegalArgumentException("JWT missing x5c information");
        }
        if (x5c.size() < 2) {
            throw new IllegalArgumentException("JWT missing certificate chain");
        }
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509", "BCFIPS");
        Base64.Decoder decoder = Base64.getDecoder();
        List<X509Certificate> certchain = new ArrayList<>();
        X509Certificate rootCert = null;
        for (int i = 0; i < x5c.size(); i++) {
            byte[] certBytes = decoder.decode(x5c.getString(i, null));
            ByteArrayInputStream instr = new ByteArrayInputStream(certBytes);
            X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(instr);
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "certificate number " + i + " = " + certificate);
            if(i == x5c.size() - 1){
                rootCert = certificate;
            }
            else{
                certchain.add(certificate);
            }
        }
        if(rootCert == null){
            throw new IllegalArgumentException("JWT missing certificate chain root");
        }
        Set<TrustAnchor> trustAnchor = new HashSet<>();
        trustAnchor.add(new TrustAnchor(rootCert, null));
        CertPath certPath = CertificateFactory.getInstance("X.509", "BCFIPS").generateCertPath(certchain);
        if(!PKIXChainValidation.pkixvalidate(certPath, trustAnchor, false, false)){     //TODO check CRLs if they exist, otherwise don't
            throw new IllegalArgumentException("JWT failed PKIX validation");
        }

        //Verify JWT signature
        if (!jwt.verifySignature(certchain.get(0).getPublicKey())) {
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-0015",
                    "JWT Signature verification failed!");
            return false;
        }

        //Verify that response is a valid SafetyNet response of version ver.
        if(version == null || version.isEmpty()){
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-0015",
                    "AndroidSafetynet missing version information");
            return false;
        }

        //Verify that the nonce in the response is identical to the SHA-256 hash of the concatenation of authenticatorData and clientDataHash.
        String nonce = jwt.getBody().getString("nonce", null);
        if(nonce == null || !Arrays.equals(decoder.decode(nonce), skfsCommon.getDigestBytes(concatenateArrays(authData.getAuthDataDecoded(),
                skfsCommon.getDigestBytes(Base64.getDecoder().decode(browserDataBase64), "SHA256")), "SHA256"))){
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-0015",
                    "JWT has incorrect nonce");
            return false;
        }

        //Verify that the attestation certificate is issued to the hostname "attest.android.com" (see SafetyNet online documentation).
        String cn = getFirstCN(certchain.get(0).getSubjectDN().getName());
        if(cn == null || !cn.equals("attest.android.com")){
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-0015",
                    "JWT attestation certificate does not match the specification");
            return false;
        }

        //Verify that the ctsProfileMatch attribute in the payload of response is true.
        if(!jwt.getBody().getBoolean("ctsProfileMatch", false)){
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.SEVERE, "FIDO-ERR-0015",
                    "JWT attestation ctsProfileMatch does not match the specification");
            return false;
        }

        return true;
    } catch (UnsupportedEncodingException | CertificateException | NoSuchAlgorithmException | NoSuchProviderException ex) {
        Logger.getLogger(AndroidSafetynetAttestationStatement.class.getName()).log(Level.SEVERE, null, ex);
    }

    return Boolean.FALSE;
}
 
Example 5
Source File: SolrEntityIndexerMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private void indexJson( SolrInputDocument input, Object object )
{
    if( object instanceof JsonArray )
    {
        JsonArray array = (JsonArray) object;
        for( int i = 0; i < array.size(); i++ )
        {
            indexJson( input, array.get( i ) );
        }
    }
    else
    {
        JsonObject jsonObject = (JsonObject) object;
        for( String name : jsonObject.keySet() )
        {
            JsonValue jsonValue = jsonObject.get( name );
            if( jsonValue.getValueType() == JsonValue.ValueType.OBJECT
                || jsonValue.getValueType() == JsonValue.ValueType.ARRAY )
            {
                indexJson( input, jsonValue );
            }
            else
            {
                SchemaField field = indexedFields.get( name );
                if( field != null )
                {
                    Object value;
                    switch( jsonValue.getValueType() )
                    {
                        case NULL:
                            value = null;
                            break;
                        case STRING:
                            value = ( (JsonString) jsonValue ).getString();
                            break;
                        case NUMBER:
                            JsonNumber jsonNumber = (JsonNumber) jsonValue;
                            value = jsonNumber.isIntegral() ? jsonNumber.longValue() : jsonNumber.doubleValue();
                            break;
                        case TRUE:
                            value = Boolean.TRUE;
                            break;
                        case FALSE:
                            value = Boolean.FALSE;
                            break;
                        default:
                            value = jsonValue.toString();
                    }
                    input.addField( name, value );
                }
            }
        }
    }
}
 
Example 6
Source File: JsonUtil.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public static Object convertJsonValue(Object jsonValue, Class desiredType) {
   if (jsonValue instanceof JsonNumber) {
      JsonNumber number = (JsonNumber) jsonValue;

      if (desiredType == null || desiredType == Long.class || desiredType == Long.TYPE) {
         return number.longValue();
      } else if (desiredType == Integer.class || desiredType == Integer.TYPE) {
         return number.intValue();
      } else if (desiredType == Double.class || desiredType == Double.TYPE) {
         return number.doubleValue();
      } else {
         return number.longValue();
      }
   } else if (jsonValue instanceof JsonString) {
      return ((JsonString) jsonValue).getString();
   } else if (jsonValue instanceof JsonValue) {
      if (jsonValue == JsonValue.TRUE) {
         return true;
      } else if (jsonValue == JsonValue.FALSE) {
         return false;
      } else {
         return jsonValue.toString();
      }
   } else if (jsonValue instanceof Number) {
      Number jsonNumber = (Number) jsonValue;
      if (desiredType == Integer.TYPE || desiredType == Integer.class) {
         return jsonNumber.intValue();
      } else if (desiredType == Long.TYPE || desiredType == Long.class) {
         return jsonNumber.longValue();
      } else if (desiredType == Double.TYPE || desiredType == Double.class) {
         return jsonNumber.doubleValue();
      } else if (desiredType == Short.TYPE || desiredType == Short.class) {
         return jsonNumber.shortValue();
      } else {
         return jsonValue;
      }
   } else if (jsonValue instanceof Object[]) {
      Object[] array = (Object[]) jsonValue;
      Object[] result;
      if (desiredType != null) {
         result = (Object[]) Array.newInstance(desiredType, array.length);
      } else {
         result = array;
      }
      for (int i = 0; i < array.length; i++) {
         result[i] = convertJsonValue(array[i], desiredType);
      }
      return result;
   } else {
      return jsonValue;
   }
}