Overview
TheCipherService is the core service for managing vault items (ciphers) in Bitwarden. It handles encryption, decryption, synchronization, and CRUD operations for all cipher types including logins, secure notes, cards, identities, and SSH keys.
Source: libs/common/src/vault/services/cipher.service.ts
Key Features
- Encrypt and decrypt vault items
- Create, read, update, and delete ciphers
- Manage cipher attachments
- Handle cipher collections and folders
- Filter ciphers by URL for autofill
- Track last used dates for ciphers
- Soft delete and restore operations
- Bulk operations for multiple ciphers
Type Definitions
EncryptionContext
Observables
cipherViews$
Observable that emits an array of decrypted ciphers for the active user.userId: UserId- The user ID to get ciphers for
null if decryption is in progress
Example:
cipherListViews$
Observable that emits SDK-optimized cipher list views for better performance.userId: UserId- The user ID to get cipher list views for
ciphers$
Observable that emits an object of encrypted ciphers.failedToDecryptCiphers$
Observable that emits ciphers that failed to decrypt.Core Methods
encrypt
Encrypts a cipher view for storage.model: CipherView- The cipher view to encryptuserId: UserId- The user ID performing the encryptionkeyForEncryption?: SymmetricCryptoKey- Optional encryption keykeyForCipherKeyDecryption?: SymmetricCryptoKey- Optional decryption key for cipher keyoriginalCipher?: Cipher- Optional original cipher for history tracking
decrypt
Decrypts a cipher using SDK or legacy method.cipher: Cipher- The encrypted cipher to decryptuserId: UserId- The user ID to use for decryption
get
Retrieves a single cipher by ID.id: string- The cipher IDuserId: UserId- The user ID
null if not found
getAll
Retrieves all ciphers for a user.getAllDecrypted
Retrieves and decrypts all ciphers for a user.CRUD Operations
createWithServer
Creates a new cipher on the server.cipherView: CipherView- The cipher to createuserId: UserId- The user ID creating the cipherorgAdmin?: boolean- If true, creates as organization admin
updateWithServer
Updates an existing cipher on the server.cipherView: CipherView- The cipher to updateuserId: UserId- The user ID updating the cipheroriginalCipherView?: CipherView- Optional original cipher for comparisonorgAdmin?: boolean- If true, updates as organization admin
deleteWithServer
Permanently deletes a cipher from the server.id: string- The cipher ID to deleteuserId: UserId- The user ID performing the deletionasAdmin?: boolean- If true, deletes as organization admin
deleteManyWithServer
Deletes multiple ciphers from the server.Soft Delete & Restore
softDelete
Soft deletes a cipher (moves to trash).id: string | string[]- Cipher ID or array of IDs to soft deleteuserId: UserId- The user ID
softDeleteWithServer
Soft deletes a cipher on the server.restore
Restores a soft-deleted cipher.restoreWithServer
Restores a cipher from trash on the server.URL Filtering
getAllDecryptedForUrl
Retrieves ciphers matching a specific URL for autofill.url: string- The URL to match againstuserId: UserId- The user IDincludeOtherTypes?: CipherType[]- Additional cipher types to includedefaultMatch?: UriMatchStrategySetting- URI matching strategyoverrideNeverMatchStrategy?: true- Override never match setting
filterCiphersForUrl
Filters an array of ciphers for a specific URL.Attachment Management
saveAttachmentWithServer
Saves a file attachment to a cipher.cipher: Cipher- The cipher to attach the file tounencryptedFile: any- The file to attachuserId: UserId- The user IDadmin?: boolean- If true, saves as admin
deleteAttachmentWithServer
Deletes an attachment from a cipher.Collection Management
saveCollectionsWithServer
Saves collection assignments for a cipher.cipher: Cipher- The cipher with updated collection IDsuserId: UserId- The user ID
bulkUpdateCollectionsWithServer
Bulk updates collections for multiple ciphers.orgId: OrganizationId- The organization IDuserId: UserId- The user IDcipherIds: CipherId[]- Array of cipher IDs to updatecollectionIds: CollectionId[]- Collections to add or removeremoveCollections: boolean- If true, removes collections; if false, adds them
Organization Sharing
shareWithServer
Shares a cipher with an organization.cipher: CipherView- The cipher to shareorganizationId: string- The organization to share withcollectionIds: string[]- Collections to assign the cipher touserId: UserId- The user IDoriginalCipher?: Cipher- Optional original cipher
shareManyWithServer
Shares multiple ciphers with an organization.Utility Methods
upsert
Inserts or updates cipher data in local storage.clearCache
Clears the decrypted cipher cache for a user.updateLastUsedDate
Updates the last used date for a cipher.sortCiphersByLastUsed
Sorting function for ciphers by last used date.getLocaleSortingFunction
Returns a locale-aware sorting function for ciphers.Related Types
CipherView- Decrypted cipher view (libs/common/src/vault/models/view/cipher.view.ts)Cipher- Encrypted cipher domain modelCipherData- Raw cipher dataCipherType- Enum for cipher types (Login, SecureNote, Card, Identity, SshKey)AttachmentView- File attachment viewFieldView- Custom field view
See Also
- Folder Service - Folder organization
- Collection Service - Collection management