examples/iot-nirvana/client/src/main/java/com/google/cloud/demo/iot/nirvana/client/IotDevice.java
/**
   * Create a JWT to authenticate this device. The device will be disconnected after the token
   * expires, and will have to reconnect with a new token. The audience field should always be set
   * to the GCP project id.
   */
Create a JWT to authenticate this device. The device will be disconnected after the token
expires, and will have to reconnect with a new token. The audience field should always be set
to the GCP project id.
Create a JWT to authenticate this device . The device will be disconnected after the token expires and will have to reconnect with a new token . The audience field should always be set to the GCP project id .
String createJwtRsa() throws ClientException {
    try {
      DateTime now = new DateTime();
      JwtBuilder jwtBuilder =
          Jwts.builder()
              .setIssuedAt(now.toDate())
              .setExpiration(now.plusMinutes(20).toDate())
              .setAudience(getGcpProjectId());
      byte[] keyBytes = java.nio.file.Files.readAllBytes(Paths.get(getPrivateKey()));
      PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
      KeyFactory kf = KeyFactory.getInstance("RSA");
      return jwtBuilder.signWith(SignatureAlgorithm.RS256, kf.generatePrivate(spec)).compact();
    } catch (IOException e) {
      String message = String.format("Cannot create JWT. Cause: %s", e.getMessage());
      LogUtils.logDebug(LOGGER, getDeviceId(), getGcpProjectId(), message);
      throw new ClientException(message, e);
    } catch (NoSuchAlgorithmException e) {
      String message = String.format("Cannot create JWT. Cause: %s", e.getMessage());
      LogUtils.logDebug(LOGGER, getDeviceId(), getGcpProjectId(), message);
      throw new ClientException(message, e);
    } catch (InvalidKeySpecException e) {
      String message = String.format("Cannot create JWT. Cause: %s", e.getMessage());
      LogUtils.logDebug(LOGGER, getDeviceId(), getGcpProjectId(), message);
      throw new ClientException(message, e);
    }
  }
String createJwtRsa ( ) throws ClientException { try { DateTime now = new DateTime ( ) ; JwtBuilder jwtBuilder = Jwts . builder ( ) . setIssuedAt ( now . toDate ( ) ) . setExpiration ( now . plusMinutes ( 20 ) . toDate ( ) ) . setAudience ( getGcpProjectId ( ) ) ; byte [ ] keyBytes = java . nio . file . Files . readAllBytes ( Paths . get ( getPrivateKey ( ) ) ) ; PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec ( keyBytes ) ; KeyFactory kf = KeyFactory . getInstance ( "RSA" ) ; return jwtBuilder . signWith ( SignatureAlgorithm . RS256 , kf . generatePrivate ( spec ) ) . compact ( ) ; } catch ( IOException e ) { String message = String . format ( "Cannot create JWT. Cause: %s" , e . getMessage ( ) ) ; LogUtils . logDebug ( LOGGER , getDeviceId ( ) , getGcpProjectId ( ) , message ) ; throw new ClientException ( message , e ) ; } catch ( NoSuchAlgorithmException e ) { String message = String . format ( "Cannot create JWT. Cause: %s" , e . getMessage ( ) ) ; LogUtils . logDebug ( LOGGER , getDeviceId ( ) , getGcpProjectId ( ) , message ) ; throw new ClientException ( message , e ) ; } catch ( InvalidKeySpecException e ) { String message = String . format ( "Cannot create JWT. Cause: %s" , e . getMessage ( ) ) ; LogUtils . logDebug ( LOGGER , getDeviceId ( ) , getGcpProjectId ( ) , message ) ; throw new ClientException ( message , e ) ; } }
Create a JWT to authenticate this device.
Create a JWT to authenticate this device .