SimpleServiceIdentityProvider implements ServiceIdentityProvider { public Principal getIdentity(String domainName, String serviceName) { domainName = domainName.toLowerCase(); serviceName = serviceName.toLowerCase(); if (!domainName.equals(domain) || !serviceName.equals(service)) { return null; } PrincipalToken token = new PrincipalToken.Builder("S1", domainName, serviceName) .expirationWindow(tokenTimeout).host(host).keyId(keyId).build(); token.sign(key); SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create(domainName, serviceName, token.getSignedToken(), System.currentTimeMillis() / 1000, authority); principal.setUnsignedCreds(token.getUnsignedToken()); return principal; } SimpleServiceIdentityProvider(String domainName, String serviceName,
            PrivateKey privateKey, String keyId);  SimpleServiceIdentityProvider(String domainName, String serviceName,
            PrivateKey privateKey, String keyId, long tokenTimeout);  SimpleServiceIdentityProvider(Authority authority, String domainName,
            String serviceName, PrivateKey privateKey, String keyId, long tokenTimeout); Principal getIdentity(String domainName, String serviceName); String getHost(); void setHost(String host); void setTokenTimeout(long tokenTimeout); Authority getAuthority(); void setAuthority(Authority authority);  }
@Test public void testSimpleIdentityDefaultV0() { SimpleServiceIdentityProvider provider = new SimpleServiceIdentityProvider("coretech", "athenz", Crypto.loadPrivateKey(k0File), "0"); Principal user = provider.getIdentity("coretech", "athenz"); assertNotNull(user); assertTrue(user.getIssueTime() != 0); String token = user.getCredentials(); PrincipalToken prToken = new PrincipalToken(token); assertTrue(prToken.validate(servicePublicKeyStringK0, 0, false)); assertEquals(prToken.getKeyId(), "0"); }
@Test public void testSimpleIdentityDefaultV1() { PrivateKey key = Crypto.loadPrivateKey(servicePrivateKeyStringK1); SimpleServiceIdentityProvider provider = new SimpleServiceIdentityProvider("coretech", "athenz", key, "1"); Principal user = provider.getIdentity("coretech", "athenz"); assertNotNull(user); assertTrue(user.getIssueTime() != 0); String token = user.getCredentials(); PrincipalToken prToken = new PrincipalToken(token); assertTrue(prToken.validate(servicePublicKeyStringK1, 0, false)); assertEquals(prToken.getKeyId(), "1"); }
@Test public void testSimpleIdentityPrivateKeyDomainMismatch() { PrivateKey key = Crypto.loadPrivateKey(servicePrivateKeyStringK1); SimpleServiceIdentityProvider provider = new SimpleServiceIdentityProvider("coretech", "athenz", key, "1"); Principal user = provider.getIdentity("coretech2", "athenz"); assertNull(user); user = provider.getIdentity("coretech", "athenz2"); assertNull(user); }