selfsdk 0.0.137 → 0.0.142

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4b5f44ffce4a1ad4c61c20b752e97173f2cc0c96091138683ddb56c2dca2f73
4
- data.tar.gz: 841ad78f32bcb7434126eac7bdea3e3eb9d7abf4cc35fd41d0d243fa7f05fffa
3
+ metadata.gz: b831656c278b883faa5a7831e39965964e87a7b2739257954ac29154d0374923
4
+ data.tar.gz: ed0231cd3e9fdd9525fa08a4119a3aee26e3146d2ed65c18b09c540c542ebce4
5
5
  SHA512:
6
- metadata.gz: 04f00c82b5f64dc8f23597e28fc11eee64db02deacfbeedb328d3f766048e40d3a625c3ae25b7a5a888969272883650e63ce9425d5056f2ed98f5beae1712987
7
- data.tar.gz: 2dfa4e9f4e3796393adfda66d994ef39fa2de8f37b08b2274b4fcdaa1ccd752e7aafdec5f7eb80b1b4f336ecaeb74350a42464c785c5026eb82e8ffd3458761f
6
+ metadata.gz: 745da1e65693ecb6e3e4946d799104cab2365e54af7faf3dfa1c0996825453e98507c4e536bc3c2690a67601e2bc565590ca71529ba53999fe6340cb48e79b8c
7
+ data.tar.gz: 0225bdfb29c5efbeb1501b7dcf144cbd09e87ab81d68c8b44a8ec5daa8cf4f08a2f24b7c1b59c356dde50cb696d1be17b02eb605ee5080ad033c6f06881c0aaf
@@ -66,19 +66,31 @@ module SelfSDK
66
66
  end
67
67
 
68
68
  def post(endpoint, body)
69
- p HTTParty.post("#{@self_url}#{endpoint}",
70
- headers: {
71
- 'Content-Type' => 'application/json',
72
- 'Authorization' => "Bearer #{@jwt.auth_token}"
73
- },
74
- body: body)
69
+ res = nil
70
+ loop do
71
+ res = HTTParty.post("#{@self_url}#{endpoint}",
72
+ headers: {
73
+ 'Content-Type' => 'application/json',
74
+ 'Authorization' => "Bearer #{@jwt.auth_token}"
75
+ },
76
+ body: body)
77
+ break if res.code != 503
78
+ sleep 2
79
+ end
80
+ return res
75
81
  end
76
82
 
77
83
  def get(endpoint)
78
- HTTParty.get("#{@self_url}#{endpoint}", headers: {
84
+ res = nil
85
+ loop do
86
+ res = HTTParty.get("#{@self_url}#{endpoint}", headers: {
79
87
  'Content-Type' => 'application/json',
80
88
  'Authorization' => "Bearer #{@jwt.auth_token}"
81
- })
89
+ })
90
+ break if res.code != 503
91
+ sleep 2
92
+ end
93
+ return res
82
94
  end
83
95
 
84
96
  # Lists all public keys stored on self for the given ID
@@ -90,6 +102,15 @@ module SelfSDK
90
102
  sg.key_by_id(kid)
91
103
  end
92
104
 
105
+ # Get the active public key for a device
106
+ #
107
+ # @param id [string] identity id
108
+ def device_public_key(id, did)
109
+ i = entity(id)
110
+ sg = SelfSDK::SignatureGraph.new(i[:history])
111
+ sg.key_by_device(did)
112
+ end
113
+
93
114
  private
94
115
 
95
116
  def get_identity(endpoint)
@@ -8,9 +8,9 @@ module SelfSDK
8
8
  @storage_key = storage_key
9
9
  @storage_folder = storage_folder
10
10
 
11
- if File.exist?('account.pickle')
11
+ if File.exist?(account_path)
12
12
  # 1a) if alice's account file exists load the pickle from the file
13
- @account = SelfCrypto::Account.from_pickle(File.read('account.pickle'), @storage_key)
13
+ @account = SelfCrypto::Account.from_pickle(File.read(account_path), @storage_key)
14
14
  else
15
15
  # 1b-i) if create a new account for alice if one doesn't exist already
16
16
  @account = SelfCrypto::Account.from_seed(@client.jwt.key)
@@ -25,12 +25,12 @@ module SelfSDK
25
25
  @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)
26
26
 
27
27
  # 1b-v) store the account to a file
28
- File.write('account.pickle', @account.to_pickle(storage_key))
28
+ File.write(account_path, @account.to_pickle(storage_key))
29
29
  end
30
30
  end
31
31
 
32
32
  def encrypt(message, recipient, recipient_device)
33
- session_file_name = "#{recipient}:#{recipient_device}-session.pickle"
33
+ session_file_name = session_path(recipient, recipient_device)
34
34
 
35
35
  if File.exist?(session_file_name)
36
36
  # 2a) if bob's session file exists load the pickle from the file
@@ -38,7 +38,7 @@ module SelfSDK
38
38
  else
39
39
  # 2b-i) if you have not previously sent or recevied a message to/from bob,
40
40
  # you must get his identity key from GET /v1/identities/bob/
41
- ed25519_identity_key = @client.public_keys(recipient).first[:key]
41
+ ed25519_identity_key = @client.device_public_key(recipient, recipient_device)
42
42
 
43
43
  # 2b-ii) get a one time key for bob
44
44
  res = @client.get("/v1/identities/#{recipient}/devices/#{recipient_device}/pre_keys")
@@ -51,13 +51,10 @@ module SelfSDK
51
51
  one_time_key = JSON.parse(res.body)["key"]
52
52
 
53
53
  # 2b-iii) convert bobs ed25519 identity key to a curve25519 key
