Search in sources :

Example 1 with JwtClaimValue

use of com.google.crypto.tink.proto.testing.JwtClaimValue in project tink by google.

the class JwtServiceImpl method convertJwtTokenToRawJwt.

private RawJwt convertJwtTokenToRawJwt(JwtToken token) throws JwtInvalidException {
    RawJwt.Builder rawJwtBuilder = RawJwt.newBuilder();
    if (token.hasTypeHeader()) {
        rawJwtBuilder.setTypeHeader(token.getTypeHeader().getValue());
    }
    if (token.hasIssuer()) {
        rawJwtBuilder.setIssuer(token.getIssuer().getValue());
    }
    if (token.hasSubject()) {
        rawJwtBuilder.setSubject(token.getSubject().getValue());
    }
    for (String audience : token.getAudiencesList()) {
        rawJwtBuilder.addAudience(audience);
    }
    if (token.hasJwtId()) {
        rawJwtBuilder.setJwtId(token.getJwtId().getValue());
    }
    if (token.hasExpiration()) {
        rawJwtBuilder.setExpiration(timestampToInstant(token.getExpiration()));
    } else {
        rawJwtBuilder.withoutExpiration();
    }
    if (token.hasNotBefore()) {
        rawJwtBuilder.setNotBefore(timestampToInstant(token.getNotBefore()));
    }
    if (token.hasIssuedAt()) {
        rawJwtBuilder.setIssuedAt(timestampToInstant(token.getIssuedAt()));
    }
    for (Map.Entry<String, JwtClaimValue> entry : token.getCustomClaimsMap().entrySet()) {
        String name = entry.getKey();
        JwtClaimValue value = entry.getValue();
        switch(value.getKindCase().getNumber()) {
            case JwtClaimValue.NULL_VALUE_FIELD_NUMBER:
                rawJwtBuilder.addNullClaim(name);
                break;
            case JwtClaimValue.BOOL_VALUE_FIELD_NUMBER:
                rawJwtBuilder.addBooleanClaim(name, value.getBoolValue());
                break;
            case JwtClaimValue.NUMBER_VALUE_FIELD_NUMBER:
                rawJwtBuilder.addNumberClaim(name, value.getNumberValue());
                break;
            case JwtClaimValue.STRING_VALUE_FIELD_NUMBER:
                rawJwtBuilder.addStringClaim(name, value.getStringValue());
                break;
            case JwtClaimValue.JSON_ARRAY_VALUE_FIELD_NUMBER:
                rawJwtBuilder.addJsonArrayClaim(name, value.getJsonArrayValue());
                break;
            case JwtClaimValue.JSON_OBJECT_VALUE_FIELD_NUMBER:
                rawJwtBuilder.addJsonObjectClaim(name, value.getJsonObjectValue());
                break;
            default:
                throw new RuntimeException("Unknown JwtClaimValue kind: " + value.getKindCase());
        }
    }
    return rawJwtBuilder.build();
}
Also used : RawJwt(com.google.crypto.tink.jwt.RawJwt) JwtClaimValue(com.google.crypto.tink.proto.testing.JwtClaimValue) ByteString(com.google.protobuf.ByteString) Map(java.util.Map)

Aggregations

RawJwt (com.google.crypto.tink.jwt.RawJwt)1 JwtClaimValue (com.google.crypto.tink.proto.testing.JwtClaimValue)1 ByteString (com.google.protobuf.ByteString)1 Map (java.util.Map)1