interfaces.go

v1.0.0
Doc Versions Source
1
package bitwarden
2
3
import (
4
	"context"
5
	"io"
6
)
7
8
// VaultClient defines the unified interface for interacting with a Bitwarden vault.
9
// Both Client (CLI-based via bw serve) and DirectClient (direct server communication)
10
// implement this interface, allowing applications to work with either mode seamlessly.
11
//
12
// The interface provides methods for vault lifecycle management (Status, Sync, Lock, Unlock),
13
// password generation, and access to various vault services through sub-APIs.
14
type VaultClient interface {
15
	// Status returns the current vault status including lock state and user info.
16
	Status(ctx context.Context) (*Status, error)
17
18
	// Sync downloads the latest vault data from the server.
19
	Sync(ctx context.Context) error
20
21
	// Lock clears encryption keys and vault data from memory.
22
	Lock(ctx context.Context) error
23
24
	// Unlock authenticates and derives encryption keys from the password.
25
	Unlock(ctx context.Context, password string) error
26
27
	// Generate creates a password or passphrase based on the provided options.
28
	Generate(ctx context.Context, opts GenerateOptions) (string, error)
29
30
	// Items returns the vault items (ciphers) API.
31
	Items() ItemsAPI
32
33
	// Folders returns the folders API.
34
	Folders() FoldersAPI
35
36
	// Collections returns the user collections API.
37
	Collections() CollectionsAPI
38
39
	// OrgCollections returns the organization collections API.
40
	OrgCollections() OrgCollectionsAPI
41
42
	// Organizations returns the organizations API.
43
	Organizations() OrganizationsAPI
44
45
	// OrgMembers returns the organization members API.
46
	OrgMembers() OrgMembersAPI
47
48
	// Send returns the Bitwarden Send API.
49
	Send() SendAPI
50
51
	// Attachments returns the file attachments API.
52
	Attachments() AttachmentsAPI
53
}
54
55
// ItemsAPI defines operations on vault items (ciphers).
56
// Items are the primary data type in a vault and can be logins, secure notes,
57
// cards, identities, or SSH keys.
58
type ItemsAPI interface {
59
	// List retrieves items from the vault with optional filtering.
60
	List(ctx context.Context, filter ListFilter) ([]Item, error)
61
62
	// Get retrieves a single item by its ID.
63
	Get(ctx context.Context, id string) (*Item, error)
64
65
	// Create adds a new item to the vault.
66
	Create(ctx context.Context, item Item) (*Item, error)
67
68
	// Update modifies an existing item.
69
	Update(ctx context.Context, id string, item Item) (*Item, error)
70
71
	// Delete removes an item (moves to trash).
72
	Delete(ctx context.Context, id string) error
73
74
	// Restore recovers a deleted item from the trash.
75
	Restore(ctx context.Context, id string) error
76
77
	// Move transfers an item to an organization with specified collections.
78
	Move(ctx context.Context, id, organizationID string, collectionIDs []string) error
79
}
80
81
// FoldersAPI defines operations on folders for organizing vault items.
82
type FoldersAPI interface {
83
	// List retrieves folders with optional search filter.
84
	List(ctx context.Context, search string) ([]Folder, error)
85
86
	// Get retrieves a single folder by its ID.
87
	Get(ctx context.Context, id string) (*Folder, error)
88
89
	// Create adds a new folder to the vault.
90
	Create(ctx context.Context, folder Folder) (*Folder, error)
91
92
	// Update modifies an existing folder.
93
	Update(ctx context.Context, id string, folder Folder) (*Folder, error)
94
95
	// Delete removes a folder from the vault.
96
	Delete(ctx context.Context, id string) error
97
}
98
99
// CollectionsAPI defines read-only operations on user collections.
100
// Collections are groups of items owned by organizations.
101
type CollectionsAPI interface {
102
	// List retrieves collections visible to the user with optional filtering.
103
	List(ctx context.Context, organizationID, search string) ([]Collection, error)
104
105
	// Get retrieves a single collection by its ID.
106
	Get(ctx context.Context, id string) (*Collection, error)
107
}
108
109
// OrgCollectionsAPI defines operations on organization collections.
110
// Users with appropriate permissions can create, update, and delete org collections.
111
type OrgCollectionsAPI interface {
112
	// List retrieves collections in an organization with optional search.
113
	List(ctx context.Context, organizationID string, search string) ([]OrgCollection, error)
114
115
	// Get retrieves a single organization collection by its ID.
116
	Get(ctx context.Context, id string) (*OrgCollection, error)
117
118
	// Create adds a new collection to the organization.
119
	Create(ctx context.Context, collection OrgCollection) (*OrgCollection, error)
120
121
	// Update modifies an existing organization collection.
122
	Update(ctx context.Context, id string, collection OrgCollection) (*OrgCollection, error)
123
124
	// Delete removes a collection from the organization.
125
	Delete(ctx context.Context, id string) error
126
}
127
128
// OrganizationsAPI defines read-only operations on organizations.
129
type OrganizationsAPI interface {
130
	// List retrieves organizations the user belongs to with optional search.
131
	List(ctx context.Context, search string) ([]Organization, error)
132
133
	// Get retrieves a single organization by its ID.
134
	Get(ctx context.Context, id string) (*Organization, error)
135
}
136
137
// OrgMembersAPI defines operations on organization members.
138
type OrgMembersAPI interface {
139
	// List retrieves all members in an organization.
140
	List(ctx context.Context, organizationID string) ([]OrgMember, error)
141
142
	// Confirm accepts a pending member into the organization.
143
	Confirm(ctx context.Context, id, organizationID string) error
144
}
145
146
// SendAPI defines operations on Bitwarden Send objects.
147
// Send allows secure sharing of data (text or files) with anyone, even non-Bitwarden users.
148
type SendAPI interface {
149
	// List retrieves all Sends owned by the user.
150
	List(ctx context.Context) ([]Send, error)
151
152
	// Get retrieves a single Send by its ID.
153
	Get(ctx context.Context, id string) (*Send, error)
154
155
	// Create adds a new Send to the vault.
156
	Create(ctx context.Context, send Send) (*Send, error)
157
158
	// Update modifies an existing Send.
159
	Update(ctx context.Context, id string, send Send) (*Send, error)
160
161
	// Delete removes a Send from the vault.
162
	Delete(ctx context.Context, id string) error
163
164
	// RemovePassword removes the password protection from a Send.
165
	RemovePassword(ctx context.Context, id string) (*Send, error)
166
}
167
168
// AttachmentsAPI defines operations on file attachments for vault items.
169
type AttachmentsAPI interface {
170
	// Create uploads a file attachment to a vault item.
171
	Create(ctx context.Context, itemID, filename string, file io.Reader) (*Item, error)
172
173
	// Get downloads a file attachment's contents.
174
	Get(ctx context.Context, attachmentID, itemID string) ([]byte, error)
175
176
	// Delete removes a file attachment from a vault item.
177
	Delete(ctx context.Context, attachmentID, itemID string) error
178
}
179
180
// Verify existing concrete types satisfy the interfaces at compile time.
181
var (
182
	_ ItemsAPI          = (*ItemsService)(nil)
183
	_ FoldersAPI        = (*FoldersService)(nil)
184
	_ CollectionsAPI    = (*CollectionsService)(nil)
185
	_ OrgCollectionsAPI = (*OrgCollectionsService)(nil)
186
	_ OrganizationsAPI  = (*OrganizationsService)(nil)
187
	_ OrgMembersAPI     = (*OrgMembersService)(nil)
188
	_ SendAPI           = (*SendService)(nil)
189
	_ AttachmentsAPI    = (*AttachmentsService)(nil)
190
)
191

Source Files