PrincipalToken extends Token { public void signForAuthorizedService(String authorizedServiceName, String authorizedServiceKeyId, String privKey) throws CryptoException { signForAuthorizedService(authorizedServiceName, authorizedServiceKeyId, Crypto.loadPrivateKey(privKey)); } private  PrincipalToken(Builder builder);  PrincipalToken(String signedToken); void signForAuthorizedService(String authorizedServiceName, String authorizedServiceKeyId,
            String privKey); void signForAuthorizedService(String authorizedServiceName, String authorizedServiceKeyId,
            PrivateKey key); boolean validateForAuthorizedService(String pubKey, StringBuilder errMsg); boolean isValidAuthorizedServiceToken(StringBuilder errMsg); String getName(); String getKeyService(); String getOriginalRequestor(); List<String> getAuthorizedServices(); String getAuthorizedServiceName(); String getAuthorizedServiceKeyId(); String getAuthorizedServiceSignature();  }
@Test public void testUserTokenWithNullAuthorizedService() throws CryptoException { long issueTime = System.currentTimeMillis() / 1000; PrincipalToken userTokenToSign = new PrincipalToken.Builder(usrVersion, usrDomain, usrName) .salt(salt).issueTime(issueTime).expirationWindow(expirationTime) .build(); userTokenToSign.sign(servicePrivateKeyStringK0); try { userTokenToSign.signForAuthorizedService("coretech.storage", "0", servicePrivateKeyStringK0); fail(); } catch (Exception ex) { assertEquals(ex.getClass(), IllegalArgumentException.class); } }
@Test public void testUserTokenWithInvalidAuthorizedService() throws CryptoException { long issueTime = System.currentTimeMillis() / 1000; List<String> authorizedServices = new ArrayList<>(); authorizedServices.add("coretech.storage"); PrincipalToken userTokenToSign = new PrincipalToken.Builder(usrVersion, usrDomain, usrName) .salt(salt).issueTime(issueTime).expirationWindow(expirationTime) .authorizedServices(authorizedServices).build(); userTokenToSign.sign(servicePrivateKeyStringK0); try { userTokenToSign.signForAuthorizedService("weather.storage", "0", servicePrivateKeyStringK0); fail(); } catch (Exception ex) { assertEquals(ex.getClass(), IllegalArgumentException.class); } authorizedServices.add("media.storage"); PrincipalToken userTokenToSign2 = new PrincipalToken.Builder(usrVersion, usrDomain, usrName) .salt(salt).issueTime(issueTime).expirationWindow(expirationTime) .authorizedServices(authorizedServices).build(); userTokenToSign2.sign(servicePrivateKeyStringK0); try { userTokenToSign2.signForAuthorizedService("weather.storage", "0", servicePrivateKeyStringK0); fail(); } catch (Exception ex) { assertEquals(ex.getClass(), IllegalArgumentException.class); } }