1 /** 2 * Copyright © DiamondMVC 2019 3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module diamond.security.tokens.sessiontoken; 7 8 import diamond.core.apptype; 9 10 static if (isWeb) 11 { 12 import diamond.security.tokens.tokengenerator; 13 14 /// Wrapper for a session token generator. 15 final class SessionToken : TokenGenerator 16 { 17 private: 18 /// Creates a new instance of the session token generator. 19 this() 20 { 21 super(); 22 } 23 24 public: 25 /** 26 * Generates a token. 27 * Returns: 28 * Returns the generated token. 29 */ 30 override string generate() 31 { 32 import diamond.security.tokens.generictoken; 33 34 return genericToken.generate(); 35 } 36 37 /** 38 * Generates a token based on an input. 39 * Params: 40 * input = An input to append to the token. 41 * Returns: 42 * Returns the generated token. 43 */ 44 override string generate(string input) 45 { 46 import diamond.core.senc; 47 48 return generate() ~ SENC.encode(input); 49 } 50 51 /** 52 * Generates a token and passes it to the parent generator. 53 * Params: 54 * parentGenerator = The parent generator to use with the generated token. 55 * Returns: 56 * Returns the generated token. 57 */ 58 override string generate(TokenGenerator parentGenerator) 59 { 60 import diamond.errors.checks; 61 enforce(parentGenerator, "Passed no parent generator."); 62 63 return parentGenerator.generate(this.generate()); 64 } 65 } 66 67 /// The session token generator instance for the current thread. 68 private SessionToken _sessionToken; 69 70 @property 71 { 72 /// Gets the session token. 73 SessionToken sessionToken() 74 { 75 if (!_sessionToken) 76 { 77 _sessionToken = new SessionToken; 78 } 79 80 return _sessionToken; 81 } 82 } 83 }