@@ -3,14 +3,24 @@ use axum::{
33 http:: { StatusCode , header} ,
44 response:: { IntoResponse , Response } ,
55} ;
6- use serde:: Serialize ;
6+ use serde:: { Deserialize , Serialize } ;
77
88#[ derive( Serialize ) ]
99pub struct LoginResponse {
1010 pub access_token : String ,
1111 pub refresh_token : String ,
1212 pub token_type : String ,
13- pub expires_in : u64 ,
13+ }
14+
15+ impl LoginResponse {
16+ pub fn new ( access_token : String , refresh_token : String ) -> Self {
17+ let token_type = String :: from ( "Bearer" ) ;
18+ Self {
19+ access_token,
20+ refresh_token,
21+ token_type,
22+ }
23+ }
1424}
1525
1626impl IntoResponse for LoginResponse {
@@ -20,16 +30,55 @@ impl IntoResponse for LoginResponse {
2030 [
2131 ( header:: CACHE_CONTROL , "no-store" ) ,
2232 ( header:: PRAGMA , "no-cache" ) ,
33+ (
34+ header:: SET_COOKIE ,
35+ & refresh_token_cookie ( & self . refresh_token ) ,
36+ ) ,
2337 ] ,
2438 Json ( self ) ,
2539 )
2640 . into_response ( )
2741 }
2842}
2943
44+ #[ derive( Deserialize ) ]
45+ pub struct RefreshRequest {
46+ pub access_token : String ,
47+ }
48+
3049#[ derive( Serialize ) ]
3150pub struct RefreshResponse {
3251 pub access_token : String ,
3352 pub token_type : String ,
34- pub expires_in : String ,
53+ }
54+
55+ impl RefreshResponse {
56+ pub fn new ( access_token : String ) -> Self {
57+ let token_type = String :: from ( "Bearer" ) ;
58+ Self {
59+ access_token,
60+ token_type,
61+ }
62+ }
63+ }
64+
65+ impl IntoResponse for RefreshResponse {
66+ fn into_response ( self ) -> Response {
67+ (
68+ StatusCode :: OK ,
69+ [
70+ ( header:: CACHE_CONTROL , "no-store" ) ,
71+ ( header:: PRAGMA , "no-cache" ) ,
72+ ] ,
73+ Json ( self ) ,
74+ )
75+ . into_response ( )
76+ }
77+ }
78+
79+ fn refresh_token_cookie ( refresh_token : & str ) -> String {
80+ format ! (
81+ "refresh_token={}; HttpOnly; Secure; SameSite=Strict" ,
82+ refresh_token
83+ )
3584}
0 commit comments