Merge pull request #361 from jwtk/304-spec-date-claims

Claims specification date enhancments
This commit is contained in:
Les Hazlewood 2018-07-23 17:22:41 -04:00 committed by GitHub
commit c9d05361fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 6 deletions

View File

@ -117,6 +117,15 @@ public class DefaultClaims extends JwtMap implements Claims {
Claims.NOT_BEFORE.equals(claimName); Claims.NOT_BEFORE.equals(claimName);
} }
@Override
public Object put(String s, Object o) {
if (o instanceof Date && isSpecDate(s)) { //since 0.10.0
Date date = (Date)o;
return setDateAsSeconds(s, date);
}
return super.put(s, o);
}
@Override @Override
public <T> T get(String claimName, Class<T> requiredType) { public <T> T get(String claimName, Class<T> requiredType) {

View File

@ -31,12 +31,13 @@ public class JwtMap implements Map<String, Object> {
private final Map<String, Object> map; private final Map<String, Object> map;
public JwtMap() { public JwtMap() {
this(new LinkedHashMap<String, Object>()); this.map = new LinkedHashMap<>();
} }
public JwtMap(Map<String, Object> map) { public JwtMap(Map<String, Object> map) {
this();
Assert.notNull(map, "Map argument cannot be null."); Assert.notNull(map, "Map argument cannot be null.");
this.map = map; putAll(map);
} }
protected String getString(String name) { protected String getString(String name) {
@ -118,12 +119,12 @@ public class JwtMap implements Map<String, Object> {
} }
} }
protected void setDateAsSeconds(String name, Date d) { protected Object setDateAsSeconds(String name, Date d) {
if (d == null) { if (d == null) {
map.remove(name); return map.remove(name);
} else { } else {
long seconds = d.getTime() / 1000; long seconds = d.getTime() / 1000;
map.put(name, seconds); return map.put(name, seconds);
} }
} }
@ -173,7 +174,7 @@ public class JwtMap implements Map<String, Object> {
return; return;
} }
for (String s : m.keySet()) { for (String s : m.keySet()) {
map.put(s, m.get(s)); put(s, m.get(s));
} }
} }

View File

@ -192,4 +192,93 @@ class DefaultClaimsTest {
assertEquals(expected, claims.getNotBefore()) assertEquals(expected, claims.getNotBefore())
} }
@Test
void testPutWithIat() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
claims.put('iat', now) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('iat') //conversion should have happened
}
@Test
void testPutAllWithIat() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
claims.putAll([iat: now]) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('iat') //conversion should have happened
}
@Test
void testConstructorWithIat() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
this.claims = new DefaultClaims([iat: now]) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('iat') //conversion should have happened
}
@Test
void testPutWithNbf() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
claims.put('nbf', now) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('nbf') //conversion should have happened
}
@Test
void testPutAllWithNbf() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
claims.putAll([nbf: now]) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('nbf') //conversion should have happened
}
@Test
void testConstructorWithNbf() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
this.claims = new DefaultClaims([nbf: now]) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('nbf') //conversion should have happened
}
@Test
void testPutWithExp() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
claims.put('exp', now) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('exp') //conversion should have happened
}
@Test
void testPutAllWithExp() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
claims.putAll([exp: now]) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('exp') //conversion should have happened
}
@Test
void testConstructorWithExp() {
long millis = System.currentTimeMillis()
long seconds = millis / 1000 as long
Date now = new Date(millis)
this.claims = new DefaultClaims([exp: now]) //this should convert 'now' to seconds since epoch
assertEquals seconds, claims.get('exp') //conversion should have happened
}
@Test
void testPutWithNonSpecDate() {
long millis = System.currentTimeMillis()
Date now = new Date(millis)
claims.put('foo', now)
assertEquals now, claims.get('foo') //conversion should NOT have occurred
}
} }