From e7b590dc80e3ba9cd5e5115b72702aa845727bde Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 18 Oct 2017 22:52:00 +0200 Subject: [PATCH] Implement method 'createCookieName' in class 'Auth' --- src/Auth.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Auth.php b/src/Auth.php index 5126ad5..0139b09 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -1773,4 +1773,34 @@ final class Auth extends UserManager { return \vsprintf('%s%s-%s-%s-%s-%s%s%s', \str_split(\bin2hex($data), 4)); } + /** + * Generates a unique cookie name for the given descriptor based on the supplied seed + * + * @param string $descriptor a short label describing the purpose of the cookie, e.g. 'session' + * @param string|null $seed (optional) the data to deterministically generate the name from + * @return string + */ + public static function createCookieName($descriptor, $seed = null) { + // use the supplied seed or the current UNIX time in seconds + $seed = ($seed !== null) ? $seed : \time(); + + foreach (self::COOKIE_PREFIXES as $cookiePrefix) { + // if the seed contains a certain cookie prefix + if (\strpos($seed, $cookiePrefix) === 0) { + // prepend the same prefix to the descriptor + $descriptor = $cookiePrefix . $descriptor; + } + } + + // generate a unique token based on the name(space) of this library and on the seed + $token = Base64::encodeUrlSafeWithoutPadding( + \md5( + __NAMESPACE__ . "\n" . $seed, + true + ) + ); + + return $descriptor . '_' . $token; + } + }