@@ -2,51 +2,11 @@ use super::bakalari::Client;
2
2
use reqwest:: Response ;
3
3
use std:: borrow:: Cow ;
4
4
use std:: sync:: Arc ;
5
- use std:: time:: { Duration , Instant } ;
6
5
use thiserror:: Error ;
7
- use tokio:: sync:: mpsc;
8
- use tokio:: sync:: oneshot;
9
6
10
- /// Struct to hold token that expires after certain time
11
- #[ derive( Debug , PartialEq , Eq , Hash , Clone ) ]
12
- pub struct TempToken {
13
- token : String ,
14
- expiration : Instant ,
15
- }
16
-
17
- /// Lifetime of [`TempToken`] in seconds
18
- const TOKEN_LIFETIME : u64 = 60 * 5 ;
19
-
20
- impl TempToken {
21
- /// Create token with expiration [`TOKEN_LIFETIME`]
22
- fn new ( token : String ) -> Self {
23
- Self {
24
- token,
25
- expiration : Instant :: now ( ) + Duration :: from_secs ( TOKEN_LIFETIME ) ,
26
- }
27
- }
28
-
29
- /// Whether token is expired
30
- fn expired ( & self ) -> bool {
31
- Instant :: now ( ) > self . expiration
32
- }
33
-
34
- /// Get reference to token if it is not expired
35
- fn get ( & self ) -> Option < & str > {
36
- if self . expired ( ) {
37
- return None ;
38
- }
39
- Some ( & self . token )
40
- }
41
- }
42
-
43
- type TokenRequest = ( Arc < Client > , oneshot:: Sender < LoginResult < String > > ) ;
7
+ pub use credentials:: Credentials ;
44
8
45
- /// Struct that hold the credentials and token
46
- #[ derive( Debug ) ]
47
- pub struct Credentials {
48
- sender : mpsc:: Sender < TokenRequest > ,
49
- }
9
+ mod credentials;
50
10
51
11
/// Authentication error
52
12
#[ derive( Debug , Error ) ]
@@ -64,82 +24,6 @@ pub enum LoginError {
64
24
65
25
pub type LoginResult < T > = Result < T , LoginError > ;
66
26
67
- impl Credentials {
68
- /// Create new credentials from username and password
69
- ///
70
- /// # Errors
71
- /// If login fails
72
- pub async fn new ( ( username, password) : ( String , String ) , client : & Client ) -> LoginResult < Self > {
73
- let token = TempToken :: new ( Self :: login ( ( & username, & password) , client) . await ?) ;
74
-
75
- let ( sender, mut receiver) = mpsc:: channel :: < TokenRequest > ( 10 ) ;
76
-
77
- tokio:: spawn ( async move {
78
- let mut store = token;
79
-
80
- while let Some ( ( client, sender) ) = receiver. recv ( ) . await {
81
- let token = if let Some ( token) = store. get ( ) {
82
- Ok ( token. to_owned ( ) )
83
- } else {
84
- let token = Self :: login ( ( & username, & password) , & client) . await ;
85
- token. map ( |token| {
86
- store = TempToken :: new ( token) ;
87
- store. token . clone ( )
88
- } )
89
- } ;
90
- sender. send ( token) . unwrap ( ) ;
91
- }
92
- } ) ;
93
-
94
- Ok ( Self { sender } )
95
- }
96
-
97
- /// Get token, and renew in case it expired
98
- ///
99
- /// # Errors
100
- /// If renew fails
101
- ///
102
- /// # Panics
103
- /// Panics if token expires somehow (shouldn't)
104
- pub async fn get_token ( & self , client : Arc < Client > ) -> LoginResult < String > {
105
- let ( tx, rx) = oneshot:: channel ( ) ;
106
- self . sender
107
- . send ( ( client, tx) )
108
- . await
109
- . expect ( "failed to send token request" ) ;
110
- rx. await . unwrap ( )
111
- }
112
-
113
- // Issue new token from api
114
- pub async fn login ( ( username, password) : ( & str , & str ) , client : & Client ) -> LoginResult < String > {
115
- let res = client
116
- . reqwest_client ( )
117
- . post ( client. url ( ) . join ( "Login" ) . unwrap ( ) )
118
- . body ( format ! ( "username={username}&password={password}" ) )
119
- . header ( "Content-Type" , "application/x-www-form-urlencoded" )
120
- . send ( )
121
- . await ?;
122
-
123
- if res. status ( ) . as_u16 ( ) != 302 {
124
- return Err ( LoginError :: Login ( res) ) ;
125
- }
126
-
127
- let v = res
128
- . headers ( )
129
- . get_all ( "Set-Cookie" )
130
- . iter ( )
131
- . filter_map ( |h| h. to_str ( ) . ok ( ) )
132
- . filter_map ( |h| h. split_once ( ';' ) )
133
- . map ( |h| h. 0 )
134
- . filter_map ( |h| h. split_once ( '=' ) )
135
- . find ( |h| h. 0 == "BakaAuth" )
136
- . map ( |h| h. 1 )
137
- . ok_or ( LoginError :: CookieParse ) ?;
138
-
139
- Ok ( v. to_owned ( ) )
140
- }
141
- }
142
-
143
27
/// Authentication types
144
28
#[ derive( Debug ) ]
145
29
pub enum Auth {
0 commit comments