Java Code Examples for org.apache.pulsar.broker.authentication.utils.AuthTokenUtils#decodePrivateKey()

The following examples show how to use org.apache.pulsar.broker.authentication.utils.AuthTokenUtils#decodePrivateKey() . 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: JwtServiceImpl.java    From pulsar-manager with Apache License 2.0 5 votes vote down vote up
private Key decodeByPrivateKey() {
    try {
        byte[] encodedKey = AuthTokenUtils.readKeyFromUrl(jwtBrokerPrivateKey);
        SignatureAlgorithm algorithm = SignatureAlgorithm.RS256;
        return AuthTokenUtils.decodePrivateKey(encodedKey, algorithm);
    } catch (IOException e) {
        log.error("Decode failed by private key, error: {}", e.getMessage());
        return null;
    }
}
 
Example 2
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyPair() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.RS256);
    String token = AuthTokenUtils.createToken(privateKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);

    provider.close();
}
 
Example 3
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyPairWithECDSA() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.ES256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);
    // Set that we are using EC keys
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_ALG, SignatureAlgorithm.ES256.getValue());

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.ES256);
    String token = AuthTokenUtils.createToken(privateKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);

    provider.close();
}
 
Example 4
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthSecretKeyPairWithCustomClaim() throws Exception {
    String authRoleClaim = "customClaim";
    String authRole = "my-test-role";

    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);
    // Set custom claim field
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_AUTH_CLAIM, authRoleClaim);

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);


    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.RS256);
    String token = Jwts.builder()
            .setClaims(new HashMap<String, Object>() {{
                put(authRoleClaim, authRole);
            }})
            .signWith(privateKey)
            .compact();


    // Pulsar protocol auth
    String role = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(role, authRole);

    provider.close();
}