@@ -3,6 +3,7 @@ import CommandPolykey from '../CommandPolykey';
33import  *  as  binUtils  from  '../utils' ; 
44import  *  as  binOptions  from  '../utils/options' ; 
55import  *  as  binProcessors  from  '../utils/processors' ; 
6+ import  *  as  identitiesUtils  from  '../../identities/utils' ; 
67
78class  CommandAuthenticate  extends  CommandPolykey  { 
89  constructor ( ...args : ConstructorParameters < typeof  CommandPolykey > )  { 
@@ -32,7 +33,11 @@ class CommandAuthenticate extends CommandPolykey {
3233        this . fs , 
3334      ) ; 
3435      let  pkClient : PolykeyClient ; 
36+       let  genReadable : ReturnType < 
37+         typeof  pkClient . grpcClient . identitiesAuthenticate 
38+       > ; 
3539      this . exitHandlers . handlers . push ( async  ( )  =>  { 
40+         if  ( genReadable  !=  null )  genReadable . stream . cancel ( ) ; 
3641        if  ( pkClient  !=  null )  await  pkClient . stop ( ) ; 
3742      } ) ; 
3843      try  { 
@@ -45,32 +50,50 @@ class CommandAuthenticate extends CommandPolykey {
4550        } ) ; 
4651        const  providerMessage  =  new  identitiesPB . Provider ( ) ; 
4752        providerMessage . setProviderId ( providerId ) ; 
48-         providerMessage . setMessage ( identityId ) ; 
49-         const  successMessage  =  await  binUtils . retryAuthentication ( 
50-           async  ( auth )  =>  { 
51-             const  stream  =  pkClient . grpcClient . identitiesAuthenticate ( 
52-               providerMessage , 
53-               auth , 
54-             ) ; 
55-             const  codeMessage  =  ( await  stream . next ( ) ) . value ; 
56-             process . stdout . write ( 
57-               binUtils . outputFormatter ( { 
58-                 type : options . format  ===  'json'  ? 'json'  : 'list' , 
59-                 data : [ `Your device code is: ${ codeMessage ! . getMessage ( ) }  ] , 
60-               } ) , 
61-             ) ; 
62-             return  ( await  stream . next ( ) ) . value ; 
63-           } , 
64-           meta , 
65-         ) ; 
66-         process . stdout . write ( 
67-           binUtils . outputFormatter ( { 
68-             type : options . format  ===  'json'  ? 'json'  : 'list' , 
69-             data : [ 
70-               `Successfully authenticated user: ${ successMessage ! . getMessage ( ) }  , 
71-             ] , 
72-           } ) , 
73-         ) ; 
53+         providerMessage . setIdentityId ( identityId ) ; 
54+         await  binUtils . retryAuthentication ( async  ( auth )  =>  { 
55+           genReadable  =  pkClient . grpcClient . identitiesAuthenticate ( 
56+             providerMessage , 
57+             auth , 
58+           ) ; 
59+           for  await  ( const  message  of  genReadable )  { 
60+             switch  ( message . getStepCase ( ) )  { 
61+               case  identitiesPB . AuthenticationProcess . StepCase . REQUEST : { 
62+                 const  authRequest  =  message . getRequest ( ) ! ; 
63+                 this . logger . info ( 
64+                   `Navigate to the URL in order to authenticate` , 
65+                 ) ; 
66+                 this . logger . info ( 
67+                   'Use any additional additional properties to complete authentication' , 
68+                 ) ; 
69+                 identitiesUtils . browser ( authRequest . getUrl ( ) ) ; 
70+                 process . stdout . write ( 
71+                   binUtils . outputFormatter ( { 
72+                     type : options . format  ===  'json'  ? 'json'  : 'dict' , 
73+                     data : { 
74+                       url : authRequest . getUrl ( ) , 
75+                       ...Object . fromEntries ( authRequest . getDataMap ( ) . entries ( ) ) , 
76+                     } , 
77+                   } ) , 
78+                 ) ; 
79+                 break ; 
80+               } 
81+               case  identitiesPB . AuthenticationProcess . StepCase . RESPONSE : { 
82+                 const  authResponse  =  message . getResponse ( ) ! ; 
83+                 this . logger . info ( 
84+                   `Authenticated digital identity provider ${ providerId } ${ identityId }  , 
85+                 ) ; 
86+                 process . stdout . write ( 
87+                   binUtils . outputFormatter ( { 
88+                     type : options . format  ===  'json'  ? 'json'  : 'list' , 
89+                     data : [ authResponse . getIdentityId ( ) ] , 
90+                   } ) , 
91+                 ) ; 
92+                 break ; 
93+               } 
94+             } 
95+           } 
96+         } ,  meta ) ; 
7497      }  finally  { 
7598        if  ( pkClient !  !=  null )  await  pkClient . stop ( ) ; 
7699      } 
0 commit comments