11mod scopes;
22
33use bon:: Builder ;
4- use chrono:: { DateTime , Utc } ;
4+ use chrono:: NaiveDateTime ;
55use diesel:: dsl:: now;
66use diesel:: prelude:: * ;
7- use diesel:: sql_types:: Timestamptz ;
87use diesel_async:: scoped_futures:: ScopedFutureExt ;
98use diesel_async:: { AsyncConnection , AsyncPgConnection , RunQueryDsl } ;
109
1110pub use self :: scopes:: { CrateScope , EndpointScope } ;
1211use crate :: models:: User ;
1312use crate :: schema:: api_tokens;
13+ use crate :: utils:: rfc3339;
1414use crate :: utils:: token:: { HashedToken , PlainToken } ;
1515
1616#[ derive( Debug , Insertable , Builder ) ]
@@ -25,7 +25,7 @@ pub struct NewApiToken {
2525 pub crate_scopes : Option < Vec < CrateScope > > ,
2626 /// A list of endpoint scopes or `None` for the `legacy` endpoint scope (see RFC #2947)
2727 pub endpoint_scopes : Option < Vec < EndpointScope > > ,
28- pub expired_at : Option < DateTime < Utc > > ,
28+ pub expired_at : Option < NaiveDateTime > ,
2929}
3030
3131impl NewApiToken {
@@ -46,15 +46,18 @@ pub struct ApiToken {
4646 #[ serde( skip) ]
4747 pub user_id : i32 ,
4848 pub name : String ,
49- pub created_at : DateTime < Utc > ,
50- pub last_used_at : Option < DateTime < Utc > > ,
49+ #[ serde( with = "rfc3339" ) ]
50+ pub created_at : NaiveDateTime ,
51+ #[ serde( with = "rfc3339::option" ) ]
52+ pub last_used_at : Option < NaiveDateTime > ,
5153 #[ serde( skip) ]
5254 pub revoked : bool ,
5355 /// `None` or a list of crate scope patterns (see RFC #2947)
5456 pub crate_scopes : Option < Vec < CrateScope > > ,
5557 /// A list of endpoint scopes or `None` for the `legacy` endpoint scope (see RFC #2947)
5658 pub endpoint_scopes : Option < Vec < EndpointScope > > ,
57- pub expired_at : Option < DateTime < Utc > > ,
59+ #[ serde( with = "rfc3339::option" ) ]
60+ pub expired_at : Option < NaiveDateTime > ,
5861}
5962
6063impl ApiToken {
@@ -77,7 +80,7 @@ impl ApiToken {
7780 . transaction ( |conn| {
7881 async move {
7982 diesel:: update ( tokens)
80- . set ( api_tokens:: last_used_at. eq ( now. into_sql :: < Timestamptz > ( ) . nullable ( ) ) )
83+ . set ( api_tokens:: last_used_at. eq ( now. nullable ( ) ) )
8184 . returning ( ApiToken :: as_returning ( ) )
8285 . get_result ( conn)
8386 . await
@@ -108,23 +111,20 @@ mod tests {
108111 created_at : NaiveDate :: from_ymd_opt ( 2017 , 1 , 6 )
109112 . unwrap ( )
110113 . and_hms_opt ( 14 , 23 , 11 )
114+ . unwrap ( ) ,
115+ last_used_at : NaiveDate :: from_ymd_opt ( 2017 , 1 , 6 )
111116 . unwrap ( )
112- . and_utc ( ) ,
113- last_used_at : Some (
114- NaiveDate :: from_ymd_opt ( 2017 , 1 , 6 )
115- . unwrap ( )
116- . and_hms_opt ( 14 , 23 , 12 )
117- . unwrap ( )
118- . and_utc ( ) ,
119- ) ,
117+ . and_hms_opt ( 14 , 23 , 12 ) ,
120118 crate_scopes : None ,
121119 endpoint_scopes : None ,
122120 expired_at : None ,
123121 } ;
124122 let json = serde_json:: to_string ( & tok) . unwrap ( ) ;
125- assert_some ! ( json. as_str( ) . find( r#""created_at":"2017-01-06T14:23:11Z""# ) ) ;
126123 assert_some ! ( json
127124 . as_str( )
128- . find( r#""last_used_at":"2017-01-06T14:23:12Z""# ) ) ;
125+ . find( r#""created_at":"2017-01-06T14:23:11+00:00""# ) ) ;
126+ assert_some ! ( json
127+ . as_str( )
128+ . find( r#""last_used_at":"2017-01-06T14:23:12+00:00""# ) ) ;
129129 }
130130}
0 commit comments