Skip to content

feat: Add TOTP/2FA support for credential management#58

Merged
arimxyer merged 10 commits into
mainfrom
feature/totp-support
Dec 26, 2025
Merged

feat: Add TOTP/2FA support for credential management#58
arimxyer merged 10 commits into
mainfrom
feature/totp-support

Conversation

@arimxyer
Copy link
Copy Markdown
Collaborator

Summary

  • Add complete TOTP/2FA (Time-based One-Time Password) support for credentials
  • Enable secure storage, generation, and export of TOTP codes

Features

CLI Commands

  • pass-cli add --totp - Interactive prompt for TOTP secret
  • pass-cli add --totp-uri - Direct TOTP URI input
  • pass-cli get --totp - Generate and display TOTP code with countdown
  • pass-cli get --totp-qr - Display QR code in terminal (for adding to another device)
  • pass-cli get --totp-qr-file - Export QR code to PNG file
  • pass-cli update --totp-uri - Update TOTP configuration
  • pass-cli update --clear-totp - Remove TOTP from credential

TUI Integration

  • TOTP indicator in detail view showing issuer
  • t key binding to copy TOTP code to clipboard
  • TOTP Secret/URI input field in Add and Edit forms
  • "Clear TOTP" checkbox in Edit form

Core Functionality

  • Support for standard otpauth:// URIs and raw base32 secrets
  • SHA1, SHA256, SHA512 algorithm support
  • 6 or 8 digit code generation
  • Configurable time periods
  • Terminal QR code display using mdp/qrterminal
  • PNG QR code export using skip2/go-qrcode

Dependencies Added

  • github.com/pquerna/otp v1.5.0 - TOTP generation
  • github.com/mdp/qrterminal/v3 v3.2.1 - Terminal QR codes
  • github.com/skip2/go-qrcode - PNG QR export

Testing

  • All existing tests pass
  • TOTP functionality manually tested

Linear Issue

Closes ARI-52

arimxyer and others added 6 commits December 26, 2025 13:04
Implement TOTP (Time-based One-Time Password) functionality per RFC 6238:

Core TOTP Implementation (internal/vault/totp.go):
- ParseTOTPURI: Parse otpauth:// URIs or raw base32 secrets
- ValidateTOTPSecret: Validate base32 format
- GenerateTOTPCode: Generate 6/8 digit codes with SHA1/SHA256/SHA512
- Credential methods: HasTOTP(), GetTOTPCode(), SetTOTPFromURI(), ClearTOTP()

CLI Commands Updated:
- add: --totp-uri and --totp flags for adding TOTP during credential creation
- get: --totp flag to display TOTP code with countdown and clipboard support
- update: --totp-uri and --clear-totp flags for modifying TOTP configuration

Vault Service:
- Extended Credential struct with TOTP fields (secret, algorithm, digits, period, issuer)
- Extended UpdateOpts with TOTP fields and ClearTOTP boolean
- Added GetTOTPCode() method with audit logging
- Extended CredentialMetadata with HasTOTP and TOTPIssuer

Security/Audit:
- Added TOTP-specific audit events: EventTOTPAccess, EventTOTPAdd, EventTOTPUpdate, EventTOTPClear
- Audit logging integrated into TOTP operations

Dependencies:
- Added github.com/pquerna/otp v1.5.0 for RFC 6238 TOTP implementation

Tests:
- Added comprehensive unit tests for TOTP functionality (18 tests)
- All existing tests pass

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
- Add GetTOTPCode() to VaultService interface and AppState
- Add TOTP display in DetailView (shows issuer when available)
- Add 't' key binding to copy TOTP code to clipboard
- Update help dialog with TOTP shortcut
- Update test mocks with GetTOTPCode method
- Add QR code scanning support in CLI get command

Phase 3 (TUI Integration) of TOTP/2FA support - in progress.

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
- Add GetTOTPCode to testVaultService in detail_test.go
- Add GetTOTPCode to MockVaultService in state_test.go

Completes TUI test mock updates for TOTP integration.

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
- Add TOTP Secret/URI input field to AddForm
- Add TOTP Secret/URI input field to EditForm with status indicator
- Add Clear TOTP checkbox to EditForm (shown when credential has TOTP)
- Update AppState UpdateCredentialOpts with TOTP fields
- Parse TOTP input using vault.ParseTOTPURI on form submission
- Support both base32 secrets and otpauth:// URIs

Completes Phase 3 TUI Integration for TOTP/2FA support.

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
- Add TOTP/2FA Support to Key Features list
- Remove TOTP from Roadmap (feature is shipped)

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
- Add --totp, --totp-uri flags to add command documentation
- Add --totp, --totp-qr, --totp-qr-file flags to get command documentation
- Add --totp-uri, --clear-totp flags to update command documentation
- Add TOTP usage examples for CLI commands
- Add 't' key shortcut to TUI Actions table

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
Comment thread internal/vault/totp.go Fixed
Comment thread internal/vault/totp.go Fixed
Comment thread internal/vault/totp.go Fixed
@arimxyer
Copy link
Copy Markdown
Collaborator Author

@greptile can you take a look and see what's causing the errors and issues

greptile-apps[bot]

This comment was marked as off-topic.

Comment thread internal/vault/totp.go Fixed
Comment thread internal/vault/totp.go Fixed
- Add bounds check for key.Period() conversion (max 300 seconds)
- Add bounds check for cred.TOTPPeriod before conversion
- Add #nosec G115 directives with justification for bounded conversions
- Fix errcheck: properly ignore resp.Body.Close() error
- Fix staticcheck: refactor base32 validation for clarity

Addresses CodeQL/gosec security warnings for uint64->int conversions.

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
@arimxyer arimxyer force-pushed the feature/totp-support branch from 43e3be1 to 46e29ac Compare December 26, 2025 20:03
arimxyer and others added 3 commits December 26, 2025 15:19
The previous implementation used totp.Generate() which re-encoded the
already base32-encoded secret, causing QR codes to contain a different
secret than what's stored and used for code generation.

This fix builds the otpauth:// URI manually, using the stored secret
directly without re-encoding. This ensures that:
- QR codes scanned by authenticator apps generate matching codes
- The secret in the URI matches the secret used for code generation

Added comprehensive tests to verify secret consistency between URI
generation and TOTP code generation.

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
Addresses issues identified in Oracle code review:

- Use %20 for spaces instead of + in query params for max authenticator
  app compatibility
- Fix test flakiness by using same timestamp for both code generations
- Add validation for algorithm (SHA1/SHA256/SHA512 only)
- Add validation for digits (6 or 8 only)
- Add validation for period (1-300 seconds range)
- Add error when both Service and Username are empty (no account identity)
- Normalize secret (trim whitespace, uppercase) before building URI
- Add comprehensive tests for all edge cases

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
- New comprehensive TOTP & 2FA guide (docs/02-guides/totp-guide.md)
- Explains how Service/Username map to authenticator app display
- Documents fallback behavior and best practices
- Added tips in command-reference.md for TOTP URI labeling
- Updated navigation links in index pages

Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
@arimxyer arimxyer merged commit 96c1517 into main Dec 26, 2025
14 checks passed
@arimxyer arimxyer deleted the feature/totp-support branch December 26, 2025 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants