Build JWT.

@param payload the payload
@return the jwt
960092
public String build(final JwtRequest payload) {
        val serviceAudience = payload.getServiceAudience();
        val claims = new JWTClaimsSet.Builder()
            .audience(serviceAudience)
            .issuer(casSeverPrefix)
            .jwtID(payload.getJwtId())
            .issueTime(payload.getIssueDate())
            .subject(payload.getSubject());

        payload.getAttributes().forEach(claims::claim);
        claims.expirationTime(payload.getValidUntilDate());

        val claimsSet = claims.build();
        val object = claimsSet.toJSONObject();

        val jwtJson = object.toJSONString();
        LOGGER.debug("Generated JWT [{}]", JsonValue.readJSON(jwtJson).toString(Stringify.FORMATTED));

        LOGGER.trace("Locating service [{}] in service registry", serviceAudience);
        val registeredService = this.servicesManager.findServiceBy(serviceAudience);
        RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(registeredService);

        LOGGER.trace("Locating service specific signing and encryption keys for [{}] in service registry", serviceAudience);
        if (registeredServiceCipherExecutor.supports(registeredService)) {
            LOGGER.trace("Encoding JWT based on keys provided by service [{}]", registeredService.getServiceId());
            return registeredServiceCipherExecutor.encode(jwtJson, Optional.of(registeredService));
        }

        if (defaultTokenCipherExecutor.isEnabled()) {
            LOGGER.trace("Encoding JWT based on default global keys for [{}]", serviceAudience);
            return defaultTokenCipherExecutor.encode(jwtJson);
        }
        val header = new PlainHeader.Builder()
            .type(JOSEObjectType.JWT)
            .build();
        val token = new PlainJWT(header, claimsSet).serialize();
        LOGGER.trace("Generating plain JWT as the ticket: [{}]", token);
        return token;
    }
974094
public String signToken(AuthenticatedUser user) {
        Date expiry = new Date(System.currentTimeMillis() + this.jwtTimeout);
        JWTClaimsSet claimsSet = user.toClaimSet(this.jwtIssuer, expiry);
        SignedJWT jwt = this.signToken(claimsSet);
        return jwt.serialize();
    }
1038048
public function jwtPayload($path = null, $request = null)
    {
        $jwt = $this->jwtToken($request);

        return $jwt->payload($path);
    }
639276
@Override
    public Builder create() throws InvalidBuilderException {
        if (!active) {
            // Tr.error(tc, "JWT_BUILDER_NOT_ACTIVE", new Object[] {
            // "defaultJWT" });
            // return null;
            String err = Tr.formatMessage(tc, "JWT_BUILDER_NOT_ACTIVE", new Object[] { "defaultJWT" });
            throw new InvalidBuilderException(err);
        }
        return create("defaultJWT");
    }
715907
public JwtClaims getJwtClaims(String jwt) throws JoseException {
        String methodName = "getJwtClaims";
        if (tc.isDebugEnabled()) {
            Tr.entry(tc, methodName, jwt);
        }
        JwtClaims jwtclaims = new JwtClaims();
        String payload = getJwtPayload(jwt);
        if (payload != null) {
            jwtclaims = getClaimsFromJwtPayload(jwt, payload);
        }
        if (tc.isDebugEnabled()) {
            Tr.exit(tc, methodName, jwtclaims);
        }
        return jwtclaims;
    }
1014144
protected JwtSigner createSigner(SignatureAlgorithm alg, Key key) {
        return new DefaultJwtSigner(alg, key, base64UrlEncoder);
    }
565898
def generate_jwt(api_key, api_secret)
      now = Time.now.to_i
      payload = {
        :iss => api_key,
        :iat => now,
        :exp => now + AUTH_EXPIRE
      }
      token = JWT.encode payload, api_secret, 'HS256', :ist => 'project'
      token
    end
433417
public JwtToken refreshToken(String token) {
        String refreshedToken;
        Date expireTime = generateExpirationDate();
        try {
            final Claims claims = getClaimsFromToken(token);
            claims.put(CLAIM_KEY_CREATED, expireTime.getTime());
            refreshedToken = build(claims, expireTime);
        } catch (Exception e) {
            log.error("refreshedToken jwt token error : {}", e.getMessage());
            refreshedToken = null;
        }
        return new JwtToken(refreshedToken, expireTime.getTime());
    }
895680
private synchronized Jwt getJwt(ICacheStrategy cacheStrategy, Jwt.Key key) {
        Jwt result = cacheStrategy.getCachedJwt(key);
        if(result == null) {
            //cache an empty JWT first.
            result = new Jwt(key);
            cacheStrategy.cacheJwt(key, result);
        }
        return result;
    }
812359
public function encode(array $payload)
    {
        // Remove the signature on the builder instance first.
        $this->builder->unsign();

        try {
            foreach ($payload as $key => $value) {
                $this->builder->set($key, $value);
            }
            $this->builder->sign($this->signer, $this->getSigningKey());
        } catch (Exception $e) {
            throw new JWTException('Could not create token: '.$e->getMessage(), $e->getCode(), $e);
        }

        return (string) $this->builder->getToken();
    }
555305
protected int addPayload(RestRequestBuilder builder, String payload) {
    if (payload == null || payload.length() == 0) {
      return 0;
    }

    builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON);
    try {
      DataMap data = JACKSON_DATA_CODEC.stringToMap(payload);
      byte[] bytes = JACKSON_DATA_CODEC.mapToBytes(data);
      builder.setEntity(bytes);
      return bytes.length;
    } catch (IOException e) {
      throw new RuntimeException("Fail to convert payload: " + payload, e);
    }
  }
378402
@Override
    public synchronized Builder create(String builderConfigId) throws InvalidBuilderException {
        if (builderConfigId == null || builderConfigId.isEmpty()) {
            String err = Tr.formatMessage(tc, "JWT_BUILDER_INVALID", new Object[] { builderConfigId });
            throw new InvalidBuilderException(err);
        }
        if (!active) {
            String err = Tr.formatMessage(tc, "JWT_BUILDER_NOT_ACTIVE", new Object[] { builderConfigId });
            throw new InvalidBuilderException(err);
        }

        // Tr.error(tc, "JWT_BUILDER_NOT_ACTIVE", new Object[] { builderConfigId
        // });
        // return null;
        return new BuilderImpl(builderConfigId);
    }
1072830
public T payloadModel(Object payload) {
        Assert.notNull(applicationContext, "Citrus application context is not initialized!");

        if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(Marshaller.class))) {
            return payload(payload, applicationContext.getBean(Marshaller.class));
        } else if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(ObjectMapper.class))) {
            return payload(payload, applicationContext.getBean(ObjectMapper.class));
        }

        throw new CitrusRuntimeException("Unable to find default object mapper or marshaller in application context");
    }
307462
public JWTPayload setIssuer(String issuer) {
        this.issuer = issuer;
        this.put(PayloadConstants.ISSUER, issuer);
        return this;
    }
1224766
public static Payload create(String data) {
    return create(ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, data), null);
  }
280125
public static <Payload> Message<Payload> create(Payload payload) {
    return new Message<>(payload, HashTreePMap.empty());
  }
125662
public static Map<String, Object> verify(String token, final RSAPublicKey pk) throws InvalidTokenException {
        // RS256签名验证器
        Verifier verifier = new Verifier() {
            @Override
            public boolean verifySignature(String content, String payload, String signature) {
                return JWT.verifySignature(content,signature,pk);
            }
        };
        return verify(token, verifier);
    }