PrincipalToken extends Token { public boolean isValidAuthorizedServiceToken(StringBuilder errMsg) { errMsg = errMsg == null ? new StringBuilder(512) : errMsg; if (authorizedServices == null) { if (authorizedServiceSignature == null) { return true; } errMsg.append("PrincipalToken:isValidAuthorizedServiceToken: Invalid Token="). append(unsignedToken). append(" : Authorized Service Signature available without service name"); LOG.error(errMsg.toString()); return false; } if (authorizedServiceSignature == null) { errMsg.append("PrincipalToken:isValidAuthorizedServiceToken: Invalid Token="). append(unsignedToken). append(" : Missing signature for specified authorized service"); LOG.error(errMsg.toString()); return false; } if (authorizedServiceName != null) { if (!authorizedServices.contains(authorizedServiceName)) { errMsg.append("PrincipalToken:isValidAuthorizedServiceToken: Invalid Token="). append(unsignedToken). append(" : Authorized service name=").append(authorizedServiceName). append(" is not listed in the service list"); LOG.error(errMsg.toString()); return false; } } else if (authorizedServices.size() != 1) { errMsg.append("PrincipalToken:isValidAuthorizedServiceToken: Invalid Token="). append(unsignedToken). append(" : No service name and Authorized service list contains multiple entries"); LOG.error(errMsg.toString()); return false; } return true; } 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 testIsValidAuthorizedServiceTokenStandardToken() { String token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;s=signature"; PrincipalToken svcToken = new PrincipalToken(token); StringBuilder errMsg = new StringBuilder(); assertTrue(svcToken.isValidAuthorizedServiceToken(errMsg)); }
@Test public void testIsValidAuthorizedServiceTokenValidSvcToken() { String token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1;s=signature;bk=0;bn=svc1;bs=signature"; PrincipalToken svcToken = new PrincipalToken(token); StringBuilder errMsg = new StringBuilder(); assertTrue(svcToken.isValidAuthorizedServiceToken(errMsg)); token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1;s=signature;bk=0;bs=signature"; svcToken = new PrincipalToken(token); assertTrue(svcToken.isValidAuthorizedServiceToken(null)); token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1,svc2;s=signature;bk=0;bn=svc1;bs=signature"; svcToken = new PrincipalToken(token); assertTrue(svcToken.isValidAuthorizedServiceToken(errMsg)); }
@Test public void testIsValidAuthorizedServiceTokenSvcYesSigNo() { String token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1;s=signature"; PrincipalToken svcToken = new PrincipalToken(token); StringBuilder errMsg = new StringBuilder(); assertFalse(svcToken.isValidAuthorizedServiceToken(errMsg)); assertTrue(!errMsg.toString().isEmpty()); }
@Test public void testIsValidAuthorizedServiceTokenSvcNoSigYes() { String token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;s=signature;bk=0;bs=signature"; PrincipalToken svcToken = new PrincipalToken(token); StringBuilder errMsg = new StringBuilder(); assertFalse(svcToken.isValidAuthorizedServiceToken(errMsg)); assertTrue(!errMsg.toString().isEmpty()); }
@Test public void testIsValidAuthorizedServiceTokenUnknownSvc() { String token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1;s=signature;bk=0;bn=svc3;bs=signature"; PrincipalToken svcToken = new PrincipalToken(token); StringBuilder errMsg = new StringBuilder(); assertFalse(svcToken.isValidAuthorizedServiceToken(errMsg)); assertTrue(!errMsg.toString().isEmpty()); token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1,svc2;s=signature;bk=0;bn=svc3;bs=signature"; svcToken = new PrincipalToken(token); errMsg = new StringBuilder(); assertFalse(svcToken.isValidAuthorizedServiceToken(errMsg)); assertTrue(!errMsg.toString().isEmpty()); }
@Test public void testIsValidAuthorizedServiceTokenNoSvcNotSingle() { String token = "v=S1;d=domain;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1,svc2;s=signature;bk=0;bs=signature"; PrincipalToken svcToken = new PrincipalToken(token); StringBuilder errMsg = new StringBuilder(); assertFalse(svcToken.isValidAuthorizedServiceToken(errMsg)); assertTrue(!errMsg.toString().isEmpty()); }