Java Code Examples for io.jsonwebtoken.lang.Assert#notNull()

The following examples show how to use io.jsonwebtoken.lang.Assert#notNull() . 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: EllipticCurveProvider.java    From jjwt with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a new secure-random key pair of sufficient strength for the specified Elliptic Curve {@link
 * SignatureAlgorithm} (must be one of {@code ES256}, {@code ES384} or {@code ES512}) using the specified {@link
 * SecureRandom} random number generator via the specified JCA provider and algorithm name.
 *
 * @param jcaAlgorithmName the JCA name of the algorithm to use for key pair generation, for example, {@code
 *                         ECDSA}.
 * @param jcaProviderName  the JCA provider name of the algorithm implementation (for example {@code "BC"} for
 *                         BouncyCastle) or {@code null} if the default provider should be used.
 * @param alg              alg the algorithm indicating strength, must be one of {@code ES256}, {@code ES384} or
 *                         {@code ES512}
 * @param random           the SecureRandom generator to use during key generation.
 * @return a new secure-randomly generated key pair of sufficient strength for the specified Elliptic Curve {@link
 * SignatureAlgorithm} (must be one of {@code ES256}, {@code ES384} or {@code ES512}) using the specified {@link
 * SecureRandom} random number generator via the specified JCA provider and algorithm name.
 * @see #generateKeyPair()
 * @see #generateKeyPair(SignatureAlgorithm)
 * @see #generateKeyPair(SignatureAlgorithm, SecureRandom)
 */
public static KeyPair generateKeyPair(String jcaAlgorithmName, String jcaProviderName, SignatureAlgorithm alg,
                                      SecureRandom random) {
    Assert.notNull(alg, "SignatureAlgorithm argument cannot be null.");
    Assert.isTrue(alg.isEllipticCurve(), "SignatureAlgorithm argument must represent an Elliptic Curve algorithm.");
    try {
        KeyPairGenerator g;

        if (Strings.hasText(jcaProviderName)) {
            g = KeyPairGenerator.getInstance(jcaAlgorithmName, jcaProviderName);
        } else {
            g = KeyPairGenerator.getInstance(jcaAlgorithmName);
        }

        String paramSpecCurveName = EC_CURVE_NAMES.get(alg);
        ECGenParameterSpec spec = new ECGenParameterSpec(paramSpecCurveName);
        g.initialize(spec, random);
        return g.generateKeyPair();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to generate Elliptic Curve KeyPair: " + e.getMessage(), e);
    }
}
 
Example 2
Source File: OrgJsonDeserializer.java    From jjwt with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserialize(byte[] bytes) throws DeserializationException {

    Assert.notNull(bytes, "JSON byte array cannot be null");

    if (bytes.length == 0) {
        throw new DeserializationException("Invalid JSON: zero length byte array.");
    }

    try {
        String s = new String(bytes, Strings.UTF_8);
        return parse(s);
    } catch (Exception e) {
        String msg = "Invalid JSON: " + e.getMessage();
        throw new DeserializationException(msg, e);
    }
}
 
