Separate signature computation

This commit is contained in:
Tobe O 2019-08-16 09:19:48 -04:00
parent 6d39f590d2
commit aef47b12b9

View file

@ -42,18 +42,30 @@ class CookieSigner {
} }
} }
/// **Overwrites** the value of a [cookie] with one that is signed /// Returns a new cookie, replacing the value of an input
/// with the [hmac]. /// [cookie] with one that is signed with the [hmac].
/// ///
/// The signature is: /// The signature is:
/// `base64Url(cookie.value) + "." + base64Url(sig)` /// `base64Url(cookie.value) + "." + base64Url(sig)`
/// ///
/// Where `sig` is the cookie's value, signed with the [hmac]. /// Where `sig` is the cookie's value, signed with the [hmac].
void signCookie(Cookie cookie) { Cookie signCookie(Cookie cookie) {
return Cookie(cookie.name, computeCookieSignature(cookie.value))
..domain = cookie.domain
..expires = cookie.expires
..httpOnly = cookie.httpOnly
..maxAge = cookie.maxAge
..path = cookie.path
..secure = cookie.secure;
}
/// Computes the signature of a [cookieValue], either for
/// signing an outgoing cookie, or verifying an incoming cookie.
String computeCookieSignature(String cookieValue) {
// base64Url(cookie) + "." + base64Url(sig) // base64Url(cookie) + "." + base64Url(sig)
var encodedCookie = base64Url.encode(cookie.value.codeUnits); var encodedCookie = base64Url.encode(cookieValue.codeUnits);
var sigBytes = hmac.convert(cookie.value.codeUnits).bytes; var sigBytes = hmac.convert(cookieValue.codeUnits).bytes;
var sig = base64Url.encode(sigBytes); var sig = base64Url.encode(sigBytes);
cookie.value = encodedCookie + '.' + sig; return encodedCookie + '.' + sig;
} }
} }