-
Notifications
You must be signed in to change notification settings - Fork 3
Login and records implementation #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ukumar-ks
wants to merge
1
commit into
release
Choose a base branch
from
login-and-records-implementation
base: release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "name": "keeper-sdk", | ||
| "version": "1.0.0", | ||
| "description": "High-level wrapper for Keeper Security JavaScript SDK", | ||
| "main": "src/index.ts", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "clean": "rm -rf dist", | ||
| "link-local": "npm link ../keeperapi", | ||
| "format": "prettier --write .", | ||
| "format:check": "prettier --check .", | ||
| "test": "jest", | ||
| "types": "tsc --watch", | ||
| "types:ci": "tsc" | ||
| }, | ||
| "dependencies": { | ||
| "@keeper-security/keeperapi": "17.1.0", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no change needed now-- but I will probably explore more formally re-organizing the project into |
||
| "@types/node": "^20.9.1", | ||
| "ts-node": "^10.7.0", | ||
| "typescript": "^4.6.3" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import readline from 'readline/promises' | ||
| import { setTimeout as delay } from 'timers/promises' | ||
| import type { AuthUI3, DeviceApprovalChannel, TwoFactorChannelData } from '@keeper-security/keeperapi' | ||
| import { Authentication } from '@keeper-security/keeperapi' | ||
| import { logger, extractErrorMessage, KeeperSdkError, AuthDefaults, ResultCodes } from '../utils' | ||
|
|
||
| export class ConsoleAuthUI implements AuthUI3 { | ||
| private static readonly DEVICE_VERIFICATION = { | ||
| Email: 0, | ||
| KeeperPush: 1, | ||
| TFA: 2, | ||
| AdminApproval: 3, | ||
| } as const | ||
|
|
||
| private static channelName(channel: number): string { | ||
| switch (channel) { | ||
| case ConsoleAuthUI.DEVICE_VERIFICATION.Email: | ||
| return 'Email Verification' | ||
| case ConsoleAuthUI.DEVICE_VERIFICATION.KeeperPush: | ||
| return 'Keeper Push' | ||
| case ConsoleAuthUI.DEVICE_VERIFICATION.TFA: | ||
| return 'Two-Factor Authentication' | ||
| case ConsoleAuthUI.DEVICE_VERIFICATION.AdminApproval: | ||
| return 'Admin Approval' | ||
| default: | ||
| return `Channel ${channel}` | ||
| } | ||
| } | ||
|
|
||
| private static twoFactorChannelName(channelType: Authentication.TwoFactorChannelType): string { | ||
| switch (channelType) { | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_TOTP: | ||
| return 'Authenticator App (TOTP)' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_SMS: | ||
| return 'SMS' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_DUO: | ||
| return 'Duo Security' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_RSA: | ||
| return 'RSA SecurID' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_DNA: | ||
| return 'Keeper DNA' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_U2F: | ||
| return 'U2F Security Key' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_WEBAUTHN: | ||
| return 'WebAuthn Security Key' | ||
| case Authentication.TwoFactorChannelType.TWO_FA_CT_KEEPER: | ||
| return 'Keeper' | ||
| default: | ||
| throw new KeeperSdkError(`Unsupported 2FA channel type: ${channelType}`, ResultCodes.UNSUPPORTED_2FA_CHANNEL) | ||
| } | ||
| } | ||
|
|
||
| private static async waitWithCancel(timeoutMs: number, cancel?: Promise<void>): Promise<void> { | ||
| if (!cancel) { | ||
| await delay(timeoutMs) | ||
| return | ||
| } | ||
| await Promise.race([delay(timeoutMs), cancel]) | ||
| } | ||
|
|
||
| public async waitForDeviceApproval(channels: DeviceApprovalChannel[], isCloud: boolean): Promise<boolean> { | ||
| const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) | ||
|
|
||
| try { | ||
| logger.info('\n--- Device Approval Required ---') | ||
| logger.info('This device needs to be approved before you can log in.') | ||
| logger.info('Available verification methods:') | ||
| channels.forEach((ch, i) => { | ||
| logger.info(` ${i + 1}. ${ConsoleAuthUI.channelName(ch.channel)}`) | ||
| }) | ||
|
|
||
| const choice = (await rl.question('\nSelect method (number): ')).trim() | ||
| const idx = parseInt(choice, 10) - 1 | ||
|
|
||
| if (isNaN(idx) || idx < 0 || idx >= channels.length) { | ||
| logger.warn('Invalid selection, cancelling.') | ||
| return false | ||
| } | ||
|
|
||
| const selected = channels[idx] | ||
| logger.info(`\nSending ${ConsoleAuthUI.channelName(selected.channel)} request...`) | ||
| await selected.sendApprovalRequest() | ||
|
|
||
| if (selected.validateCode) { | ||
| const code = (await rl.question('Enter verification code: ')).trim() | ||
| if (!code) return false | ||
| await selected.validateCode(code) | ||
| } else { | ||
| logger.info('Approval request sent. Waiting for approval on your other device...') | ||
| await ConsoleAuthUI.waitWithCancel(AuthDefaults.APPROVAL_TIMEOUT_MS) | ||
| } | ||
|
|
||
| return true | ||
| } catch (e) { | ||
| logger.error('Device approval error:', extractErrorMessage(e)) | ||
| return false | ||
| } finally { | ||
| rl.close() | ||
| } | ||
| } | ||
|
|
||
| public async waitForTwoFactorCode(channels: TwoFactorChannelData[], cancel: Promise<void>): Promise<boolean> { | ||
| const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) | ||
|
|
||
| try { | ||
| logger.info('\n--- Two-Factor Authentication Required ---') | ||
| logger.info('Available 2FA methods:') | ||
| channels.forEach((ch, i) => { | ||
| const name = ConsoleAuthUI.twoFactorChannelName(ch.channel.channelType!) | ||
| logger.info(` ${i + 1}. ${name}`) | ||
| }) | ||
|
|
||
| const choice = (await rl.question('\nSelect method (number): ')).trim() | ||
| const idx = parseInt(choice, 10) - 1 | ||
|
|
||
| if (isNaN(idx) || idx < 0 || idx >= channels.length) { | ||
| logger.warn('Invalid selection, cancelling.') | ||
| return false | ||
| } | ||
|
|
||
| const selected = channels[idx] | ||
| const name = ConsoleAuthUI.twoFactorChannelName(selected.channel.channelType!) | ||
|
|
||
| if (selected.availablePushes && selected.availablePushes.length > 0) { | ||
| const pushChoice = (await rl.question(`Send push notification for ${name}? (y/n): `)).trim() | ||
| if (pushChoice.toLowerCase() === 'y' && selected.sendPush) { | ||
| selected.sendPush(selected.availablePushes[0]) | ||
| logger.info('Push sent. Waiting for approval...') | ||
| await ConsoleAuthUI.waitWithCancel(AuthDefaults.APPROVAL_TIMEOUT_MS, cancel) | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| const code = (await rl.question(`Enter ${name} code: `)).trim() | ||
| if (!code) return false | ||
|
|
||
| selected.sendCode(code) | ||
| await ConsoleAuthUI.waitWithCancel(AuthDefaults.CODE_VALIDATION_DELAY_MS, cancel) | ||
| return true | ||
| } catch (e) { | ||
| logger.error('2FA error:', extractErrorMessage(e)) | ||
| return false | ||
| } finally { | ||
| rl.close() | ||
| } | ||
| } | ||
|
|
||
| public async getPassword(isAlternate: boolean): Promise<string> { | ||
| const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) | ||
| try { | ||
| const label = isAlternate ? 'alternate master password' : 'master password' | ||
| return (await rl.question(`Enter your ${label}: `)).trim() | ||
| } finally { | ||
| rl.close() | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're going to publish as a separate package called @keeper-security/keeper-sdk-javascript-- name here should be updated accordingly