1 module hunt.jwt.Claims; 2 3 import hunt.jwt.Base64Codec; 4 import hunt.jwt.Component; 5 import hunt.jwt.Exceptions; 6 import hunt.jwt.JwtAlgorithm; 7 8 import std.conv; 9 import std.datetime; 10 import std.json; 11 import std.string; 12 13 /** 14 * represents the claims component of a JWT 15 */ 16 class Claims : Component { 17 private JSONValue data; 18 19 private this(in JSONValue claims) { 20 this.data = claims; 21 22 } 23 24 this() { 25 this.data = JSONValue(["iat": JSONValue(Clock.currTime.toUnixTime())]); 26 } 27 28 void set(T)(string name, T data) { 29 static if(is(T == JSONValue)) { 30 this.data.object[name] = data; 31 } else { 32 this.data.object[name] = JSONValue(data); 33 } 34 } 35 36 /** 37 * Params: 38 * name = the name of the claim 39 * Returns: returns a string representation of the claim if it exists and is a string or an empty string if doesn't exist or is not a string 40 */ 41 string get(string name) { 42 try { 43 return this.data[name].str(); 44 45 } catch (JSONException e) { 46 return string.init; 47 48 } 49 50 } 51 52 /** 53 * Params: 54 * name = the name of the claim 55 * Returns: an array of JSONValue 56 */ 57 JSONValue[] getArray(string name) { 58 try { 59 return this.data[name].array(); 60 61 } catch (JSONException e) { 62 return JSONValue.Store.array.init; 63 64 } 65 66 } 67 68 69 /** 70 * Params: 71 * name = the name of the claim 72 * Returns: a JSONValue 73 */ 74 JSONValue[string] getObject(string name) { 75 try { 76 return this.data[name].object(); 77 78 } catch (JSONException e) { 79 return JSONValue.Store.object.init; 80 81 } 82 83 } 84 85 /** 86 * Params: 87 * name = the name of the claim 88 * Returns: returns a long representation of the claim if it exists and is an 89 * integer or the initial value for long if doesn't exist or is not an integer 90 */ 91 long getInt(string name) { 92 try { 93 return this.data[name].integer(); 94 95 } catch (JSONException e) { 96 return long.init; 97 98 } 99 100 } 101 102 /** 103 * Params: 104 * name = the name of the claim 105 * Returns: returns a double representation of the claim if it exists and is a 106 * double or the initial value for double if doesn't exist or is not a double 107 */ 108 double getDouble(string name) { 109 try { 110 return this.data[name].floating(); 111 112 } catch (JSONException e) { 113 return double.init; 114 115 } 116 117 } 118 119 /** 120 * Params: 121 * name = the name of the claim 122 * Returns: returns a boolean representation of the claim if it exists and is a 123 * boolean or the initial value for bool if doesn't exist or is not a boolean 124 */ 125 bool getBool(string name) { 126 try { 127 return this.data[name].type == JSONType.true_; 128 129 } catch (JSONException e) { 130 return bool.init; 131 132 } 133 134 } 135 136 /** 137 * Params: 138 * name = the name of the claim 139 * Returns: returns a boolean value if the claim exists and is null or 140 * the initial value for bool it it doesn't exist or is not null 141 */ 142 bool isNull(string name) { 143 try { 144 return this.data[name].isNull(); 145 146 } catch (JSONException) { 147 return bool.init; 148 149 } 150 151 } 152 153 @property void iss(string s) { 154 this.data.object["iss"] = s; 155 } 156 157 158 @property string iss() { 159 try { 160 return this.data["iss"].str(); 161 162 } catch (JSONException e) { 163 return ""; 164 165 } 166 167 } 168 169 @property void sub(string s) { 170 this.data.object["sub"] = s; 171 } 172 173 @property string sub() { 174 try { 175 return this.data["sub"].str(); 176 177 } catch (JSONException e) { 178 return ""; 179 180 } 181 182 } 183 184 @property void aud(string s) { 185 this.data.object["aud"] = s; 186 } 187 188 @property string aud() { 189 try { 190 return this.data["aud"].str(); 191 192 } catch (JSONException e) { 193 return ""; 194 195 } 196 197 } 198 199 @property void exp(long n) { 200 this.data.object["exp"] = n; 201 } 202 203 @property long exp() { 204 try { 205 return this.data["exp"].integer; 206 207 } catch (JSONException) { 208 return 0; 209 210 } 211 212 } 213 214 @property void nbf(long n) { 215 this.data.object["nbf"] = n; 216 } 217 218 @property long nbf() { 219 try { 220 return this.data["nbf"].integer; 221 222 } catch (JSONException) { 223 return 0; 224 225 } 226 227 } 228 229 @property void iat(long n) { 230 this.data.object["iat"] = n; 231 } 232 233 @property long iat() { 234 try { 235 return this.data["iat"].integer; 236 237 } catch (JSONException) { 238 return 0; 239 240 } 241 242 } 243 244 @property void jit(string s) { 245 this.data.object["jit"] = s; 246 } 247 248 @property string jit() { 249 try { 250 return this.data["jit"].str(); 251 252 } catch(JSONException e) { 253 return ""; 254 255 } 256 257 } 258 259 /** 260 * gives json encoded claims 261 * Returns: json encoded claims 262 */ 263 @property override string json() { 264 return this.data.toString(); 265 266 } 267 }