|
| 1 | +import type PolykeyClient from 'polykey/PolykeyClient.js'; |
| 2 | +import CommandPolykey from '../CommandPolykey.js'; |
| 3 | +import * as binProcessors from '../utils/processors.js'; |
| 4 | +import * as binUtils from '../utils/index.js'; |
| 5 | +import * as binOptions from '../utils/options.js'; |
| 6 | + |
| 7 | +class CommandLogin extends CommandPolykey { |
| 8 | + constructor(...args: ConstructorParameters<typeof CommandPolykey>) { |
| 9 | + super(...args); |
| 10 | + this.name('login'); |
| 11 | + this.description('Login to a platform with Polykey identity'); |
| 12 | + this.argument('<token>', 'Token provided by platform for logging in'); |
| 13 | + this.addOption(binOptions.nodeId); |
| 14 | + this.addOption(binOptions.clientHost); |
| 15 | + this.addOption(binOptions.clientPort); |
| 16 | + this.action(async (token, options) => { |
| 17 | + const { default: PolykeyClient } = await import( |
| 18 | + 'polykey/PolykeyClient.js' |
| 19 | + ); |
| 20 | + const { default: Token } = await import('polykey/tokens/Token.js'); |
| 21 | + const keysUtils = await import('polykey/keys/utils/index.js'); |
| 22 | + const tokensUtils = await import('polykey/tokens/utils.js'); |
| 23 | + |
| 24 | + const clientOptions = await binProcessors.processClientOptions( |
| 25 | + options.nodePath, |
| 26 | + options.nodeId, |
| 27 | + options.clientHost, |
| 28 | + options.clientPort, |
| 29 | + this.fs, |
| 30 | + this.logger.getChild(binProcessors.processClientOptions.name), |
| 31 | + ); |
| 32 | + const meta = await binProcessors.processAuthentication( |
| 33 | + options.passwordFile, |
| 34 | + this.fs, |
| 35 | + ); |
| 36 | + |
| 37 | + let pkClient: PolykeyClient; |
| 38 | + this.exitHandlers.handlers.push(async () => { |
| 39 | + if (pkClient != null) await pkClient.stop(); |
| 40 | + }); |
| 41 | + try { |
| 42 | + pkClient = await PolykeyClient.createPolykeyClient({ |
| 43 | + nodeId: clientOptions.nodeId, |
| 44 | + host: clientOptions.clientHost, |
| 45 | + port: clientOptions.clientPort, |
| 46 | + options: { |
| 47 | + nodePath: options.nodePath, |
| 48 | + }, |
| 49 | + logger: this.logger.getChild(PolykeyClient.name), |
| 50 | + }); |
| 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(); |
| 80 | + const response = await binUtils.retryAuthentication( |
| 81 | + (auth) => |
| 82 | + pkClient.rpcClient.methods.authSignToken({ |
| 83 | + metadata: auth, |
| 84 | + payload: inTok.toEncoded().payload, |
| 85 | + signatures: inTok.toEncoded().signatures, |
| 86 | + // signatures: [{protectees.protecHeaderteok.signature}], |
| 87 | + }), |
| 88 | + meta, |
| 89 | + ); |
| 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`); |
| 102 | + } finally { |
| 103 | + if (pkClient! != null) await pkClient.stop(); |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export default CommandLogin; |
0 commit comments