Example 3
Source File: DefaultJwtBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, byte[] secretKey) {
    Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
    Assert.notEmpty(secretKey, "secret key byte array cannot be null or empty.");
    Assert.isTrue(alg.isHmac(), "Key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
    this.algorithm = alg;
    this.keyBytes = secretKey;
    return this;
}
 
Example 4
Source File: SecretService.java    From tutorials with MIT License 5 votes vote down vote up
public void setSecrets(Map<String, String> secrets) {
    Assert.notNull(secrets);
    Assert.hasText(secrets.get(SignatureAlgorithm.HS256.getValue()));
    Assert.hasText(secrets.get(SignatureAlgorithm.HS384.getValue()));
    Assert.hasText(secrets.get(SignatureAlgorithm.HS512.getValue()));

    this.secrets = secrets;
}
 
Example 5
Source File: DefaultJwtBuilder.java    From jjwt with Apache License 2.0 5 votes vote down vote up
@Override
public JwtBuilder signWith(Key key, SignatureAlgorithm alg) throws InvalidKeyException {
    Assert.notNull(key, "Key argument cannot be null.");
    Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
    alg.assertValidSigningKey(key); //since 0.10.0 for https://github.com/jwtk/jjwt/issues/334
    this.algorithm = alg;
    this.key = key;
    return this;
}
 
Example 6
Source File: AbstractCompressionCodec.java    From jjwt with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that payload is not null and calls {@link #doCompress(byte[]) doCompress}
 *
 * @param payload bytes to compress
 * @return compressed bytes
 * @throws CompressionException if {@link #doCompress(byte[]) doCompress} throws an IOException
 */
@Override
public final byte[] compress(byte[] payload) {
    Assert.notNull(payload, "payload cannot be null.");

    try {
        return doCompress(payload);
    } catch (IOException e) {
        throw new CompressionException("Unable to compress payload.", e);
    }
}
 
Example 7
Source File: AbstractCompressionCodec.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Asserts that payload is not null and calls {@link #doCompress(byte[]) doCompress}
 *
 * @param payload bytes to compress
 * @return compressed bytes
 * @throws CompressionException if {@link #doCompress(byte[]) doCompress} throws an IOException
 */
@Override
public final byte[] compress(byte[] payload) {
    Assert.notNull(payload, "payload cannot be null.");

    try {
        return doCompress(payload);
    } catch (IOException e) {
        throw new CompressionException("Unable to compress payload.", e);
    }
}
 
Example 8
Source File: JacksonSerializer.java    From jjwt with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(T t) throws SerializationException {
    Assert.notNull(t, "Object to serialize cannot be null.");
    try {
        return writeValueAsBytes(t);
    } catch (JsonProcessingException e) {
        String msg = "Unable to serialize object: " + e.getMessage();
        throw new SerializationException(msg, e);
    }
}
 
Example 9
Source File: JwsToUserDetailsConverter.java    From juiser with Apache License 2.0 5 votes vote down vote up
public JwsToUserDetailsConverter(Function<String, Claims> claimsExtractor,
                                 Function<Claims, User> claimsUserFactory,
                                 Function<Claims, Collection<? extends GrantedAuthority>> authoritiesResolver) {
    Assert.notNull(claimsExtractor, "claimsExtractor cannot be null.");
    Assert.notNull(claimsUserFactory, "claimsUserFactory cannot be null.");
    this.claimsExtractor = claimsExtractor;
    this.claimsUserFactory = claimsUserFactory;
    this.authoritiesResolver = authoritiesResolver;
}
 
Example 10
Source File: ExceptionPropagatingDecoder.java    From jjwt with Apache License 2.0 4 votes vote down vote up
ExceptionPropagatingDecoder(Decoder<T, R> decoder) {
    Assert.notNull(decoder, "Decoder cannot be null.");
    this.decoder = decoder;
}
 
Example 11
Source File: DefaultCompressionCodecResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private String getAlgorithmFromHeader(Header header) {
    Assert.notNull(header, "header cannot be null.");

    return header.getCompressionAlgorithm();
}
 
Example 12
Source File: Base64Decoder.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] decode(String s) throws DecodingException {
    Assert.notNull(s, "String argument cannot be null");
    return this.base64.decodeFast(s.toCharArray());
}
 
Example 13
Source File: DefaultJwtParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JwtParser setSigningKeyResolver(SigningKeyResolver signingKeyResolver) {
    Assert.notNull(signingKeyResolver, "SigningKeyResolver cannot be null.");
    this.signingKeyResolver = signingKeyResolver;
    return this;
}
 
Example 14
Source File: DefaultJwtParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JwtParser setClock(Clock clock) {
    Assert.notNull(clock, "Clock instance cannot be null.");
    this.clock = clock;
    return this;
}
 
Example 15
Source File: ClaimValueResolver.java    From juiser with Apache License 2.0 4 votes vote down vote up
public ClaimValueResolver(Function<Claims, ?> delegate, boolean resultRequired) {
    Assert.notNull(delegate, "delegate function cannot be null.");
    this.delegate = delegate;
    this.resultRequired = resultRequired;
}
 
Example 16
Source File: DefaultJwtParser.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Override
public JwtParser setCompressionCodecResolver(CompressionCodecResolver compressionCodecResolver) {
    Assert.notNull(compressionCodecResolver, "compressionCodecResolver cannot be null.");
    this.compressionCodecResolver = compressionCodecResolver;
    return this;
}
 
Example 17
Source File: DefaultJwtParserBuilder.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Override
public JwtParserBuilder setCompressionCodecResolver(CompressionCodecResolver compressionCodecResolver) {
    Assert.notNull(compressionCodecResolver, "compressionCodecResolver cannot be null.");
    this.compressionCodecResolver = compressionCodecResolver;
    return this;
}
 
Example 18
Source File: DefaultJwtParserBuilder.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Override
public JwtParserBuilder setSigningKeyResolver(SigningKeyResolver signingKeyResolver) {
    Assert.notNull(signingKeyResolver, "SigningKeyResolver cannot be null.");
    this.signingKeyResolver = signingKeyResolver;
    return this;
}
 
Example 19
Source File: RequestHeaderUserFactory.java    From juiser with Apache License 2.0 4 votes vote down vote up
public RequestHeaderUserFactory(String headerName, Function<String, User> headerValueToUserConverter) {
    Assert.hasText(headerName, "headerName argument cannot be null or empty.");
    Assert.notNull(headerValueToUserConverter, "headerValueToUserConverter function cannot be null.");
    this.headerName = headerName;
    this.headerValueToUser = headerValueToUserConverter;
}
 
Example 20
Source File: GsonDeserializer.java    From jjwt with Apache License 2.0 4 votes vote down vote up
private GsonDeserializer(Gson gson, Class<T> returnType) {
    Assert.notNull(gson, "gson cannot be null.");
    Assert.notNull(returnType, "Return type cannot be null.");
    this.gson = gson;
    this.returnType = returnType;
}