mailcycle 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00ac2000980d75a7daff3d987843ec8a7f55f8901972343322a96c73180834b4
4
- data.tar.gz: c1f1d87eca6363dbd6d304f7fceef45ddc82ba6cb0ae0dadab8498dc84c1b8d6
3
+ metadata.gz: af0a5621cd2d8fd6f9c31bbf137463ebde367f530c06b5b285fbc81190b13660
4
+ data.tar.gz: 9da1a431696d248b521ed612c635843d62d257586ea44df19198617c17b15670
5
5
  SHA512:
6
- metadata.gz: d9794b89fd4d79c0b6db76614fc3483862a933f792b198d5b748cd7fe203515f4c6c1170d07a71480db73102cedf42632947ff8ba6cbaab03e525696ee6ecd0a
7
- data.tar.gz: b29ab90068d15725085ab6eb2034d53eb59ec500acd5647e93f4f5636125ba17f288fbfa1a5fb1e7d7c4e2473e141849923d1329695b8ee1d85d85550abf7dd9
6
+ metadata.gz: 18f3215df579c6628e68b5a8f8a4ac0a78a8533c6999bdf4cc603da75ba50f0daedc4adc5f09ff2506f2199a23b0e1df8f410d991ad912f9c76a87b42e138852
7
+ data.tar.gz: 158998c0cf0ce99ba62ac67b08edf8a22089d610f738d71695db23646a30d190cd97cd9f9f52fd4dcd4d633be77a60998e392d0dc4df0293855b70bcf3d839d3
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  A Ruby client for the Mailcycle API that does the encryption on your machine. Create addresses that receive mail, read what arrives, send, and listen for events. Mailcycle stores mail sealed to keys derived from your recovery phrase, and this gem derives them locally; the phrase never leaves your machine.
4
4
 
5
+ Home page and examples: https://mailcycle.email/sdks/ruby/
6
+
7
+ Guide: https://docs.mailcycle.email/guides/sdk-ruby/
8
+
5
9
  Needs Ruby 3.1 or later. It has no dependencies beyond Ruby's own standard library.
6
10
 
7
11
  ```sh
@@ -36,6 +40,21 @@ puts address.email_address
36
40
 
37
41
  The address's public key is derived from the phrase and sent with it, so the mail server can seal what arrives. The label is sealed on your machine, and the server stores it as ciphertext.
38
42
 
43
+ ## Export and rotate an address key
44
+
45
+ ```ruby
46
+ exported = mc.addresses.export_key(address.id) # the current key epoch
47
+ old = mc.addresses.export_key(address.id, 0) # or any epoch from 0 to the current one
48
+ puts exported.private_key # base64url, 32 bytes
49
+ puts exported.public_key
50
+
51
+ mc.addresses.rotate_key(address.id) # => #<struct AddressRotation address_id=..., epoch=1>
52
+ ```
53
+
54
+ `export_key` returns one address's private key, to give a helper that reads that one address's mail, such as the [decrypt Worker](https://docs.mailcycle.email/guides/decrypt-worker/) for n8n, Make, Zapier and Pipedream. It opens that address's mail and nothing else, so treat it like the mail itself. The export is recorded in the account's history before the key is derived, and only the address id is sent. An epoch outside 0 to the current one raises `invalid_epoch`.
55
+
56
+ `rotate_key` moves the address to its next key epoch, so an exported key stops opening new mail, and hands the new key to the device or agent that holds the address. An address shared with team members raises `team_address`: rotate it in the web app, which also gives each member the new key. Both need the phrase and a signed-in session.
57
+
39
58
  ## Reading mail
40
59
 
41
60
  ```ruby
@@ -418,6 +418,58 @@ module Mailcycle
418
418
  nil
419
419
  end
420
420
 
