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 +4 -4
- data/lib/client.rb +29 -8
- data/lib/crypto.rb +29 -15
- data/lib/messages/message.rb +1 -1
- data/lib/messaging.rb +2 -2
- data/lib/services/auth.rb +5 -2
- data/lib/services/facts.rb +5 -2
- 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: b831656c278b883faa5a7831e39965964e87a7b2739257954ac29154d0374923
|
4
|
+
data.tar.gz: ed0231cd3e9fdd9525fa08a4119a3aee26e3146d2ed65c18b09c540c542ebce4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 745da1e65693ecb6e3e4946d799104cab2365e54af7faf3dfa1c0996825453e98507c4e536bc3c2690a67601e2bc565590ca71529ba53999fe6340cb48e79b8c
|
7
|
+
data.tar.gz: 0225bdfb29c5efbeb1501b7dcf144cbd09e87ab81d68c8b44a8ec5daa8cf4f08a2f24b7c1b59c356dde50cb696d1be17b02eb605ee5080ad033c6f06881c0aaf
|
data/lib/client.rb
CHANGED
@@ -66,19 +66,31 @@ module SelfSDK
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def post(endpoint, body)
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
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)
|
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
|
@@ -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.
|
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 =
|
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
|
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
|
data/lib/services/auth.rb
CHANGED
@@ -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
|
-
|
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
|
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.
|
data/lib/services/facts.rb
CHANGED
@@ -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
|
-
|
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
|
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?
|