@@ -105,16 +105,50 @@ pub fn server(port: u16) -> Result<impl AsyncSendSync<Result<Info, OAuthError>>,
105105 } )
106106}
107107
108+
108109fn parse_info ( data : & [ u8 ] ) -> Result < Info , OAuthError > {
109- // Assuming the data is in a format that can be directly deserialized into Info
110- // For demonstration, let's assume the data is a JSON string
111110 let data_str = std:: str:: from_utf8 ( data)
112111 . map_err ( |_| OAuthError :: ParseError ( "Invalid UTF-8" . to_string ( ) ) ) ?;
113- println ! ( "Data: {} \n " , data_str) ;
114- serde_json:: from_str :: < Info > ( data_str)
115- . map_err ( |_| OAuthError :: ParseError ( "Failed to parse Info" . to_string ( ) ) )
112+
113+ // Extract the query string from the HTTP request
114+ let query_start = data_str. find ( '?' ) . ok_or_else ( || OAuthError :: ParseError ( "No query string found" . to_string ( ) ) ) ?;
115+ let query_end = data_str. find ( '#' ) . unwrap_or_else ( || data_str. len ( ) ) ;
116+ let query_string = & data_str[ query_start + 1 ..query_end] ;
117+
118+ // Parse the query string directly
119+ let query_params: Vec < ( String , String ) > = url:: form_urlencoded:: parse ( query_string. as_bytes ( ) )
120+ . into_owned ( )
121+ . collect ( ) ;
122+
123+ // Extract the 'code', 'state', 'error', and 'error_description' parameters
124+ let code = query_params. iter ( ) . find_map ( |( k, v) | if k == "code" { Some ( v. clone ( ) ) } else { None } ) ;
125+ let state = query_params. iter ( ) . find_map ( |( k, v) | {
126+ if k == "state" {
127+ // Find the position of "HTTP/1.1\r\n" in the state value
128+ let http_start = v. find ( " HTTP/1.1\r \n " ) ;
129+ // If found, slice the string up to that position
130+ http_start. map ( |pos| v[ ..pos] . to_string ( ) )
131+ } else {
132+ None
133+ }
134+ } ) ;
135+ let error = query_params. iter ( ) . find_map ( |( k, v) | if k == "error" { Some ( v. clone ( ) ) } else { None } ) ;
136+ let error_description = query_params. iter ( ) . find_map ( |( k, v) | if k == "error_description" { Some ( v. clone ( ) ) } else { None } ) ;
137+
138+ // Construct the Info struct
139+ let info = Info {
140+ code,
141+ state,
142+ error,
143+ error_description,
144+ } ;
145+ println ! ( "{:?}" , info) ;
146+
147+ Ok ( info)
116148}
117149
150+
151+
118152pub fn token (
119153 code : & str ,
120154 client_id : & str ,
0 commit comments