421
+ # One address's private key at a key epoch (the current one unless `epoch`
422
+ # is given), base64url. It opens that address's mail and nothing else: it
423
+ # cannot sign in, create keys or send. Treat it as the address's mail itself.
424
+ #
425
+ # Needs the phrase and a signed-in session. The export is recorded in the
426
+ # account's history before the key is derived; only the address id is
427
+ # sent. The key is derived on this machine.
428
+ def export_key(address_id, epoch = nil)
429
+ keys = @client.require_keys("export an address key")
430
+ inbox = find_inbox(address_id)
431
+ current = Mail.key_epoch(inbox)
432
+ at = epoch.nil? ? current : epoch
433
+ unless at.is_a?(Integer) && at.between?(0, current)
434
+ raise ApiError.new(400, "invalid_epoch", "That address has key epochs 0 to #{current}.")
435
+ end
436
+
437
+ @client.http.request("POST", "/inboxes/#{Mailcycle.path_segment(address_id)}/key-exported", body: {})
438
+ raw = keys.address_private_key(address_id, epoch: at)
439
+ begin
440
+ AddressKeyExport.new(address_id: address_id, epoch: at, private_key: Encoding.to_b64u(raw),
441
+ public_key: keys.address_public_key(address_id, epoch: at))
442
+ ensure
443
+ raw.replace("\0".b * raw.bytesize) unless raw.frozen?
444
+ end
445
+ end
446
+
447
+ # Moves an address to its next key epoch: mail from now on is sealed to a
448
+ # new key, so an exported key stops opening new mail. A device or agent
449
+ # holding the address is handed the new key. Needs the phrase and a
450
+ # signed-in session. An address shared with team members is refused with a
451
+ # 409 `team_address`: rotate it in the web app, which also gives each
452
+ # member the new key.
453
+ def rotate_key(address_id)
454
+ keys = @client.require_keys("give an address new keys")
455
+ inbox = find_inbox(address_id)
456
+ ensure_not_shared(address_id)
457
+ epoch = Mail.key_epoch(inbox) + 1
458
+ @client.http.request("POST", "/inboxes/#{Mailcycle.path_segment(address_id)}/rotate",
459
+ body: { "epoch" => epoch, "publicKey" => keys.address_public_key(address_id, epoch: epoch),
460
+ "grants" => [] })
461
+ holder = inbox["assignedWorkerId"]
462
+ if holder.is_a?(String) && !holder.empty?
463
+ begin
464
+ # A holder reads the new epoch once it has the new key; one that cannot be reached is left as it was.
465
+ @client.devices.hand_over_keys(holder)
466
+ rescue Error
467
+ nil
468
+ end
469
+ end
470
+ AddressRotation.new(address_id: address_id, epoch: epoch)
471
+ end
472
+
421
473
  # How many addresses the plan allows and how many are left.
422
474
  def allocation
423
475
  @client.http.request("GET", "/inboxes/allocation")
@@ -425,6 +477,29 @@ module Mailcycle
425
477
 
426
478
  private
427
479
 
480
+ def find_inbox(address_id)
481
+ list = Wire.array(Wire.object(@client.http.request("GET", "/inboxes"))["inboxes"])
482
+ found = list.find { |item| item.is_a?(Hash) && item["id"] == address_id }
483
+ raise ApiError.new(404, "not_found", "That address is not on this account.") if found.nil?
484
+
485
+ found
486
+ end
487
+
488
+ def ensure_not_shared(address_id)
489
+ members = Wire.array(Wire.object(@client.http.request("GET", "/team"))["members"])
490
+ shared = members.any? do |member|
491
+ Wire.array(Wire.object(member)["grants"]).any? { |grant| Wire.object(grant)["inboxId"] == address_id }
492
+ end
493
+ return unless shared
494
+
495
+ raise ApiError.new(409, "team_address",
496
+ "This address is shared with team members. Give it new keys in the web app, " \
497
+ "which also gives them the new key.")
498
+ rescue ApiError => e
499
+ # A plan without Teams has no members to keep.
500
+ raise unless e.status == 402
501
+ end
502
+
428
503
  def to_address(wire)
429
504
  wire = Wire.object(wire)
430
505
  id = Wire.text(wire["id"])
