Skip to content

Commit

Permalink
Use local timezones for non-Unix OSes. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
whikloj authored and dannylamb committed Apr 28, 2017
1 parent 99ed831 commit bb6a59d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
36 changes: 24 additions & 12 deletions src/test/java/ca/islandora/syn/token/VerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,45 @@

import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.junit.Test;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAKey;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

public class VerifierTest {

private static String token;

private static ZoneOffset offset;

@Before
public void setUp() {
offset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
}

@Test
public void testClaimsWithoutVerify() {
token = JWT.create()
.withArrayClaim("roles", new String[]{"Role1", "Role2"})
.withClaim("uid", 1)
.withClaim("name", "admin")
.withClaim("url", "http://test.com")
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.none());
final Verifier verifier = Verifier.create(token);
assertEquals(1, verifier.getUid());
Expand Down Expand Up @@ -64,8 +76,8 @@ public void testClaimsAndVerifyHmac() throws Exception {
.withClaim("uid", 1)
.withClaim("name", "admin")
.withClaim("url", "http://test.com")
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.HMAC256("secret"));

final Verifier verifier = Verifier.create(token);
Expand Down Expand Up @@ -93,8 +105,8 @@ public void testClaimsAndVerifyRsa() throws Exception {
.withClaim("uid", 1)
.withClaim("name", "admin")
.withClaim("url", "http://test.com")
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.RSA512(privateKey));

final Verifier verifier = Verifier.create(token);
Expand All @@ -117,8 +129,8 @@ public void testClaimsAndVerifyHmacBadIssueDate() throws Exception {
.withClaim("uid", 1)
.withClaim("name", "admin")
.withClaim("url", "http://test.com")
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().minusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().minusHours(2).toInstant(offset)))
.sign(Algorithm.HMAC256("secret"));

final Verifier verifier = Verifier.create(token);
Expand Down
21 changes: 13 additions & 8 deletions src/test/java/ca/islandora/syn/valves/SynValveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -62,6 +64,8 @@ public class SynValveTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private static ZoneOffset offset;

@Before
public void setUp() throws Exception {
settings = temporaryFolder.newFile();
Expand All @@ -74,6 +78,7 @@ public void setUp() throws Exception {

when(container.getRealm()).thenReturn(realm);
when(request.getContext()).thenReturn(context);
offset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
}

@Test
Expand All @@ -97,8 +102,8 @@ public void shouldPassAuth() throws Exception {
.withClaim("name", "adminuser")
.withClaim("url", "http://test.com")
.withArrayClaim("roles", new String[] {"role1", "role2", "role3"})
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.HMAC256("secret"));

final SecurityConstraint securityConstraint = new SecurityConstraint();
Expand Down Expand Up @@ -208,8 +213,8 @@ public void shouldFailTokenMissingUid() throws Exception {
.withClaim("name", "adminuser")
.withClaim("url", "http://test.com")
.withArrayClaim("roles", new String[] {"role1", "role2", "role3"})
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.HMAC256("secret"));

final SecurityConstraint securityConstraint = new SecurityConstraint();
Expand All @@ -234,8 +239,8 @@ public void shouldPassAuthDefaultSite() throws Exception {
.withClaim("name", "normalUser")
.withClaim("url", "http://test2.com")
.withArrayClaim("roles", new String[] {})
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.HMAC256("secret2"));

final ArgumentCaptor<GenericPrincipal> argument = ArgumentCaptor.forClass(GenericPrincipal.class);
Expand Down Expand Up @@ -272,8 +277,8 @@ public void shouldFailAuthBecauseNoSiteMatch() throws Exception {
.withClaim("name", "normalUser")
.withClaim("url", "http://test-no-match.com")
.withArrayClaim("roles", new String[] {})
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC)))
.withIssuedAt(Date.from(LocalDateTime.now().toInstant(offset)))
.withExpiresAt(Date.from(LocalDateTime.now().plusHours(2).toInstant(offset)))
.sign(Algorithm.HMAC256("secret"));

final SecurityConstraint securityConstraint = new SecurityConstraint();
Expand Down

0 comments on commit bb6a59d

Please sign in to comment.