本文整理匯總了Java中com.nimbusds.jwt.JWTClaimsSet.Builder方法的典型用法代碼示例。如果您正苦於以下問題:Java JWTClaimsSet.Builder方法的具體用法?Java JWTClaimsSet.Builder怎麽用?Java JWTClaimsSet.Builder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.nimbusds.jwt.JWTClaimsSet
的用法示例。
在下文中一共展示了JWTClaimsSet.Builder方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildJwtClaimsSet
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
protected JWTClaimsSet buildJwtClaimsSet(final U profile) {
// claims builder with subject and issue time
final JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder()
.subject(profile.getTypedId())
.issueTime(new Date());
// add attributes
final Map<String, Object> attributes = profile.getAttributes();
for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
builder.claim(entry.getKey(), entry.getValue());
}
builder.claim(INTERNAL_ROLES, profile.getRoles());
builder.claim(INTERNAL_PERMISSIONS, profile.getPermissions());
// claims
return builder.build();
}
示例2: getIdTokenClaims
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
protected JWTClaimsSet getIdTokenClaims(@Nonnull ClientID clientId, @Nullable Nonce nonce,
@Nullable AccessTokenHash atHash, @Nullable CodeHash cHash) throws ParseException {
UserInfo ui = getUserInfo();
JWTClaimsSet.Builder cb = new JWTClaimsSet.Builder(ui.toJWTClaimsSet());
cb.issuer(getTokenIssuer().getValue());
cb.audience(getTokenAudience(clientId));
cb.issueTime(getTokenIssuedAt());
cb.expirationTime(getTokenExpiration());
if (nonce != null) {
cb.claim("nonce", nonce.getValue());
}
if (atHash != null) {
cb.claim("at_hash", atHash.getValue());
}
if (cHash != null) {
cb.claim("c_hash", cHash.getValue());
}
JWTClaimsSet claims = cb.build();
return claims;
}
示例3: setUp
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
secretKey = TOKEN.getBytes();
sessionIdentifier = spy(new SessionIdentifier(TOKEN, secretKey, authenticationData));
signer = new MACSigner(secretKey);
whenNew(MACSigner.class).withArguments(secretKey).thenReturn(signer);
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer(USER);
builder.claim("mode", MODE);
JWTClaimsSet buildClaim = builder.build();
when(authenticationData.buildClaimSet()).thenReturn(buildClaim);
jwsHeader = new JWSHeader(JWSAlgorithm.HS256);
signedJWT = spy(new SignedJWT(jwsHeader, buildClaim));
whenNew(SignedJWT.class).withAnyArguments().thenReturn(signedJWT);
}
示例4: createToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
default String createToken(Object userId) {
try {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer(getIssuer());
builder.subject(userId.toString());
builder.issueTime(new Date());
builder.notBeforeTime(new Date());
builder.expirationTime(new Date(new Date().getTime() + getExpirationDate()));
builder.jwtID(UUID.randomUUID().toString());
JWTClaimsSet claimsSet = builder.build();
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
Payload payload = new Payload(claimsSet.toJSONObject());
JWSObject jwsObject = new JWSObject(header, payload);
JWSSigner signer = new MACSigner(getSharedKey());
jwsObject.sign(signer);
return jwsObject.serialize();
} catch (JOSEException ex) {
return null;
}
}
示例5: encryptIdToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
/**
* Encrypt id token.
*
* @param client the client
* @param idClaims the id claims
*/
private JWT encryptIdToken(final ClientDetailsEntity client, final JWTClaimsSet.Builder idClaims) {
log.debug("Locating encrypter service for client {}", client.getClientId());
final JWTEncryptionAndDecryptionService encrypter = encrypters.getEncrypter(client);
if (encrypter == null) {
log.error("Couldn't find encrypter for client: {} ", client.getClientId());
return null;
}
log.debug("Found encrypter service for client {}.", client.getClientId());
final JWTClaimsSet claims = idClaims.build();
final EncryptedJWT idToken = new EncryptedJWT(new JWEHeader(client.getIdTokenEncryptedResponseAlg(),
client.getIdTokenEncryptedResponseEnc()), claims);
log.debug("Encrypting idToken with response alg {} and response encoding {} and claims {}",
client.getIdTokenEncryptedResponseAlg(),
client.getIdTokenEncryptedResponseEnc(), claims.getClaims().keySet());
encrypter.encryptJwt(idToken);
return idToken;
}
示例6: calculateAmrAndAcrClaims
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
/**
* Calculate amr and acr claims.
*
* @param accessToken the access token
* @param idClaims the id claims
*/
private void calculateAmrAndAcrClaims(final OAuth2AccessTokenEntity accessToken,
final JWTClaimsSet.Builder idClaims) {
final OAuth2Authentication authN = accessToken.getAuthenticationHolder().getAuthentication();
final Collection<GrantedAuthority> authorities = authN.getAuthorities();
for (final GrantedAuthority authority : authorities) {
log.debug("Evaluating authority {} of the authentication", authority);
final AuthenticationClassRefAuthority acr =
AuthenticationClassRefAuthority.getAuthenticationClassRefAuthority(authority);
if (acr != null) {
idClaims.claim(OIDCConstants.ACR, acr.getAuthority());
log.debug("Added {} claim as {}", OIDCConstants.ACR, acr.getAuthority());
}
final AuthenticationMethodRefAuthority amr =
AuthenticationMethodRefAuthority.getAuthenticationClassRefAuthority(authority);
if (amr != null) {
idClaims.claim(OIDCConstants.AMR, amr.getAuthority());
log.debug("Added {} claim as {}", OIDCConstants.AMR, amr.getAuthority());
}
}
}
示例7: generateJWToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
/**
* Generates a new JWT token.
* @param user a User object belonging to the app
* @param app the app object
* @return a new JWT or null
*/
public static SignedJWT generateJWToken(User user, App app) {
if (app != null) {
try {
Date now = new Date();
JWTClaimsSet.Builder claimsSet = new JWTClaimsSet.Builder();
String userSecret = "";
claimsSet.issueTime(now);
claimsSet.expirationTime(new Date(now.getTime() + (app.getTokenValiditySec() * 1000)));
claimsSet.notBeforeTime(now);
claimsSet.claim("refresh", getNextRefresh(app.getTokenValiditySec()));
claimsSet.claim(Config._APPID, app.getId());
if (user != null) {
claimsSet.subject(user.getId());
userSecret = user.getTokenSecret();
}
JWSSigner signer = new MACSigner(app.getSecret() + userSecret);
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet.build());
signedJWT.sign(signer);
return signedJWT;
} catch (JOSEException e) {
logger.warn("Unable to sign JWT: {}.", e.getMessage());
}
}
return null;
}
示例8: generateToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
/**
* Generate token string.
*
* @param service the service
* @param parameters the parameters
* @return the jwt
*/
protected String generateToken(final Service service, final Map<String, String> parameters) {
try {
final String ticketId = parameters.get(CasProtocolConstants.PARAMETER_TICKET);
final Cas30ServiceTicketValidator validator = new Cas30ServiceTicketValidator(casProperties.getServer().getPrefix());
final Assertion assertion = validator.validate(ticketId, service.getId());
final JWTClaimsSet.Builder claims =
new JWTClaimsSet.Builder()
.audience(service.getId())
.issuer(casProperties.getServer().getPrefix())
.jwtID(ticketId)
.issueTime(assertion.getAuthenticationDate())
.subject(assertion.getPrincipal().getName());
assertion.getAttributes().forEach(claims::claim);
assertion.getPrincipal().getAttributes().forEach(claims::claim);
if (assertion.getValidUntilDate() != null) {
claims.expirationTime(assertion.getValidUntilDate());
} else {
final ZonedDateTime dt = ZonedDateTime.now().plusSeconds(ticketGrantingTicketExpirationPolicy.getTimeToLive());
claims.expirationTime(DateTimeUtils.dateOf(dt));
}
final JWTClaimsSet claimsSet = claims.build();
final JSONObject object = claimsSet.toJSONObject();
return tokenCipherExecutor.encode(object.toJSONString());
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
示例9: createToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
private static String createToken(Info info) {
JWTClaimsSet.Builder claimsSetBuilder = new JWTClaimsSet.Builder();
claimsSetBuilder.subject(info.getUserName());
claimsSetBuilder.issueTime(new Date());
claimsSetBuilder.expirationTime(new Date(new Date().getTime() + 30 * 1000));
JSONArray roleValues = new JSONArray();
roleValues.addAll(info.getRoles());
Map<String, Object> roles = new HashMap<>();
roles.put("roles", roleValues);
claimsSetBuilder.claim("realm_access", roles);
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS512).type(JOSEObjectType.JWT).keyID(info.getApiKey()).build();
SignedJWT signedJWT = new SignedJWT(header, claimsSetBuilder.build());
System.out.println(claimsSetBuilder.build().toJSONObject().toString(JSONStyle.NO_COMPRESS));
try {
JWSSigner signer = new RSASSASigner((RSAKey) jwkSet.getKeyByKeyId(info.getApiKey()));
signedJWT.sign(signer);
} catch (JOSEException e) {
// Should not happen
e.printStackTrace();
}
return signedJWT.serialize();
}
示例10: generate
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
/**
* Generate a JWT from a map of claims.
*
* @param claims the map of claims
* @return the created JWT
*/
public String generate(final Map<String, Object> claims) {
// claims builder
final JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
// add claims
for (final Map.Entry<String, Object> entry : claims.entrySet()) {
builder.claim(entry.getKey(), entry.getValue());
}
return internalGenerate(builder.build());
}
示例11: createToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
public Token createToken(String host, Cidadao cidadao) throws JOSEException {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder()
.subject(cidadao.getId().toString()).issuer(host)
.issueTime(DateTime.now().toDate())
.expirationTime(DateTime.now().plusDays(14).toDate());
JWSSigner signer = new MACSigner(tokenSecret);
SignedJWT jwt = new SignedJWT(jwtHeader, builder.build());
jwt.sign(signer);
return new Token(jwt.serialize());
}
示例12: buildClaimSet
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Override
public JWTClaimsSet buildClaimSet() {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer(userEmail);
builder.claim("mode", MODE);
return builder.build();
}
示例13: buildClaimSet
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Override
public JWTClaimsSet buildClaimSet() {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer(user);
builder.claim("mode", MODE);
return builder.build();
}
示例14: createToken
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
private static String createToken(Info info) {
JWTClaimsSet.Builder claimsSetBuilder = new JWTClaimsSet.Builder();
claimsSetBuilder.subject(info.getUserName());
claimsSetBuilder.issueTime(new Date());
claimsSetBuilder.expirationTime(new Date(new Date().getTime() + 60 * 1000));
JSONArray roleValues = new JSONArray();
roleValues.addAll(info.getRoles());
Map<String, Object> roles = new HashMap<>();
roles.put("roles", roleValues);
claimsSetBuilder.claim("realm_access", roles);
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.HS256).type(JOSEObjectType.JWT).keyID(info.getApiKey()).build();
SignedJWT signedJWT = new SignedJWT(header, claimsSetBuilder.build());
try {
JWSSigner signer = new MACSigner(info.getHashKey());
signedJWT.sign(signer);
} catch (JOSEException e) {
// Should not happen
e.printStackTrace();
}
return signedJWT.serialize();
}
示例15: generateClaimsSet
import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
private JWTClaimsSet generateClaimsSet(AuthenticationDto authenticationDto) {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.subject(authenticationDto.getEmail());
builder.issuer(authenticationDto.getIssuer());
return builder.build();
}