@@ -61,6 +61,23 @@ module Mailcycle
61
61
  Address = Struct.new(:id, :email_address, :status, :created_at, :retention_days, :assigned_worker_id,
62
62
  :paused_at, :label, :sender_name, :receives, keyword_init: true)
63
63
 
64
+ # An address's private key at one key epoch, as `addresses.export_key` returns it.
65
+ # `private_key` and `public_key` are base64url, 32 bytes each. The private key
66
+ # opens that address's mail and attachments and nothing else; treat it as the mail itself.
67
+ AddressKeyExport = Struct.new(:address_id, :epoch, :private_key, :public_key, keyword_init: true) do
68
+ def inspect
69
+ "#<struct Mailcycle::AddressKeyExport address_id=#{address_id.inspect} epoch=#{epoch}>"
70
+ end
71
+ alias_method :to_s, :inspect
72
+
73
+ def pretty_print(printer)
74
+ printer.text(inspect)
75
+ end
76
+ end
77
+
78
+ # The result of `addresses.rotate_key`: the address and the epoch it moved to.
79
+ AddressRotation = Struct.new(:address_id, :epoch, keyword_init: true)
80
+
64
81
  # `reserved` is true for a domain that belongs to this account alone.
65
82
  Domain = Struct.new(:domain, :reserved, :extra, keyword_init: true) do
66
83
  def self.from_wire(wire)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mailcycle
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -223,8 +223,8 @@ module Mailcycle
223
223
 
224
224
  first = @buffer.getbyte(0)
225
225
  second = @buffer.getbyte(1)
226
- raise Broken, "the server masked a frame" if second & 0x80 != 0
227
- raise Broken, "a frame used reserved bits" if first & 0x70 != 0
226
+ raise Broken, "the server masked a frame" if second.anybits?(0x80)
227
+ raise Broken, "a frame used reserved bits" if first.anybits?(0x70)
228
228
 
229
229
  length = second & 0x7f
230
230
  offset = 2
@@ -244,7 +244,7 @@ module Mailcycle
244
244
 
245
245
  payload = @buffer.byteslice(offset, length)
246
246
  @buffer = @buffer.byteslice(offset + length, @buffer.bytesize - offset - length)
247
- [first & 0x80 != 0, first & 0x0f, payload]
247
+ [first.anybits?(0x80), first & 0x0f, payload]
248
248
  end
249
249
 
250
250
  # A Frame for a whole message or a close, nil for anything handled here.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailcycle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Northlab Studios Ltd
@@ -23,8 +23,8 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '3.0'
26
- description: Create addresses that receive mail, read and decrypt it on your machine,
27
- send, and listen for events.
26
+ description: Create addresses that receive email, read and decrypt messages on your
27
+ machine, send email, verify webhooks and stream events.
28
28
  email:
29
29
  - hello@mailcycle.email
30
30
  executables: []
@@ -56,12 +56,12 @@ files:
56
56
  - lib/mailcycle/version.rb
57
57
  - lib/mailcycle/webhook_signature.rb
58
58
  - lib/mailcycle/websocket.rb
59
- homepage: https://mailcycle.email/
59
+ homepage: https://mailcycle.email/sdks/ruby/
60
60
  licenses:
61
61
  - Apache-2.0
62
62
  metadata:
63
- homepage_uri: https://mailcycle.email/
64
- documentation_uri: https://docs.mailcycle.email/
63
+ homepage_uri: https://mailcycle.email/sdks/ruby/
64
+ documentation_uri: https://docs.mailcycle.email/guides/sdk-ruby/
65
65
  rubygems_mfa_required: 'true'
66
66
  rdoc_options: []
67
67
  require_paths:
@@ -79,5 +79,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements: []
80
80
  rubygems_version: 4.0.20
81
81
  specification_version: 4
82
- summary: Ruby client for the Mailcycle API.
82
+ summary: 'Ruby email API client: receive, read and send email, verify webhooks.'
83
83
  test_files: []