11import type PolykeyClient from 'polykey/PolykeyClient.js' ;
2+ import type {
3+ TokenPayloadEncoded ,
4+ TokenProtectedHeaderEncoded ,
5+ TokenSignatureEncoded ,
6+ } from 'polykey/tokens/types.js' ;
7+ import type { IdentityRequestData } from 'polykey/client/types.js' ;
28import CommandPolykey from '../CommandPolykey.js' ;
39import * as binProcessors from '../utils/processors.js' ;
410import * as binUtils from '../utils/index.js' ;
511import * as binOptions from '../utils/options.js' ;
12+ import * as binErrors from '../errors.js' ;
613
714class CommandLogin extends CommandPolykey {
815 constructor ( ...args : ConstructorParameters < typeof CommandPolykey > ) {
@@ -17,10 +24,7 @@ class CommandLogin extends CommandPolykey {
1724 const { default : PolykeyClient } = await import (
1825 'polykey/PolykeyClient.js'
1926 ) ;
20- const { default : Token } = await import ( 'polykey/tokens/Token.js' ) ;
21- const keysUtils = await import ( 'polykey/keys/utils/index.js' ) ;
2227 const tokensUtils = await import ( 'polykey/tokens/utils.js' ) ;
23-
2428 const clientOptions = await binProcessors . processClientOptions (
2529 options . nodePath ,
2630 options . nodeId ,
@@ -48,57 +52,53 @@ class CommandLogin extends CommandPolykey {
4852 } ,
4953 logger : this . logger . getChild ( PolykeyClient . name ) ,
5054 } ) ;
51- const keyPair = keysUtils . generateKeyPair ( ) ;
52- const inTok = Token . fromPayload ( {
53- returnUrl : 'localhost:8000' ,
54- publicKey : keyPair . publicKey . toString ( 'base64url' ) ,
55- } ) ;
56- inTok . signWithPrivateKey ( keyPair ) ;
57- console . log ( `tok: ${ inTok . toEncoded ( ) } ` ) ;
58- // token = inTok.toEncoded();
59-
60- // // Compact JWTs are in xxxx.yyyy.zzzz format where x is the protected
61- // // header, y is the payload, and z is the binary signature.
62- // const [protectedHeader, payload, signature] = token.split('.');
63- // const tokenProtectedHeader =
64- // tokensUtils.parseTokenProtectedHeader(protectedHeader);
65- // const tokenPayload = tokensUtils.parseTokenPayload(payload);
66- // const tokenSignature = tokensUtils.parseTokenSignature(signature);
67- // const parsedToken = {
68- // payload: tokenPayload,
69- // signatures: [
70- // {
71- // protected: tokenProtectedHeader,
72- // signature: tokenSignature,
73- // }
74- // ]
75- // };
76- const parsedToken = inTok ;
77- console . log ( `parsed: ${ JSON . stringify ( parsedToken ) } \n` ) ;
78- // const incomingToken = Token.fromSigned(parsedToken);
79- // const tokenJson = incomingToken.toJSON();
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 ( '.' ) ;
59+ const incomingTokenEncoded = {
60+ payload : payload as TokenPayloadEncoded ,
61+ signatures : [
62+ {
63+ protected : protectedHeader as TokenProtectedHeaderEncoded ,
64+ signature : signature as TokenSignatureEncoded ,
65+ } ,
66+ ] ,
67+ } ;
8068 const response = await binUtils . retryAuthentication (
8169 ( auth ) =>
8270 pkClient . rpcClient . methods . authSignToken ( {
8371 metadata : auth ,
84- payload : inTok . toEncoded ( ) . payload ,
85- signatures : inTok . toEncoded ( ) . signatures ,
86- // signatures: [{protectees.protecHeaderteok.signature}],
72+ ...incomingTokenEncoded ,
8773 } ) ,
8874 meta ,
8975 ) ;
90- const tokenOut = {
91- payload : response . payload ,
92- signatures : response . signatures ,
93- } ;
94- console . log ( `received: ${ JSON . stringify ( tokenOut ) } \n` ) ;
95- console . log ( `payload: ${ JSON . stringify ( tokensUtils . parseTokenPayload ( tokenOut . payload ) ) } \n` ) ;
96- console . log ( `inc payload: ${ JSON . stringify ( tokensUtils . parseTokenPayload ( ( tokensUtils . parseTokenPayload ( tokenOut . payload ) . requestToken ! as any ) . payload ! ) ) } \n` ) ;
97- // await fetch(parsedToken.payload.returnUrl, {
98- // method: 'POST',
99- // body: JSON.stringify(tokenOut),
100- // });
101- // console.log(`sent payload`);
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 ) ;
79+ let result : Response ;
80+ try {
81+ result = await fetch ( incomingPayload . returnUrl , {
82+ method : 'POST' ,
83+ body : JSON . stringify ( { token : compactHeader } ) ,
84+ } ) ;
85+ } catch ( e ) {
86+ throw new binErrors . ErrorPolykeyCLILoginFailed (
87+ 'Failed to send token to return url' ,
88+ { cause : e , } ,
89+ ) ;
90+ }
91+ // Handle non-200 response
92+ if ( ! result . ok ) {
93+ throw new binErrors . ErrorPolykeyCLILoginFailed (
94+ 'Return url returned failure' ,
95+ {
96+ data : {
97+ code : result . status ,
98+ } ,
99+ } ,
100+ ) ;
101+ }
102102 } finally {
103103 if ( pkClient ! != null ) await pkClient . stop ( ) ;
104104 }
0 commit comments