diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-05-07 12:34:50 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-05-07 12:34:50 -0500 |
commit | 1dbe3776c682f469d1497247fac22f0aa233a598 (patch) | |
tree | 6edbeae2f431b76066d9679525da15415042cbc2 /src/auth/claims.rs | |
parent | aac78e11339db945f3bb81ad40bc71d021bcd754 (diff) |
Diffstat (limited to 'src/auth/claims.rs')
-rw-r--r-- | src/auth/claims.rs | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/src/auth/claims.rs b/src/auth/claims.rs index 67c4fbb..652a47f 100644 --- a/src/auth/claims.rs +++ b/src/auth/claims.rs @@ -106,18 +106,10 @@ const ACCESS: i64 = 86400; pub type AccessClaims = Claims<ACCESS>; -impl From<RefreshClaims> for AccessClaims { - fn from(value: RefreshClaims) -> Self { - Claims::issue(value.sub) - } -} - -impl TryFrom<AccessClaims> for Cookie<'_> { - type Error = Error; - - fn try_from(value: AccessClaims) -> Result<Self, Self::Error> { - Ok(Cookie::build(("token", JWT.encode(&value)?)) - .expires(value.exp) +impl AccessClaims { + pub fn as_cookie(&self) -> Result<Cookie, Error> { + Ok(Cookie::build(("token", JWT.encode(&self)?)) + .expires(self.exp) .secure(true) .http_only(true) .path("/api") @@ -125,14 +117,9 @@ impl TryFrom<AccessClaims> for Cookie<'_> { } } -impl TryFrom<AccessClaims> for HeaderValue { - type Error = Error; - - fn try_from(value: AccessClaims) -> Result<Self, Self::Error> { - Cookie::try_from(value)? - .to_string() - .parse() - .map_err(Into::into) +impl From<RefreshClaims> for AccessClaims { + fn from(value: RefreshClaims) -> Self { + Claims::issue(value.sub) } } @@ -150,7 +137,7 @@ impl IntoResponseParts for AccessClaims { mut res: axum::response::ResponseParts, ) -> Result<axum::response::ResponseParts, Self::Error> { res.headers_mut() - .append(SET_COOKIE, HeaderValue::try_from(self)?); + .try_append(SET_COOKIE, self.as_cookie()?.to_string().parse()?)?; Ok(res) } |