virgil-sdk 4.2.4 → 4.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/README.md +1 -1
- data/README_LOW_LEVEL.md +427 -0
- data/lib/virgil/sdk/client/card.rb +9 -7
- data/lib/virgil/sdk/client/http/cards_service_connection.rb +6 -0
- data/lib/virgil/sdk/client/requests.rb +4 -0
- data/lib/virgil/sdk/client/requests/add_relation_request.rb +52 -0
- data/lib/virgil/sdk/client/requests/create_card_request.rb +2 -1
- data/lib/virgil/sdk/client/requests/delete_relation_request.rb +51 -0
- data/lib/virgil/sdk/client/requests/signable_request.rb +9 -6
- data/lib/virgil/sdk/client/virgil_client.rb +45 -4
- data/lib/virgil/sdk/cryptography/virgil_crypto.rb +8 -2
- data/lib/virgil/sdk/high_level.rb +2 -2
- data/lib/virgil/sdk/high_level/virgil_buffer.rb +8 -10
- data/lib/virgil/sdk/high_level/virgil_card.rb +41 -11
- data/lib/virgil/sdk/high_level/virgil_card_manager.rb +14 -6
- data/lib/virgil/sdk/high_level/virgil_context.rb +6 -4
- data/lib/virgil/sdk/high_level/virgil_key.rb +69 -17
- data/lib/virgil/sdk/high_level/virgil_key_manager.rb +10 -0
- data/lib/virgil/sdk/version.rb +1 -1
- data/virgil-sdk.gemspec +1 -1
- metadata +7 -5
- data/lib/virgil/sdk/client/signatures_base64.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: caf90993b1ff232ef33d9e7f602312730d8f8ced
|
4
|
+
data.tar.gz: 8973c40c7a43ca0910ae2ac4d22efdd0e1926635
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a38a32dec1433ed979d0231711dd8614574ee85b94b0f2cbaf661058b8a55f86a6f28f14262df3b4418a4ac76c173536784b8b0b745da4f665b97c55919e7494
|
7
|
+
data.tar.gz: 38ec5674f9eb5f959422b4d11167618aaaf688a5eb90bb41aa89c85fef6f3066341ef66630cdb524fb23dc7e0c752f054d5c70e70ee4be989960ce4048652dc3
|
data/.gitignore
CHANGED
data/README.md
CHANGED
data/README_LOW_LEVEL.md
ADDED
@@ -0,0 +1,427 @@
|
|
1
|
+
# Ruby SDK Programming Guide
|
2
|
+
|
3
|
+
This guide is a practical introduction to creating Ruby apps using Virgil Security features. The code examples in this guide are written in Ruby.
|
4
|
+
|
5
|
+
In this guide you will find code for every task you need to implement in order to create an application using Virgil Security. It also includes a description of the main classes and methods. The aim of this guide is to get you up and running quickly. You should be able to copy and paste the code provided into your own apps and use it with minumal changes.
|
6
|
+
|
7
|
+
## Table of Contents
|
8
|
+
|
9
|
+
* [Setting up your project](#setting-up-your-project)
|
10
|
+
* [User and App Credentials](#user-and-app-credentials)
|
11
|
+
* [Creating a Virgil Card](#creating-a-virgil-card)
|
12
|
+
* [Search for Virgil Cards](#search-for-virgil-cards)
|
13
|
+
* [Getting a Virgil Card](#getting-a-virgil-card)
|
14
|
+
* [Validating Virgil Cards](#validating-virgil-cards)
|
15
|
+
* [Revoking a Virgil Card](#revoking-a-virgil-card)
|
16
|
+
* [Operations with Crypto Keys](#operations-with-crypto-keys)
|
17
|
+
* [Generate Keys](#generate-keys)
|
18
|
+
* [Import and Export Keys](#import-and-export-keys)
|
19
|
+
* [Encryption and Decryption](#encryption-and-decryption)
|
20
|
+
* [Encrypt Data](#encrypt-data)
|
21
|
+
* [Decrypt Data](#decrypt-data)
|
22
|
+
* [Generating and Verifying Signatures](#generating-and-verifying-signatures)
|
23
|
+
* [Generating a Signature](#generating-a-signature)
|
24
|
+
* [Verifying a Signature](#verifying-a-signature)
|
25
|
+
* [Authenticated Encryption](#authenticated-encryption)
|
26
|
+
* [Sign then Encrypt](#sign-then-encrypt)
|
27
|
+
* [Decrypt then Verify](#decrypt-then-verify)
|
28
|
+
* [Fingerprint Generation](#fingerprint-generation)
|
29
|
+
* [Release Notes](#release-notes)
|
30
|
+
|
31
|
+
## Setting up your project
|
32
|
+
|
33
|
+
The Virgil SDK is provided as a gem named *virgil-sdk*. The package is distributed via *bundler* package manager.
|
34
|
+
|
35
|
+
### Target platforms
|
36
|
+
|
37
|
+
* Ruby 2.0+
|
38
|
+
|
39
|
+
### Installation
|
40
|
+
|
41
|
+
To install package use the command below:
|
42
|
+
|
43
|
+
```
|
44
|
+
gem install virgil-sdk --pre
|
45
|
+
```
|
46
|
+
|
47
|
+
or add the following line to your Gemfile:
|
48
|
+
|
49
|
+
```
|
50
|
+
gem 'virgil-sdk', '~> 4.0.0b'
|
51
|
+
```
|
52
|
+
|
53
|
+
## User and App Credentials
|
54
|
+
|
55
|
+
When you register an application on the Virgil developer's [dashboard](https://developer.virgilsecurity.com/dashboard), we provide you with an *app_id*, *app_key* and *access_token*.
|
56
|
+
|
57
|
+
* **app_id** uniquely identifies your application in our services, it is also used to identify the Public key generated in a pair with *app_key*, for example: ```af6799a2f26376731abb9abf32b5f2ac0933013f42628498adb6b12702df1a87```
|
58
|
+
* **app_key** is a Private key that is used to perform creation and revocation of *Virgil Cards* (Public key) in Virgil services. Also the *app_key* can be used for cryptographic operations to take part in application logic. The *app_key* is generated at the time of creation application and has to be saved in secure place.
|
59
|
+
* **access_token** is a unique string value that provides an authenticated secure access to the Virgil services and is passed with each API call. The *accessToken* also allows the API to associate your app’s requests with your Virgil developer’s account.
|
60
|
+
|
61
|
+
## Connecting to Virgil
|
62
|
+
Before you can use any Virgil services features in your app, you must first initialize ```VirgilClient``` class from ```Virgil::SDK::Client``` module. You use the ```VirgilClient``` object to get access to Create, Revoke and Search for *Virgil Cards* (Public keys).
|
63
|
+
|
64
|
+
### Initializing an API Client
|
65
|
+
|
66
|
+
To create an instance of *VirgilClient* class, just call its constructor with your application's *access_token* which you generated on developer's deshboard.
|
67
|
+
|
68
|
+
Module: ```Virgil::SDK::Client```
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require 'virgil/sdk'
|
72
|
+
|
73
|
+
client = Virgil::SDK::Client::VirgilClient.new("[YOUR_ACCESS_TOKEN_HERE]")
|
74
|
+
```
|
75
|
+
|
76
|
+
you can also customize initialization using your own parameters
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
client = Virgil::SDK::Client::VirgilClient.new(
|
80
|
+
"[YOUR_ACCESS_TOKEN_HERE]",
|
81
|
+
https://cards.virgilsecurity.com",
|
82
|
+
https://cards-ro.virgilsecurity.com"
|
83
|
+
)
|
84
|
+
```
|
85
|
+
|
86
|
+
### Initializing Crypto
|
87
|
+
|
88
|
+
The *VirgilCrypto* class provides cryptographic operations in applications, such as hashing, signature generation and verification, and encryption and decryption.
|
89
|
+
|
90
|
+
Module: ```Virgil::SDK::Cryptography```
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'virgil/sdk'
|
94
|
+
|
95
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
96
|
+
```
|
97
|
+
|
98
|
+
## Creating a Virgil Card
|
99
|
+
|
100
|
+
A *Virgil Card* is the main entity of the Virgil services, it includes the information about the user and his public key. The *Virgil Card* identifies the user/device by one of his types.
|
101
|
+
|
102
|
+
Collect an *app_id* and *app_key* for your app. These parametes are required to create a Virgil Card in your app scope.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
app_id = "[YOUR_APP_ID_HERE]"
|
106
|
+
app_key_password = "[YOUR_APP_KEY_PASSWORD_HERE]"
|
107
|
+
app_key_data = Virgil::Crypto::Bytes.from_string(File.read("[YOUR_APP_KEY_PATH_HERE]"))
|
108
|
+
|
109
|
+
app_key = crypto.import_private_key(app_key_data, app_key_password)
|
110
|
+
```
|
111
|
+
Generate a new Public/Private keypair using *VirgilCrypto* class.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
alice_keys = crypto.generate_keys()
|
115
|
+
```
|
116
|
+
|
117
|
+
Prepare request
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
exported_public_key = crypto.export_public_key(alice_keys.public_key)
|
121
|
+
create_card_request = Virgil::SDK::Client::Requests::CreateCardRequest.new(
|
122
|
+
identity: "alice",
|
123
|
+
identity_type: "username",
|
124
|
+
public_key: exported_public_key
|
125
|
+
)
|
126
|
+
```
|
127
|
+
|
128
|
+
then, use *RequestSigner* class to sign request with owner and app keys.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
request_signer = Virgil::SDK::Client::RequestSigner.new(crypto)
|
132
|
+
|
133
|
+
request_signer.self_sign(create_card_request, alice_keys.private_key)
|
134
|
+
requestSigner.authority_sign(create_card_request, app_id, app_key)
|
135
|
+
```
|
136
|
+
Publish a Virgil Card
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
alice_card = client.create_card_from_signed_request(create_card_request)
|
140
|
+
```
|
141
|
+
|
142
|
+
Or you can use the shorthand versions
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
alice_keys = crypto.generate_keys
|
146
|
+
|
147
|
+
alice_card = virgil_client.create_card(
|
148
|
+
"alice",
|
149
|
+
"username",
|
150
|
+
alice_keys,
|
151
|
+
app_id,
|
152
|
+
app_key
|
153
|
+
)
|
154
|
+
```
|
155
|
+
|
156
|
+
this will sign and send the card creation request.
|
157
|
+
|
158
|
+
## Search for Virgil Cards
|
159
|
+
|
160
|
+
Performs the `Virgil Card`s search by criteria:
|
161
|
+
- the *identities* request parameter is mandatory;
|
162
|
+
- the *identity_type* is optional and specifies the *IdentityType* of a `Virgil Card`s to be found;
|
163
|
+
- the *scope* optional request parameter specifies the scope to perform search on. Either 'global' or 'application'. The default value is 'application';
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
client = Virgil::SDK::Client::VirgilClient.new("[YOUR_ACCESS_TOKEN_HERE]")
|
167
|
+
|
168
|
+
criteria = Virgil::SDK::Client::SearchCriteria.by_identities("alice", "bob")
|
169
|
+
cards = client.search_cards_by_criteria(criteria)
|
170
|
+
```
|
171
|
+
|
172
|
+
Or you can use the shorthand versions
|
173
|
+
```ruby
|
174
|
+
client = Virgil::SDK::Client::VirgilClient.new("[YOUR_ACCESS_TOKEN_HERE]")
|
175
|
+
|
176
|
+
cards = client.search_cards_by_identities("alice", "bob")
|
177
|
+
app_bundle_cards = client.seach_cards_by_app_bundle("[APP_BUNDLE]")
|
178
|
+
```
|
179
|
+
|
180
|
+
## Getting a Virgil Card
|
181
|
+
|
182
|
+
Gets a `Virgil Card` by ID.
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
client = Virgil::SDK::Client::VirgilClient.new("[YOUR_ACCESS_TOKEN_HERE]")
|
186
|
+
card = client.get_card("[YOUR_CARD_ID_HERE]")
|
187
|
+
```
|
188
|
+
|
189
|
+
## Validating Virgil Cards
|
190
|
+
|
191
|
+
This sample uses *built-in* ```CardValidator``` to validate cards. By default ```CardValidator``` validates only *Cards Service* signature.
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
# Initialize crypto API
|
195
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
196
|
+
|
197
|
+
validator = Virgil::SDK::Client::CardValidator.new(crypto)
|
198
|
+
|
199
|
+
# Your can also add another Public Key for verification.
|
200
|
+
# validator.add_verifier("[HERE_VERIFIER_CARD_ID]", [HERE_VERIFIER_PUBLIC_KEY]);
|
201
|
+
|
202
|
+
# Initialize service client
|
203
|
+
client = Virgil::SDK::Client::VirgilClient.new("[YOUR_ACCESS_TOKEN_HERE]")
|
204
|
+
client.set_card_validator(validator)
|
205
|
+
|
206
|
+
begin
|
207
|
+
cards = client.search_cards_by_identities("alice", "bob");
|
208
|
+
rescue Virgil::SDK::Client::InvalidCardException => ex
|
209
|
+
# ex.invalid_cards is the list of Card objects that didn't pass validation
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
## Revoking a Virgil Card
|
214
|
+
|
215
|
+
Initialize required components.
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
client = Virgil::SDK::Client::VirgilClient.new("[YOUR_ACCESS_TOKEN_HERE]")
|
219
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
220
|
+
request_signer = Virgil::SDK::Client::RequestSigner.new(crypto)
|
221
|
+
```
|
222
|
+
|
223
|
+
Collect *App* credentials
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
app_id = "[YOUR_APP_ID_HERE]"
|
227
|
+
app_key_password = "[YOUR_APP_KEY_PASSWORD_HERE]"
|
228
|
+
app_key_path = "[YOUR_APP_KEY_PATH_HERE]"
|
229
|
+
app_key_data = Virgil::Crypto::Bytes.from_string(File.read(app_key_path))
|
230
|
+
|
231
|
+
app_key = crypto.import_private_key(app_key_data, app_key_password)
|
232
|
+
```
|
233
|
+
|
234
|
+
Prepare revocation request
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
card_id = "[YOUR_CARD_ID_HERE]"
|
238
|
+
|
239
|
+
revoke_request = Virgil::SDK::Client::Requests::RevokeCardRequest(
|
240
|
+
card_id, Virgil::SDK::Client::Requests::RevokeCardRequest::Reasons::Unspecified
|
241
|
+
)
|
242
|
+
request_signer.authority_sign(revoke_request, app_id, app_key)
|
243
|
+
|
244
|
+
client.revoke_card_from_signed_request(revoke_request)
|
245
|
+
```
|
246
|
+
|
247
|
+
The shorthand version is
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
client.revoke_card(
|
251
|
+
"[YOUR_CARD_ID_HERE]",
|
252
|
+
Virgil::SDK::Client::Requests::RevokeCardRequest::Reasons::Unspecified,
|
253
|
+
app_id,
|
254
|
+
app_key
|
255
|
+
)
|
256
|
+
```
|
257
|
+
## Adding card relation
|
258
|
+
Create request
|
259
|
+
```ruby
|
260
|
+
add_relation_request = Virgil::SDK::Client::Requests::AddRelationRequest.new(
|
261
|
+
trusted_card
|
262
|
+
)
|
263
|
+
```
|
264
|
+
sign request with trustor card
|
265
|
+
```ruby
|
266
|
+
request_signer = Virgil::SDK::Client::RequestSigner.new(crypto)
|
267
|
+
|
268
|
+
request_signer.authority_sign(add_relation_request, alice_card.id, alice_key.private_key)
|
269
|
+
|
270
|
+
```
|
271
|
+
|
272
|
+
publish request
|
273
|
+
```ruby
|
274
|
+
client.add_relation(add_relation_request)
|
275
|
+
```
|
276
|
+
|
277
|
+
## Deleting card relation
|
278
|
+
Create request
|
279
|
+
```ruby
|
280
|
+
delete_relation_request = Virgil::SDK::Client::Requests::DeleteRelationRequest.new(
|
281
|
+
trusted_card.id
|
282
|
+
)
|
283
|
+
```
|
284
|
+
sign request with trustor card
|
285
|
+
```ruby
|
286
|
+
request_signer = Virgil::SDK::Client::RequestSigner.new(crypto)
|
287
|
+
|
288
|
+
request_signer.authority_sign(delete_relation_request, alice_card.id, alice_key.private_key)
|
289
|
+
|
290
|
+
```
|
291
|
+
|
292
|
+
publish request
|
293
|
+
```ruby
|
294
|
+
client.delete_relation(delete_relation_request)
|
295
|
+
```
|
296
|
+
|
297
|
+
## Operations with Crypto Keys
|
298
|
+
|
299
|
+
### Generate Keys
|
300
|
+
The following code sample illustrates keypair generation. The default algorithm is ed25519
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
alice_keys = crypto.generate_keys
|
304
|
+
```
|
305
|
+
|
306
|
+
### Import and Export Keys
|
307
|
+
You can export and import your Public/Private keys to/from supported wire representation.
|
308
|
+
|
309
|
+
To export Public/Private keys, simply call one of the Export methods:
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
exported_private_key = crypto.export_private_key(alice_keys.private_key)
|
313
|
+
exported_public_key = crypto.export_public_key(alice_keys.public_key)
|
314
|
+
```
|
315
|
+
|
316
|
+
To import Public/Private keys, simply call one of the Import methods:
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
private_key = crypto.import_private_key(exported_private_key)
|
320
|
+
public_key = crypto.import_public_key(exported_public_key)
|
321
|
+
```
|
322
|
+
|
323
|
+
## Encryption and Decryption
|
324
|
+
|
325
|
+
Initialize Crypto API and generate keypair.
|
326
|
+
|
327
|
+
```ruby
|
328
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
329
|
+
alice_keys = crypto.generate_keys
|
330
|
+
```
|
331
|
+
|
332
|
+
### Encrypt Data
|
333
|
+
|
334
|
+
Data encryption using ECIES scheme with AES-GCM.
|
335
|
+
There can be more than one recipient.
|
336
|
+
|
337
|
+
```ruby
|
338
|
+
plain_data = Virgil::Crypto::Bytes.from_string("Hello Bob!")
|
339
|
+
cipher_data = crypto.encrypt(plain_data, alice_keys.public_key)
|
340
|
+
```
|
341
|
+
|
342
|
+
### Decrypt Data
|
343
|
+
|
344
|
+
You can decrypt data using your private key
|
345
|
+
|
346
|
+
```ruby
|
347
|
+
crypto.decrypt(cipher_data, alice_keys.private_key);
|
348
|
+
```
|
349
|
+
|
350
|
+
## Generating and Verifying Signatures
|
351
|
+
|
352
|
+
This section walks you through the steps necessary to use the
|
353
|
+
*VirgilCrypto* to generate a digital signature for data
|
354
|
+
and to verify that a signature is authentic.
|
355
|
+
|
356
|
+
Generate a new Public/Private keypair and *data* to be signed.
|
357
|
+
|
358
|
+
```ruby
|
359
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
360
|
+
alice_keys = crypto.generate_keys()
|
361
|
+
|
362
|
+
# The data to be signed with alice's Private key
|
363
|
+
data = Virgil::Crypto::Bytes.from_string("Hello Bob, How are you?")
|
364
|
+
```
|
365
|
+
|
366
|
+
### Generating a Signature
|
367
|
+
|
368
|
+
Sign the SHA-384 fingerprint of bytes using your private key.
|
369
|
+
To generate the signature, simply call the sign method:
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
signature = crypto.sign(data, alice.private_key)
|
373
|
+
```
|
374
|
+
|
375
|
+
### Verifying a Signature
|
376
|
+
|
377
|
+
Verify the signature of the SHA-384 fingerprint of bytes using Public key.
|
378
|
+
The signature can now be verified by calling the verify method:
|
379
|
+
|
380
|
+
```ruby
|
381
|
+
is_valid = crypto.verify(data, signature, alice.public_key)
|
382
|
+
```
|
383
|
+
|
384
|
+
## Authenticated Encryption
|
385
|
+
Authenticated Encryption provides both data confidentiality and data
|
386
|
+
integrity assurances to the information being protected.
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
390
|
+
|
391
|
+
alice = crypto.generate_keys
|
392
|
+
bob = crypto.generate_keys
|
393
|
+
|
394
|
+
# The data to be signed with alice's Private key
|
395
|
+
data = Virgil::Crypto::Bytes.from_string("Hello Bob, How are you?")
|
396
|
+
```
|
397
|
+
|
398
|
+
### Sign then Encrypt
|
399
|
+
|
400
|
+
```ruby
|
401
|
+
cipher_data = crypto.sign_then_encrypt(
|
402
|
+
data,
|
403
|
+
alice.private_key,
|
404
|
+
bob.public_key
|
405
|
+
)
|
406
|
+
```
|
407
|
+
|
408
|
+
### Decrypt then Verify
|
409
|
+
```ruby
|
410
|
+
decrypted_data = crypto.decrypt_then_verify(
|
411
|
+
cipher_data,
|
412
|
+
bob.private_key,
|
413
|
+
alice.public_key
|
414
|
+
)
|
415
|
+
```
|
416
|
+
|
417
|
+
## Fingerprint Generation
|
418
|
+
|
419
|
+
The default Fingerprint algorithm is SHA-256.
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
crypto = Virgil::SDK::Cryptography::VirgilCrypto.new
|
423
|
+
fingerprint = crypto.calculate_fingerprint(content_bytes)
|
424
|
+
```
|
425
|
+
|
426
|
+
## Release Notes
|
427
|
+
- Please read the latest note here: [https://github.com/VirgilSecurity/virgil-sdk-ruby/releases](https://github.com/VirgilSecurity/virgil-sdk-ruby/releases)
|