PrincipalToken extends Token { public String getName() { return name; } 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 testServiceToken() throws CryptoException { String testKeyVersionK1 = "1"; PrincipalToken serviceTokenToValidate = createServiceToken(testKeyVersionK1); assertEquals(serviceTokenToValidate.getVersion(), svcVersion); assertEquals(serviceTokenToValidate.getDomain(), svcDomain); assertEquals(serviceTokenToValidate.getName(), svcName); assertEquals(serviceTokenToValidate.getHost(), host); assertEquals(serviceTokenToValidate.getSalt(), salt); assertEquals(serviceTokenToValidate.getKeyId(), testKeyVersionK1); assertTrue(serviceTokenToValidate.validate(servicePublicKeyStringK1, 300, false)); serviceTokenToValidate = createServiceToken(); assertEquals(serviceTokenToValidate.getKeyId(), "0"); assertTrue(serviceTokenToValidate.validate(servicePublicKeyStringK0, 300, false)); PrincipalToken svcToken2 = new PrincipalToken(serviceTokenToValidate.getSignedToken()); assertEquals(svcToken2.getSignedToken(), serviceTokenToValidate.getSignedToken()); }
@Test public void testUserToken() throws CryptoException { long issueTime = System.currentTimeMillis() / 1000; PrincipalToken userTokenToValidate = createUserToken(issueTime); assertEquals(userTokenToValidate.getVersion(), usrVersion); assertEquals(userTokenToValidate.getDomain(), usrDomain); assertEquals(userTokenToValidate.getName(), usrName); assertNull(userTokenToValidate.getHost()); assertEquals(userTokenToValidate.getSalt(), salt); assertTrue(userTokenToValidate.validate(servicePublicKeyStringK0, 300, false)); }
@Test public void testTokenWithoutSignature() { PrincipalToken token = new PrincipalToken("v=S1;d=coretech;n=storage"); assertEquals(token.getDomain(), "coretech"); assertEquals(token.getName(), "storage"); assertEquals(token.getVersion(), "S1"); assertNull(token.getUnsignedToken()); }
@Test public void testTokenInvalidVersionValue() { PrincipalToken token = new PrincipalToken("v=S1=S2;d=coretech;n=storage"); assertEquals(token.getName(), "storage"); assertEquals(token.getDomain(), "coretech"); assertNull(token.getVersion()); }
@Test public void testBuilderDefaultOptionalValues() { PrincipalToken token = new PrincipalToken.Builder(svcVersion, svcDomain, svcName).build(); assertEquals(token.getVersion(), svcVersion); assertEquals(token.getDomain(), svcDomain); assertEquals(token.getName(), svcName); assertNull(token.getHost()); assertNotNull(token.getSalt()); assertEquals(token.getKeyId(), "0"); assertNull(token.getIP()); long timestamp = token.getTimestamp(); assertTrue(timestamp != 0); assertEquals(token.getExpiryTime(), timestamp + 3600); }
@Test public void testTokenWithExtraArgsAfterSignature() { PrincipalToken token = new PrincipalToken("v=S1;d=coretech;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1;s=signature;d=domain2;n=api"); assertEquals(token.getDomain(), "coretech"); assertEquals(token.getVersion(), "S1"); assertEquals(token.getName(), "service"); assertEquals(token.getSignature(), "signature;d=domain2;n=api"); assertEquals(token.getUnsignedToken(), "v=S1;d=coretech;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1"); token = new PrincipalToken("v=S1;d=coretech;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1;s=signature;d=domain2;n=api;bs=svc-signature;d=domain2;n=api"); assertEquals(token.getDomain(), "coretech"); assertEquals(token.getVersion(), "S1"); assertEquals(token.getName(), "service"); assertEquals(token.getSignature(), "signature"); assertEquals(token.getUnsignedToken(), "v=S1;d=coretech;n=service;t=1234;e=1235;k=0;h=host1;i=1.2.3.4;b=svc1"); }