|
| 1 | +import 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'; |
| 8 | +import CommandPolykey from '../CommandPolykey.js'; |
| 9 | +import * as binProcessors from '../utils/processors.js'; |
| 10 | +import * as binParsers from '../utils/parsers.js'; |
| 11 | +import * as binUtils from '../utils/index.js'; |
| 12 | +import * as binOptions from '../utils/options.js'; |
| 13 | +import * as errors from '../errors.js'; |
| 14 | + |
| 15 | +class CommandLogin extends CommandPolykey { |
| 16 | + constructor(...args: ConstructorParameters<typeof CommandPolykey>) { |
| 17 | + super(...args); |
| 18 | + this.name('login'); |
| 19 | + this.description('Login to a platform with Polykey identity'); |
| 20 | + this.argument( |
| 21 | + '<token>', |
| 22 | + 'Token provided by platform for logging in', |
| 23 | + binParsers.parseCompactJWT, |
| 24 | + ); |
| 25 | + this.addOption(binOptions.nodeId); |
| 26 | + this.addOption(binOptions.clientHost); |
| 27 | + this.addOption(binOptions.clientPort); |
| 28 | + this.action(async (encodedToken, options) => { |
| 29 | + const { default: PolykeyClient } = await import( |
| 30 | + 'polykey/PolykeyClient.js' |
| 31 | + ); |
| 32 | + const tokensUtils = await import('polykey/tokens/utils.js'); |
| 33 | + const clientOptions = await binProcessors.processClientOptions( |
| 34 | + options.nodePath, |
| 35 | + options.nodeId, |
| 36 | + options.clientHost, |
| 37 | + options.clientPort, |
| 38 | + this.fs, |
| 39 | + this.logger.getChild(binProcessors.processClientOptions.name), |
| 40 | + ); |
| 41 | + const meta = await binProcessors.processAuthentication( |
| 42 | + options.passwordFile, |
| 43 | + this.fs, |
| 44 | + ); |
| 45 | + |
| 46 | + let pkClient: PolykeyClient; |
| 47 | + this.exitHandlers.handlers.push(async () => { |
| 48 | + if (pkClient != null) await pkClient.stop(); |
| 49 | + }); |
| 50 | + try { |
| 51 | + pkClient = await PolykeyClient.createPolykeyClient({ |
| 52 | + nodeId: clientOptions.nodeId, |
| 53 | + host: clientOptions.clientHost, |
| 54 | + port: clientOptions.clientPort, |
| 55 | + options: { |
| 56 | + nodePath: options.nodePath, |
| 57 | + }, |
| 58 | + logger: this.logger.getChild(PolykeyClient.name), |
| 59 | + }); |
| 60 | + |
| 61 | + // Create a JSON representation of the encoded header |
| 62 | + const [protectedHeader, payload, signature] = encodedToken; |
| 63 | + const incomingTokenEncoded = { |
| 64 | + payload: payload as TokenPayloadEncoded, |
| 65 | + signatures: [ |
| 66 | + { |
| 67 | + protected: protectedHeader as TokenProtectedHeaderEncoded, |
| 68 | + signature: signature as TokenSignatureEncoded, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }; |
| 72 | + |
| 73 | + // Get it verified and signed by the agent |
| 74 | + const response = await binUtils.retryAuthentication( |
| 75 | + (auth) => |
| 76 | + pkClient.rpcClient.methods.authSignToken({ |
| 77 | + metadata: auth, |
| 78 | + ...incomingTokenEncoded, |
| 79 | + }), |
| 80 | + meta, |
| 81 | + ); |
| 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); |
| 87 | + let result: Response; |
| 88 | + try { |
| 89 | + result = await fetch(incomingPayload.returnURL, { |
| 90 | + method: 'POST', |
| 91 | + body: JSON.stringify({ token: compactHeader }), |
| 92 | + }); |
| 93 | + } catch (e) { |
| 94 | + throw new errors.ErrorPolykeyCLILoginFailed( |
| 95 | + 'Failed to send token to return url', |
| 96 | + { cause: e }, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // Handle non-200 response |
| 101 | + if (!result.ok) { |
| 102 | + throw new errors.ErrorPolykeyCLILoginFailed( |
| 103 | + `Return url returned failure with code ${result.status}`, |
| 104 | + ); |
| 105 | + } |
| 106 | + } finally { |
| 107 | + if (pkClient! != null) await pkClient.stop(); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export default CommandLogin; |
0 commit comments