Java Code Examples for org.gluu.oxauth.model.jwt.Jwt#toString()

The following examples show how to use org.gluu.oxauth.model.jwt.Jwt#toString() . 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: UserInfoRestWebServiceImpl.java    From oxAuth with MIT License 6 votes vote down vote up
private String getJwtResponse(SignatureAlgorithm signatureAlgorithm, User user, AuthorizationGrant authorizationGrant,
                              Collection<String> scopes) throws Exception {
    log.trace("Building JWT reponse with next scopes {0} for user {1} and user custom attributes {0}", scopes, user.getUserId(), user.getCustomAttributes());

    Jwt jwt = new Jwt();

    // Header
    jwt.getHeader().setType(JwtType.JWT);
    jwt.getHeader().setAlgorithm(signatureAlgorithm);

    String keyId = new ServerCryptoProvider(cryptoProvider).getKeyId(webKeysConfiguration, Algorithm.fromString(signatureAlgorithm.getName()), Use.SIGNATURE);
    if (keyId != null) {
        jwt.getHeader().setKeyId(keyId);
    }

    // Claims
    jwt.setClaims(createJwtClaims(user, authorizationGrant, scopes));

    // Signature
    String sharedSecret = clientService.decryptSecret(authorizationGrant.getClient().getClientSecret());
    String signature = cryptoProvider.sign(jwt.getSigningInput(), jwt.getHeader().getKeyId(), sharedSecret, signatureAlgorithm);
    jwt.setEncodedSignature(signature);

    return jwt.toString();
}
 
Example 2
Source File: JwtCrossCheckTest.java    From oxAuth with MIT License 6 votes vote down vote up
private static String createOxauthJwt(OxAuthCryptoProvider cryptoProvider, String kid, SignatureAlgorithm algorithm) throws Exception {
    Jwt jwt = new Jwt();

    jwt.getHeader().setKeyId(kid);
    jwt.getHeader().setType(JwtType.JWT);
    jwt.getHeader().setAlgorithm(algorithm);

    jwt.getClaims().setSubjectIdentifier("1202.d50a4eeb-ab5d-474b-aaaf-e4aa47bc54a5");
    jwt.getClaims().setIssuer("1202.d50a4eeb-ab5d-474b-aaaf-e4aa47bc54a5");
    jwt.getClaims().setExpirationTime(new Date(1575559276888000L));
    jwt.getClaims().setIssuedAt(new Date(1575559276888000L));
    jwt.getClaims().setAudience("https://gomer-vbox/oxauth/restv1/token");

    String signature = cryptoProvider.sign(jwt.getSigningInput(), jwt.getHeader().getKeyId(), null, algorithm);
    jwt.setEncodedSignature(signature);
    return jwt.toString();
}
 
Example 3
Source File: ClientAuthnRequest.java    From oxAuth with MIT License 4 votes vote down vote up
public String getClientAssertion() {
    if (cryptoProvider == null) {
        LOG.error("Crypto provider is not specified");
        return null;
    }

    if (algorithm == null) {
        algorithm = SignatureAlgorithm.HS256;
    }

    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    Date issuedAt = calendar.getTime();
    calendar.add(Calendar.MINUTE, 5);
    Date expirationTime = calendar.getTime();

    Jwt clientAssertion = new Jwt();
    // Header
    clientAssertion.getHeader().setType(JwtType.JWT);
    clientAssertion.getHeader().setAlgorithm(algorithm);
    if (StringUtils.isNotBlank(keyId)) {
        clientAssertion.getHeader().setKeyId(keyId);
    }

    // Claims
    clientAssertion.getClaims().setIssuer(getAuthUsername());
    clientAssertion.getClaims().setSubjectIdentifier(getAuthUsername());
    clientAssertion.getClaims().setAudience(audience);
    clientAssertion.getClaims().setJwtId(UUID.randomUUID());
    clientAssertion.getClaims().setExpirationTime(expirationTime);
    clientAssertion.getClaims().setIssuedAt(issuedAt);

    // Signature
    try {
        if (sharedKey == null) {
            sharedKey = getAuthPassword();
        }
        String signature = cryptoProvider.sign(clientAssertion.getSigningInput(), keyId, sharedKey, algorithm);
        clientAssertion.setEncodedSignature(signature);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }

    return clientAssertion.toString();
}