1 module rfc7519; 2 3 public import jwt; 4 5 version (unittest) 6 { 7 void safelog(string msg) @trusted @nogc nothrow 8 { 9 import core.stdc.stdio : printf; 10 11 printf("%.*s\n", msg.length, &msg[0]); 12 } 13 14 void testFunctionality()() 15 { 16 import std.datetime : Clock; 17 import std.exception : assertThrown; 18 19 string secret = "super_secret"; 20 long now = Clock.currTime.toUnixTime(); 21 Token token = new Token(JWTAlgorithm.HS512); 22 token.claims.nbf = now + (60 * 60); 23 string encodedToken = token.encode(secret); 24 assertThrown!NotBeforeException(verify(encodedToken, secret, [ 25 JWTAlgorithm.HS512 26 ])); 27 token = new Token(JWTAlgorithm.HS512); 28 token.claims.iat = now - 3600; 29 token.claims.exp = now - 60; 30 encodedToken = token.encode(secret); 31 assertThrown!ExpiredException(verify(encodedToken, secret, [ 32 JWTAlgorithm.HS512 33 ])); 34 token = new Token(JWTAlgorithm.NONE); 35 encodedToken = token.encode(secret); 36 assertThrown!VerifyException(verify(encodedToken, secret, [ 37 JWTAlgorithm.HS512 38 ])); 39 token = new Token(JWTAlgorithm.HS512); 40 encodedToken = token.encode(secret) ~ "we_are"; 41 assertThrown!InvalidSignatureException(verify(encodedToken, secret, [ 42 JWTAlgorithm.HS512 43 ])); 44 token = new Token(JWTAlgorithm.HS512); 45 encodedToken = token.encode(secret); 46 assertThrown!InvalidAlgorithmException(verify(encodedToken, secret, 47 [JWTAlgorithm.HS256, JWTAlgorithm.HS384])); 48 } 49 50 unittest 51 { 52 testFunctionality(); 53 safelog("package works"); 54 } 55 56 @safe unittest 57 { 58 static if (__traits(compiles, testFunctionality())) 59 { 60 pragma(msg, "package is @safe"); 61 } 62 else 63 { 64 pragma(msg, "package is not @safe"); 65 } 66 } 67 68 @nogc unittest 69 { 70 static if (__traits(compiles, testFunctionality())) 71 { 72 pragma(msg, "package is @nogc"); 73 } 74 else 75 { 76 pragma(msg, "package is not @nogc"); 77 } 78 } 79 80 nothrow unittest 81 { 82 static if (__traits(compiles, testFunctionality())) 83 { 84 pragma(msg, "package is nothrow"); 85 } 86 else 87 { 88 pragma(msg, "package is not nothrow"); 89 } 90 } 91 }