/** * Base64-decode the given string as byte[]. * * @param data the base64 string * @return the encoded array */ public static byte[] decodeUrlSafeBase64(final String data) { return decodeBase64(data); }
/** * Base64-decode the given string as String. * * @param data the base64 string * @return the string */ public static String decodeBase64ToString(final String data) { return new String(decodeBase64(data), StandardCharsets.UTF_8); }
@Override public String decode(final Serializable value, final Object[] parameters) { val decoded = EncodingUtils.decodeBase64(value.toString()); return new String(decoded, StandardCharsets.UTF_8); } }
/** * Instantiates a new Encoded ticket. * * @param encodedTicket the encoded ticket that will be decoded from base64 * @param encodedTicketId the encoded ticket id */ @SneakyThrows @JsonCreator public EncodedTicket(@JsonProperty("encoded") final String encodedTicket, @JsonProperty("id") final String encodedTicketId) { this.id = encodedTicketId; this.encodedTicket = EncodingUtils.decodeBase64(encodedTicket); }
/** * First decode base64 String to byte array, then use ZipInputStream to revert the byte array to a * string. * * @param zippedBase64Str the zipped base 64 str * @return the string, or null */ @SneakyThrows public static String decompress(final String zippedBase64Str) { val bytes = EncodingUtils.decodeBase64(zippedBase64Str); try (val zi = new GZIPInputStream(new ByteArrayInputStream(bytes))) { return IOUtils.toString(zi, Charset.defaultCharset()); } }
/** * Retrieve authn request authn request. * * @param request the request * @return the authn request * @throws Exception the exception */ protected AuthnRequest retrieveSamlAuthenticationRequestFromHttpRequest(final HttpServletRequest request) throws Exception { LOGGER.debug("Retrieving authentication request from scope"); val requestValue = request.getParameter(SamlProtocolConstants.PARAMETER_SAML_REQUEST); if (StringUtils.isBlank(requestValue)) { throw new IllegalArgumentException("SAML request could not be determined from the authentication request"); } val encodedRequest = EncodingUtils.decodeBase64(requestValue.getBytes(StandardCharsets.UTF_8)); return (AuthnRequest) XMLObjectSupport.unmarshallFromInputStream(this.configBean.getParserPool(), new ByteArrayInputStream(encodedRequest)); }
@Override public X509CertificateCredential deserialize(final JsonParser jp, final DeserializationContext deserializationContext) throws IOException { val oc = jp.getCodec(); val node = JsonNode.class.cast(oc.readTree(jp)); val certs = new ArrayList<X509Certificate>(); node.findValues("certificates").forEach(n -> { val cert = n.get(0).textValue(); val data = EncodingUtils.decodeBase64(cert); certs.add(CertUtils.readCertificate(new InputStreamResource(new ByteArrayInputStream(data)))); }); return new X509CertificateCredential(certs.toArray(X509Certificate[]::new)); } }
/** * Gets x509 cRL from attribute. Retrieves the binary attribute value, * decodes it to base64, and fetches it as a byte-array resource. * * @param aval the attribute, which may be null if it's not found * @return the x 509 cRL from attribute * @throws IOException the exception thrown if resources cant be fetched * @throws CRLException the exception thrown if resources cant be fetched * @throws CertificateException if connection to ldap fails, or attribute to get the revocation list is unavailable */ protected X509CRL fetchX509CRLFromAttribute(final LdapAttribute aval) throws CertificateException, IOException, CRLException { if (aval != null && aval.isBinary()) { val val = aval.getBinaryValue(); if (val == null || val.length == 0) { throw new CertificateException("Empty attribute. Can not download CRL from ldap"); } val decoded64 = EncodingUtils.decodeBase64(val); if (decoded64 == null) { throw new CertificateException("Could not decode the attribute value to base64"); } LOGGER.debug("Retrieved CRL from ldap as byte array decoded in base64. Fetching..."); return super.fetch(new ByteArrayResource(decoded64)); } throw new CertificateException("Attribute not found. Can not retrieve CRL"); }
private void ensureEncryptionKeyExists(final String encryptionSecretKey, final int encryptionKeySize) { final byte[] genEncryptionKey; if (StringUtils.isBlank(encryptionSecretKey)) { LOGGER.warn("Secret key for encryption is not defined under [{}]. CAS will attempt to auto-generate the encryption key", getEncryptionKeySetting()); val key = new Base64RandomStringGenerator(encryptionKeySize).getNewString(); LOGGER.warn("Generated encryption key [{}] of size [{}]. The generated key MUST be added to CAS settings under setting [{}].", key, encryptionKeySize, getEncryptionKeySetting()); genEncryptionKey = EncodingUtils.decodeBase64(key); } else { val base64 = EncodingUtils.isBase64(encryptionSecretKey); val key = base64 ? EncodingUtils.decodeBase64(encryptionSecretKey) : ArrayUtils.EMPTY_BYTE_ARRAY; if (base64 && key.length == encryptionKeySize) { LOGGER.trace("Secret key for encryption defined under [{}] is Base64 encoded.", getEncryptionKeySetting()); genEncryptionKey = key; } else if (encryptionSecretKey.length() != encryptionKeySize) { LOGGER.warn("Secret key for encryption defined under [{}] is Base64 encoded but the size does not match the key size [{}].", getEncryptionKeySetting(), encryptionKeySize); genEncryptionKey = encryptionSecretKey.getBytes(StandardCharsets.UTF_8); } else { LOGGER.warn("Secret key for encryption defined under [{}] is not Base64 encoded. Clear the setting to regenerate (Recommended) or replace with" + " [{}].", getEncryptionKeySetting(), EncodingUtils.encodeBase64(encryptionSecretKey)); genEncryptionKey = encryptionSecretKey.getBytes(StandardCharsets.UTF_8); } } this.encryptionSecretKey = genEncryptionKey; }
/** * Verify jws signature byte [ ]. * * @param signingKey the signing key * @param asString the as string * @return the byte [ ] */ @SneakyThrows public static byte[] verifyJwsSignature(final Key signingKey, final String asString) { val jws = new JsonWebSignature(); jws.setCompactSerialization(asString); jws.setKey(signingKey); val verified = jws.verifySignature(); if (verified) { val payload = jws.getEncodedPayload(); LOGGER.trace("Successfully decoded value. Result in Base64-encoding is [{}]", payload); return EncodingUtils.decodeBase64(payload); } return null; }