@@ -7,20 +7,25 @@ import type {
77import type { IdentityRequestData } from 'polykey/client/types.js' ;
88import CommandPolykey from '../CommandPolykey.js' ;
99import * as binProcessors from '../utils/processors.js' ;
10+ import * as binParsers from '../utils/parsers.js' ;
1011import * as binUtils from '../utils/index.js' ;
1112import * as binOptions from '../utils/options.js' ;
12- import * as binErrors from '../errors.js' ;
13+ import * as errors from '../errors.js' ;
1314
1415class CommandLogin extends CommandPolykey {
1516 constructor ( ...args : ConstructorParameters < typeof CommandPolykey > ) {
1617 super ( ...args ) ;
1718 this . name ( 'login' ) ;
1819 this . description ( 'Login to a platform with Polykey identity' ) ;
19- this . argument ( '<token>' , 'Token provided by platform for logging in' ) ;
20+ this . argument (
21+ '<token>' ,
22+ 'Token provided by platform for logging in' ,
23+ binParsers . parseCompactJWT ,
24+ ) ;
2025 this . addOption ( binOptions . nodeId ) ;
2126 this . addOption ( binOptions . clientHost ) ;
2227 this . addOption ( binOptions . clientPort ) ;
23- this . action ( async ( token , options ) => {
28+ this . action ( async ( encodedToken , options ) => {
2429 const { default : PolykeyClient } = await import (
2530 'polykey/PolykeyClient.js'
2631 ) ;
@@ -52,10 +57,9 @@ class CommandLogin extends CommandPolykey {
5257 } ,
5358 logger : this . logger . getChild ( PolykeyClient . name ) ,
5459 } ) ;
55- // Compact JWTs are in xxxx.yyyy.zzzz format where x is the protected
56- // header, y is the payload, and z is the binary signature.
57- const [ protectedHeader , payload , signature ] : [ string , string , string ] =
58- token . split ( '.' ) ;
60+
61+ // Create a JSON representation of the encoded header
62+ const [ protectedHeader , payload , signature ] = encodedToken ;
5963 const incomingTokenEncoded = {
6064 payload : payload as TokenPayloadEncoded ,
6165 signatures : [
@@ -65,6 +69,8 @@ class CommandLogin extends CommandPolykey {
6569 } ,
6670 ] ,
6771 } ;
72+
73+ // Get it verified and signed by the agent
6874 const response = await binUtils . retryAuthentication (
6975 ( auth ) =>
7076 pkClient . rpcClient . methods . authSignToken ( {
@@ -73,30 +79,28 @@ class CommandLogin extends CommandPolykey {
7379 } ) ,
7480 meta ,
7581 ) ;
76- // We don't expect multiple signatures so a compact JWT will suffice
77- const compactHeader = `${ response . signatures [ 0 ] . protected } .${ response . payload } .${ response . signatures [ 0 ] . signature } ` ;
78- const incomingPayload = tokensUtils . parseTokenPayload < IdentityRequestData > ( payload ) ;
82+
83+ // Send the returned JWT to the returnURL provided by the initial token
84+ const compactHeader = binUtils . jsonToCompactJWT ( response ) ;
85+ const incomingPayload =
86+ tokensUtils . parseTokenPayload < IdentityRequestData > ( payload ) ;
7987 let result : Response ;
8088 try {
81- result = await fetch ( incomingPayload . returnUrl , {
89+ result = await fetch ( incomingPayload . returnURL , {
8290 method : 'POST' ,
8391 body : JSON . stringify ( { token : compactHeader } ) ,
8492 } ) ;
8593 } catch ( e ) {
86- throw new binErrors . ErrorPolykeyCLILoginFailed (
94+ throw new errors . ErrorPolykeyCLILoginFailed (
8795 'Failed to send token to return url' ,
88- { cause : e , } ,
96+ { cause : e } ,
8997 ) ;
9098 }
99+
91100 // Handle non-200 response
92101 if ( ! result . ok ) {
93- throw new binErrors . ErrorPolykeyCLILoginFailed (
94- 'Return url returned failure' ,
95- {
96- data : {
97- code : result . status ,
98- } ,
99- } ,
102+ throw new errors . ErrorPolykeyCLILoginFailed (
103+ `Return url returned failure with code ${ result . status } ` ,
100104 ) ;
101105 }
102106 } finally {
0 commit comments