示例#1
0
 private void setAtHashAndNonce(IdToken idToken, ServerAccessToken st) {
   if (idToken.getAccessTokenHash() == null) {
     Properties props = JwsUtils.loadSignatureOutProperties(false);
     SignatureAlgorithm sigAlgo = null;
     if (super.isSignWithClientSecret()) {
       sigAlgo = OAuthUtils.getClientSecretSignatureAlgorithm(props);
     } else {
       sigAlgo = JwsUtils.getSignatureAlgorithm(props, SignatureAlgorithm.RS256);
     }
     if (sigAlgo != SignatureAlgorithm.NONE) {
       String atHash = OidcUtils.calculateAccessTokenHash(st.getTokenKey(), sigAlgo);
       idToken.setAccessTokenHash(atHash);
     }
   }
   Message m = JAXRSUtils.getCurrentMessage();
   if (m != null && m.getExchange().containsKey(OAuthConstants.NONCE)) {
     idToken.setNonce((String) m.getExchange().get(OAuthConstants.NONCE));
   } else if (st.getNonce() != null) {
     idToken.setNonce(st.getNonce());
   }
 }
示例#2
0
  public static String createToken(
      String issuer, String subject, String audience, boolean expiry, boolean sign) {
    // Create the JWT Token
    JwtClaims claims = new JwtClaims();
    claims.setSubject(subject);
    if (issuer != null) {
      claims.setIssuer(issuer);
    }
    claims.setIssuedAt(new Date().getTime() / 1000L);
    if (expiry) {
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.SECOND, 60);
      claims.setExpiryTime(cal.getTimeInMillis() / 1000L);
    }
    if (audience != null) {
      claims.setAudiences(Collections.singletonList(audience));
    }

    if (sign) {
      // Sign the JWT Token
      Properties signingProperties = new Properties();
      signingProperties.put("rs.security.keystore.type", "jks");
      signingProperties.put("rs.security.keystore.password", "password");
      signingProperties.put("rs.security.keystore.alias", "alice");
      signingProperties.put(
          "rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/alice.jks");
      signingProperties.put("rs.security.key.password", "password");
      signingProperties.put("rs.security.signature.algorithm", "RS256");

      JwsHeaders jwsHeaders = new JwsHeaders(signingProperties);
      JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);

      JwsSignatureProvider sigProvider =
          JwsUtils.loadSignatureProvider(signingProperties, jwsHeaders);

      return jws.signWith(sigProvider);
    }

    JwsHeaders jwsHeaders = new JwsHeaders(SignatureAlgorithm.NONE);
    JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);
    return jws.getSignedEncodedJws();
  }