@@ -6,6 +6,7 @@ use std::{
66
77use byteorder:: { BigEndian , ByteOrder } ;
88use bytes:: Bytes ;
9+ use data_encoding:: HEXUPPER_PERMISSIVE ;
910use futures_util:: future:: IntoStream ;
1011use http:: header:: HeaderValue ;
1112use hyper:: {
@@ -189,9 +190,10 @@ impl SpClient {
189190 // on macOS and Windows. On Android and iOS we can send a platform-specific client ID and are
190191 // then presented with a hash cash challenge. On Linux, we have to pass the old keymaster ID.
191192 // We delegate most of this logic to `SessionConfig`.
192- let client_id = match OS {
193+ let os = OS ;
194+ let client_id = match os {
193195 "macos" | "windows" => self . session ( ) . client_id ( ) ,
194- _ => SessionConfig :: default ( ) . client_id ,
196+ os => SessionConfig :: default_for_os ( os ) . client_id ,
195197 } ;
196198 client_data. client_id = client_id;
197199
@@ -206,7 +208,7 @@ impl SpClient {
206208 let os_version = sys. os_version ( ) . unwrap_or_else ( || String :: from ( "0" ) ) ;
207209 let kernel_version = sys. kernel_version ( ) . unwrap_or_else ( || String :: from ( "0" ) ) ;
208210
209- match OS {
211+ match os {
210212 "windows" => {
211213 let os_version = os_version. parse :: < f32 > ( ) . unwrap_or ( 10. ) as i32 ;
212214 let kernel_version = kernel_version. parse :: < i32 > ( ) . unwrap_or ( 21370 ) ;
@@ -269,7 +271,10 @@ impl SpClient {
269271 match ClientTokenResponseType :: from_i32 ( message. response_type . value ( ) ) {
270272 // depending on the platform, you're either given a token immediately
271273 // or are presented a hash cash challenge to solve first
272- Some ( ClientTokenResponseType :: RESPONSE_GRANTED_TOKEN_RESPONSE ) => break message,
274+ Some ( ClientTokenResponseType :: RESPONSE_GRANTED_TOKEN_RESPONSE ) => {
275+ debug ! ( "Received a granted token" ) ;
276+ break message;
277+ }
273278 Some ( ClientTokenResponseType :: RESPONSE_CHALLENGES_RESPONSE ) => {
274279 debug ! ( "Received a hash cash challenge, solving..." ) ;
275280
@@ -279,20 +284,22 @@ impl SpClient {
279284 let hash_cash_challenge = challenge. evaluate_hashcash_parameters ( ) ;
280285
281286 let ctx = vec ! [ ] ;
282- let prefix = hex:: decode ( & hash_cash_challenge. prefix ) . map_err ( |e| {
283- Error :: failed_precondition ( format ! (
284- "Unable to decode hash cash challenge: {e}"
285- ) )
286- } ) ?;
287+ let prefix = HEXUPPER_PERMISSIVE
288+ . decode ( hash_cash_challenge. prefix . as_bytes ( ) )
289+ . map_err ( |e| {
290+ Error :: failed_precondition ( format ! (
291+ "Unable to decode hash cash challenge: {e}"
292+ ) )
293+ } ) ?;
287294 let length = hash_cash_challenge. length ;
288295
289- let mut suffix = vec ! [ 0 ; 0x10 ] ;
296+ let mut suffix = [ 0u8 ; 0x10 ] ;
290297 let answer = Self :: solve_hash_cash ( & ctx, & prefix, length, & mut suffix) ;
291298
292299 match answer {
293300 Ok ( _) => {
294301 // the suffix must be in uppercase
295- let suffix = hex :: encode ( suffix) . to_uppercase ( ) ;
302+ let suffix = HEXUPPER_PERMISSIVE . encode ( & suffix) ;
296303
297304 let mut answer_message = ClientTokenRequest :: new ( ) ;
298305 answer_message. request_type =
@@ -302,7 +309,7 @@ impl SpClient {
302309 let challenge_answers = answer_message. mut_challenge_answers ( ) ;
303310
304311 let mut challenge_answer = ChallengeAnswer :: new ( ) ;
305- challenge_answer. mut_hash_cash ( ) . suffix = suffix. to_string ( ) ;
312+ challenge_answer. mut_hash_cash ( ) . suffix = suffix;
306313 challenge_answer. ChallengeType =
307314 ChallengeType :: CHALLENGE_HASH_CASH . into ( ) ;
308315
@@ -477,11 +484,14 @@ impl SpClient {
477484 HeaderValue :: from_str ( & format ! ( "{} {}" , token. token_type, token. access_token, ) ) ?,
478485 ) ;
479486
480- if let Ok ( client_token) = self . client_token ( ) . await {
481- headers_mut. insert ( CLIENT_TOKEN , HeaderValue :: from_str ( & client_token) ?) ;
482- } else {
483- // currently these endpoints seem to work fine without it
484- warn ! ( "Unable to get client token. Trying to continue without..." ) ;
487+ match self . client_token ( ) . await {
488+ Ok ( client_token) => {
489+ let _ = headers_mut. insert ( CLIENT_TOKEN , HeaderValue :: from_str ( & client_token) ?) ;
490+ }
491+ Err ( e) => {
492+ // currently these endpoints seem to work fine without it
493+ warn ! ( "Unable to get client token: {e} Trying to continue without..." )
494+ }
485495 }
486496
487497 last_response = self . session ( ) . http_client ( ) . request_body ( request) . await ;
0 commit comments