54
- curve25519_identity_key = SelfCrypto::Util.ed25519_pk_to_curve25519(ed25519_identity_key)
54
+ curve25519_identity_key = SelfCrypto::Util.ed25519_pk_to_curve25519(ed25519_identity_key.raw_public_key)
55
55
 
56
56
  # 2b-iv) create the session with bob
57
57
  session_with_bob = @account.outbound_session(curve25519_identity_key, one_time_key)
58
-
59
- # 2b-v) store the session to a file
60
- File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
61
58
  end
62
59
 
63
60
  # 3) create a group session and set the identity of the account youre using
@@ -67,11 +64,16 @@ module SelfSDK
67
64
  gs.add_participant("#{recipient}:#{recipient_device}", session_with_bob)
68
65
 
69
66
  # 5) encrypt a message
70
- gs.encrypt(message).to_s
67
+ ct = gs.encrypt(message).to_s
68
+
69
+ # 6) store the session to a file
70
+ File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
71
+
72
+ ct
71
73
  end
72
74
 
73
75
  def decrypt(message, sender, sender_device)
74
- session_file_name = "#{sender}:#{sender_device}-session.pickle"
76
+ session_file_name = session_path(sender, sender_device)
75
77
 
76
78
  if File.exist?(session_file_name)
77
79
  # 7a) if carol's session file exists load the pickle from the file
@@ -84,9 +86,6 @@ module SelfSDK
84
86
 
85
87
  # 7b-ii) use the initial message to create a session for bob or carol
86
88
  session_with_bob = @account.inbound_session(m)
87
-
88
- # 7b-iii) store the session to a file
89
- File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
90
89
  end
91
90
 
92
91
  # 8) create a group session and set the identity of the account you're using
@@ -96,7 +95,22 @@ module SelfSDK
96
95
  gs.add_participant("#{sender}:#{sender_device}", session_with_bob)
97
96
 
98
97
  # 10) decrypt the message ciphertext
99
- gs.decrypt("#{sender}:#{sender_device}", message).to_s
98
+ pt = gs.decrypt("#{sender}:#{sender_device}", message).to_s
99
+
100
+ # 11) store the session to a file
101
+ File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
102
+
103
+ pt
104
+ end
105
+
106
+ private
107
+
108
+ def account_path
109
+ "#{@storage_folder}/account.pickle"
110
+ end
111
+
112
+ def session_path(selfid, device)
113
+ "#{@storage_folder}/#{selfid}:#{device}-session.pickle"
100
114
  end
101
115
  end
102
116
  end
@@ -11,7 +11,7 @@ module SelfSDK
11
11
  body = if input.is_a? String
12
12
  input
13
13
  else
14
- issuer = input.recipient.split(":")
14
+ issuer = input.sender.split(":")
15
15
  messaging.encryption_client.decrypt(input.ciphertext, issuer.first, issuer.last)
16
16
  end
17
17
 
@@ -30,7 +30,7 @@ module SelfSDK
30
30
  # @params storage_folder [String] folder to perist messaging encryption
31
31
  # @option opts [Bool] :auto_reconnect Automatically reconnects to websocket if connection is lost (defaults to true).
32
32
  # @option opts [String] :device_id The device id to be used by the app defaults to "1".
33
- def initialize(url, client, storage_key, storage_folder, options = {})
33
+ def initialize(url, client, storage_key, options = {})
34
34
  @mon = Monitor.new
35
35
  @url = url
36
36
  @messages = {}
@@ -49,7 +49,7 @@ module SelfSDK
49
49
 
50
50
  FileUtils.mkdir_p @storage_dir unless File.exist? @storage_dir
51
51
  unless options.include? :no_crypto
52
- @encryption_client = Crypto.new(@client, @device_id, storage_folder, storage_key)
52
+ @encryption_client = Crypto.new(@client, @device_id, @storage_dir, storage_key)
53
53
  end
54
54
 
55
55
  if options.include? :ws
@@ -39,13 +39,16 @@ module SelfSDK
39
39
  # @return [String, String] conversation id or encoded body.
40
40
  def request(selfid, opts = {}, &block)
41
41
  SelfSDK.logger.info "authenticating #{selfid}"
42
- raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
42
+ rq = opts.fetch(:request, true)
43
+ if rq
44
+ raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
45
+ end
43
46
 
44
47
  req = SelfSDK::Messages::AuthenticationReq.new(@messaging)
45
48
  req.populate(selfid, opts)
46
49
 
47
50
  body = @client.jwt.prepare(req.body)
48
- return body unless opts.fetch(:request, true)
51
+ return body unless rq
49
52
  return req.send_message if opts.fetch(:async, false)
50
53
 
51
54
  # when a block is given the request will always be asynchronous.
@@ -41,13 +41,16 @@ module SelfSDK
41
41
  # @return [Object] SelfSDK:::Messages::FactRequest
42
42
  def request(selfid, facts, opts = {}, &block)
43
43
  SelfSDK.logger.info "authenticating #{selfid}"
44
- raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
44
+ rq = opts.fetch(:request, true)
45
+ if rq
46
+ raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
47
+ end
45
48
 
46
49
  req = SelfSDK::Messages::FactRequest.new(@messaging)
47
50
  req.populate(selfid, prepare_facts(facts), opts)
48
51
 
49
52
  body = @client.jwt.prepare(req.body)
50
- return body unless opts.fetch(:request, true)
53
+ return body unless rq
51
54
 
52
55
  # when a block is given the request will always be asynchronous.
53
56
  if block_given?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfsdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.137
4
+ version: 0.0.142
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aldgate Ventures