1 module rfc7797; 2 3 public import itsdangerous; 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.stdio; 17 18 // Signer 19 string secretkey = "hello"; 20 21 auto signer = new Signer!(SHA1, SHA1)(secretkey); 22 string signature = signer.sign("this is a test"); 23 assert(signature == "this is a test.hgGT0Zoara4L13FX3_xm-xmfa_0"); 24 25 assert(signer.verifySignature("this is a test", "hgGT0Zoara4L13FX3_xm-xmfa_0") == true); 26 assert(signer.unsign("this is a test.hgGT0Zoara4L13FX3_xm-xmfa_0") == "this is a test"); 27 28 // TimedJSONWebSignatureSerializer 29 int expiresIn = 3600; 30 31 JSONValue obj; 32 obj["a"] = 13; 33 obj["b"] = "test me"; 34 35 // below configuration is the default of Python version: s = TimedJSONWebSignatureSerializer(secretkey, expiresIn) 36 // output token can be tested on https://jwt.io/ 37 38 auto tjwss = new TimedJSONWebSignatureSerializer!(SHA512, Signer!(SHA1, SHA512))(secretkey); 39 40 auto ttoken = tjwss.dumps(obj); 41 ttoken.writeln; 42 43 try 44 { 45 tjwss.loadsWithHeader("eyJhbGciOiJIUzUxMiIsImV4cCI6MTU3ODQzNzg0MiwiaWF0IjoxNTc4NDM0MjQyfQ.eyJhIjoxMywiYiI6InRlc3QgbWUifQ.TMHBXKna35Ah5vtxNmkXUOx96xgW7iZntsHqHKOH5dTZdgt3zdjZjR5urDzdHVOlaD5Hj_CSlH_xFf1cZRF4eA") 46 .writeln; 47 } 48 catch (SignatureExpired exp) 49 { 50 writeln("signature expired!: " ~ exp.msg ~ " | For paylodad: " ~ exp.payload); 51 } 52 catch (BadSignature exp) 53 { 54 writeln("Bad signature!: " ~ exp.msg); 55 } 56 57 // URLSafeSerializer 58 JSONValue obj2; 59 obj2["id"] = 5; 60 obj2["name"] = "itsdangerous"; 61 62 auto uss = new URLSafeSerializer("secret key", "auth"); 63 string s = uss.dumps(obj2); 64 assert(s == "eyJpZCI6NSwibmFtZSI6Iml0c2Rhbmdlcm91cyJ9.6YP6T0BaO67XP--9UzTrmurXSmg"); 65 66 JSONValue oobj = uss.loads(s); 67 oobj.writeln; 68 69 // URLSafeTimedSerializer 70 auto usts = new URLSafeTimedSerializer(secretkey); 71 string st = usts.dumps(obj); 72 st.writeln; 73 } 74 75 unittest 76 { 77 testFunctionality(); 78 safelog("package works"); 79 } 80 81 @safe unittest 82 { 83 static if (__traits(compiles, testFunctionality())) 84 { 85 pragma(msg, "package is @safe"); 86 } 87 else 88 { 89 pragma(msg, "package is not @safe"); 90 } 91 } 92 93 @nogc unittest 94 { 95 static if (__traits(compiles, testFunctionality())) 96 { 97 pragma(msg, "package is @nogc"); 98 } 99 else 100 { 101 pragma(msg, "package is not @nogc"); 102 } 103 } 104 105 nothrow unittest 106 { 107 static if (__traits(compiles, testFunctionality())) 108 { 109 pragma(msg, "package is nothrow"); 110 } 111 else 112 { 113 pragma(msg, "package is not nothrow"); 114 } 115 } 116 }