selfsdk 0.0.140 → 0.0.145
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/crypto.rb +27 -13
- data/lib/messages/authentication_req.rb +3 -4
- data/lib/messages/base.rb +25 -5
- data/lib/messages/fact_request.rb +2 -8
- data/lib/messages/message.rb +1 -1
- data/lib/messaging.rb +8 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c59c2a25ba8f895e9a1820e6cf8072c219680ccfc763582f9b8761903ae0f3f
|
4
|
+
data.tar.gz: 9ca32b859e6f0feeddf5c5e87b38309e691f7459e60d3a9b281f1396dc4bc998
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65b73466d77d25bfa80dfa9c191f19bb6d525c739754753e653652ebbb59eecb3b3c8936d3205c8b41dd7a71376bef6aa18367fb8f275fe60ef18888633e0a8d
|
7
|
+
data.tar.gz: c39e65926ddb0923673b5e1109ae54dcc01adfa2408a560182acd8cc3160adb8f7085e9a0f22545078108f2efc1f9d432363255086400833ad5bdd5947091867
|
data/lib/crypto.rb
CHANGED
@@ -8,9 +8,9 @@ module SelfSDK
|
|
8
8
|
@storage_key = storage_key
|
9
9
|
@storage_folder = storage_folder
|
10
10
|
|
11
|
-
if File.exist?(
|
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(
|
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(
|
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 =
|
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
|
@@ -55,9 +55,6 @@ module SelfSDK
|
|
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 =
|
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
|
@@ -38,13 +38,12 @@ module SelfSDK
|
|
38
38
|
|
39
39
|
protected
|
40
40
|
|
41
|
-
def proto
|
42
|
-
@to_device = @client.devices(@to).first
|
41
|
+
def proto(to_device)
|
43
42
|
Msgproto::Message.new(type: Msgproto::MsgType::MSG,
|
44
43
|
sender: "#{@jwt.id}:#{@messaging.device_id}",
|
45
44
|
id: @id,
|
46
|
-
recipient: "#{@to}:#{
|
47
|
-
ciphertext: encrypt_message(@jwt.prepare(body), @to,
|
45
|
+
recipient: "#{@to}:#{to_device}",
|
46
|
+
ciphertext: encrypt_message(@jwt.prepare(body), @to, to_device))
|
48
47
|
end
|
49
48
|
|
50
49
|
end
|
data/lib/messages/base.rb
CHANGED
@@ -15,15 +15,24 @@ module SelfSDK
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def request
|
18
|
-
|
19
|
-
|
18
|
+
check_credits!
|
19
|
+
msgs = []
|
20
|
+
devices.each do |d|
|
21
|
+
msgs << proto(d)
|
22
|
+
end
|
23
|
+
res = @messaging.send_and_wait_for_response(msgs, self)
|
24
|
+
SelfSDK.logger.info "synchronously messaging to #{@to}:#{@d}"
|
20
25
|
res
|
21
26
|
end
|
22
27
|
|
23
28
|
def send_message
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
check_credits!
|
30
|
+
res = []
|
31
|
+
devices.each do |d|
|
32
|
+
res << @messaging.send_message(proto(d))
|
33
|
+
SelfSDK.logger.info "asynchronously requested information to #{@to}:#{@d}"
|
34
|
+
end
|
35
|
+
res.first
|
27
36
|
end
|
28
37
|
|
29
38
|
def encrypt_message(message, recipient, recipient_device)
|
@@ -65,6 +74,17 @@ module SelfSDK
|
|
65
74
|
raise ::StandardError.new("must define this method")
|
66
75
|
end
|
67
76
|
|
77
|
+
def devices
|
78
|
+
return @client.devices(@to) if @intermediary.nil?
|
79
|
+
|
80
|
+
@client.devices(@intermediary)
|
81
|
+
end
|
82
|
+
|
83
|
+
def check_credits!
|
84
|
+
app = @client.app(@jwt.id)
|
85
|
+
raise "Your credits have expired, please log in to the developer portal and top up your account." if app[:paid_actions] == false
|
86
|
+
end
|
87
|
+
|
68
88
|
private
|
69
89
|
|
70
90
|
def get_payload(input)
|
@@ -85,14 +85,8 @@ module SelfSDK
|
|
85
85
|
|
86
86
|
protected
|
87
87
|
|
88
|
-
def proto
|
89
|
-
|
90
|
-
@client.devices(@to)
|
91
|
-
else
|
92
|
-
@client.devices(@intermediary)
|
93
|
-
end
|
94
|
-
@to_device = devices.first
|
95
|
-
|
88
|
+
def proto(to_device)
|
89
|
+
@to_device = to_device
|
96
90
|
if @intermediary.nil?
|
97
91
|
recipient = "#{@to}:#{@to_device}"
|
98
92
|
ciphertext = encrypt_message(@jwt.prepare(body), @to, @to_device)
|
data/lib/messages/message.rb
CHANGED
data/lib/messaging.rb
CHANGED
@@ -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,
|
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,
|
52
|
+
@encryption_client = Crypto.new(@client, @device_id, @storage_dir, storage_key)
|
53
53
|
end
|
54
54
|
|
55
55
|
if options.include? :ws
|
@@ -94,6 +94,7 @@ module SelfSDK
|
|
94
94
|
# @param type [string] message type
|
95
95
|
# @param request [hash] original message requesing information
|
96
96
|
def send_custom(recipient, request_body)
|
97
|
+
# TODO (adriacidre) this is sending the message to the first device only
|
97
98
|
@to_device = @client.devices(recipient).first
|
98
99
|
send_message msg = Msgproto::Message.new(
|
99
100
|
type: Msgproto::MsgType::MSG,
|
@@ -142,9 +143,11 @@ module SelfSDK
|
|
142
143
|
# Sends a message and waits for the response
|
143
144
|
#
|
144
145
|
# @params msg [Msgproto::Message] message object to be sent
|
145
|
-
def send_and_wait_for_response(
|
146
|
-
wait_for msg.id, original do
|
147
|
-
|
146
|
+
def send_and_wait_for_response(msgs, original)
|
147
|
+
wait_for msg.first.id, original do
|
148
|
+
msgs.each do |msg|
|
149
|
+
send_message msg
|
150
|
+
end
|
148
151
|
end
|
149
152
|
end
|
150
153
|
|