@@ -43,7 +43,7 @@ public class AuthenticatorLinker
4343 public AuthenticatorLinker ( SessionData session )
4444 {
4545 this . _session = session ;
46- this . DeviceID = _generateDeviceID ( ) ;
46+ this . DeviceID = GenerateDeviceID ( ) ;
4747
4848 this . _cookies = new CookieContainer ( ) ;
4949 session . AddCookies ( _cookies ) ;
@@ -76,7 +76,17 @@ public LinkResult AddAuthenticator()
7676 if ( response == null ) return LinkResult . GeneralFailure ;
7777
7878 var addAuthenticatorResponse = JsonConvert . DeserializeObject < AddAuthenticatorResponse > ( response ) ;
79- if ( addAuthenticatorResponse == null || addAuthenticatorResponse . Response == null || addAuthenticatorResponse . Response . Status != 1 )
79+ if ( addAuthenticatorResponse == null || addAuthenticatorResponse . Response == null )
80+ {
81+ return LinkResult . GeneralFailure ;
82+ }
83+
84+ if ( addAuthenticatorResponse . Response . Status == 29 )
85+ {
86+ return LinkResult . AuthenticatorPresent ;
87+ }
88+
89+ if ( addAuthenticatorResponse . Response . Status != 1 )
8090 {
8191 return LinkResult . GeneralFailure ;
8292 }
@@ -181,7 +191,8 @@ public enum LinkResult
181191 MustProvidePhoneNumber , //No phone number on the account
182192 MustRemovePhoneNumber , //A phone number is already on the account
183193 AwaitingFinalization , //Must provide an SMS code
184- GeneralFailure //General failure (really now!)
194+ GeneralFailure , //General failure (really now!)
195+ AuthenticatorPresent
185196 }
186197
187198 public enum FinalizeResult
@@ -231,7 +242,7 @@ private class AddPhoneResponse
231242 public bool Success { get ; set ; }
232243 }
233244
234- private string _generateDeviceID ( )
245+ public static string GenerateDeviceID ( )
235246 {
236247 using ( var sha1 = new SHA1Managed ( ) )
237248 {
@@ -240,8 +251,27 @@ private string _generateDeviceID()
240251 secureRandom . GetBytes ( randomBytes ) ;
241252
242253 byte [ ] hashedBytes = sha1 . ComputeHash ( randomBytes ) ;
243- return "android:" + BitConverter . ToString ( hashedBytes ) . Replace ( "-" , "" ) ;
254+ string random32 = BitConverter . ToString ( hashedBytes ) . Replace ( "-" , "" ) . Substring ( 0 , 32 ) . ToLower ( ) ;
255+
256+ return "android:" + SplitOnRatios ( random32 , new [ ] { 8 , 4 , 4 , 4 , 12 } , "-" ) ;
244257 }
245258 }
259+
260+ private static string SplitOnRatios ( string str , int [ ] ratios , string intermediate )
261+ {
262+ string result = "" ;
263+
264+ int pos = 0 ;
265+ for ( int index = 0 ; index < ratios . Length ; index ++ )
266+ {
267+ result += str . Substring ( pos , ratios [ index ] ) ;
268+ pos = ratios [ index ] ;
269+
270+ if ( index < ratios . Length - 1 )
271+ result += intermediate ;
272+ }
273+
274+ return result ;
275+ }
246276 }
247277}
0 commit comments