PrincipalAuthority implements Authority, AuthorityKeyStore { public PrincipalAuthority() { allowedOffset = Integer.parseInt(System.getProperty(ATHENZ_PROP_TOKEN_OFFSET, "300")); ipCheckMode = IpCheckMode.valueOf(System.getProperty(ATHENZ_PROP_IP_CHECK_MODE, IpCheckMode.OPS_WRITE.toString())); userDomain = System.getProperty(ATHENZ_PROP_USER_DOMAIN, USER_DOMAIN); headerName = System.getProperty(ATHENZ_PROP_PRINCIPAL_HEADER, HTTP_HEADER); if (allowedOffset < 0) { allowedOffset = 300; } } PrincipalAuthority(); @Override String getID(); @Override void initialize(); @Override String getDomain(); @Override String getHeader(); @Override String getAuthenticateChallenge(); @Override Principal authenticate(String signedToken, String remoteAddr, String httpMethod,
            StringBuilder errMsg); @Override void setKeyStore(KeyStore keyStore); static final String HTTP_HEADER; static final String ATHENZ_AUTH_CHALLENGE; static final String ATHENZ_PROP_PRINCIPAL_HEADER; }
@Test public void testPrincipalAuthority() throws IOException, CryptoException { PrincipalAuthority serviceAuthority = new PrincipalAuthority(); KeyStore keyStore = new KeyStoreMock(); serviceAuthority.setKeyStore(keyStore); assertNull(serviceAuthority.getDomain()); assertEquals(serviceAuthority.getHeader(), "Athenz-Principal-Auth"); PrincipalToken serviceToken = new PrincipalToken.Builder(svcVersion, svcDomain, svcName) .host(host).salt(salt).expirationWindow(expirationTime).build(); serviceToken.sign(servicePrivateKeyStringK0); StringBuilder errMsg = new StringBuilder(); Principal principal = serviceAuthority.authenticate( serviceToken.getSignedToken(), null, "GET", errMsg); assertNotNull(principal); assertNotNull(principal.getAuthority()); assertEquals(principal.getCredentials(), serviceToken.getSignedToken()); assertEquals(principal.getDomain(), serviceToken.getDomain()); assertEquals(principal.getName(), serviceToken.getName()); assertEquals(principal.getKeyId(), "0"); principal = serviceAuthority.authenticate( serviceToken.getSignedToken(), null, "GET", null); assertNotNull(principal); String testKeyVersionK0 = "0"; serviceToken = new PrincipalToken.Builder(svcVersion, svcDomain, svcName) .host(host).salt(salt).expirationWindow(expirationTime) .keyId(testKeyVersionK0).build(); serviceToken.sign(servicePrivateKeyStringK0); principal = serviceAuthority.authenticate(serviceToken.getSignedToken(), null, "GET", errMsg); assertNotNull(principal); assertEquals(principal.getCredentials(), serviceToken.getSignedToken()); String testKeyVersionK1 = "1"; serviceToken = new PrincipalToken.Builder(svcVersion, svcDomain, svcName) .host(host).salt(salt).expirationWindow(expirationTime).keyId(testKeyVersionK1).build(); serviceToken.sign(servicePrivateKeyStringK1); principal = serviceAuthority.authenticate(serviceToken.getSignedToken(), null, "GET", errMsg); assertNotNull(principal); assertEquals(principal.getCredentials(), serviceToken.getSignedToken()); }