@Test public void testMultipleSecrets() throws Exception { TestSignerSecretProvider secretProvider = new TestSignerSecretProvider(); Signer signer = new Signer(secretProvider); secretProvider.setCurrentSecret("secretB"); String t1 = "test"; String s1 = signer.sign(t1); String e1 = signer.verifyAndExtract(s1); Assert.assertEquals(t1, e1); secretProvider.setPreviousSecret("secretA"); String t2 = "test"; String s2 = signer.sign(t2); String e2 = signer.verifyAndExtract(s2); Assert.assertEquals(t2, e2); Assert.assertEquals(s1, s2); // check is using current secret for signing secretProvider.setCurrentSecret("secretC"); secretProvider.setPreviousSecret("secretB"); String t3 = "test"; String s3 = signer.sign(t3); String e3 = signer.verifyAndExtract(s3); Assert.assertEquals(t3, e3); Assert.assertNotEquals(s1, s3); // check not using current secret for signing String e1b = signer.verifyAndExtract(s1); Assert.assertEquals(t1, e1b); // previous secret still valid secretProvider.setCurrentSecret("secretD"); secretProvider.setPreviousSecret("secretC"); try { signer.verifyAndExtract(s1); // previous secret no longer valid Assert.fail(); } catch (SignerException ex) { // Expected } }
@Test public void testSignature() throws Exception { Signer signer = new Signer(createStringSignerSecretProvider()); String s1 = signer.sign("ok"); String s2 = signer.sign("ok"); String s3 = signer.sign("wrong"); Assert.assertEquals(s1, s2); Assert.assertNotEquals(s1, s3); }
@Test public void testVerify() throws Exception { Signer signer = new Signer(createStringSignerSecretProvider()); String t = "test"; String s = signer.sign(t); String e = signer.verifyAndExtract(s); Assert.assertEquals(t, e); }
@Test public void testNullAndEmptyString() throws Exception { Signer signer = new Signer(createStringSignerSecretProvider()); try { signer.sign(null); Assert.fail(); } catch (IllegalArgumentException ex) { // Expected } catch (Throwable ex) { Assert.fail(); } try { signer.sign(""); Assert.fail(); } catch (IllegalArgumentException ex) { // Expected } catch (Throwable ex) { Assert.fail(); } }
@Test public void testTampering() throws Exception { Signer signer = new Signer(createStringSignerSecretProvider()); String t = "test"; String s = signer.sign(t); s += "x"; try { signer.verifyAndExtract(s); Assert.fail(); } catch (SignerException ex) { // Expected } catch (Throwable ex) { Assert.fail(); } }
public void signRequest(SignedService request) { signer.sign(request, accessToken); }