selfsdk 0.0.163 → 0.0.168

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: 5765cd4b687126291084eeeff9cf4e1662fb31c289b0d0f6bd88c3338207b83f
4
- data.tar.gz: 2e3b7da25fe3b1e7c6c8d5151fc5ea71b6d5078cf0356b3b04361c8219f8425d
3
+ metadata.gz: ffcdce1f12b74a382540ad69d30e73e9f4b89316d10831c07917d59646c24764
4
+ data.tar.gz: 56c549de3f1cc8574760ded98e5ec1c790293e3555e2b3d0ac69376a124c39e9
5
5
  SHA512:
6
- metadata.gz: 529f816374357065e3b6d168255aa2d02fba2567e92b90fc50dea8727d1d6c1aa5202a261e540c9e6f2d18d5c877f61982cb884f7bc5740eeb5d864d315a9330
7
- data.tar.gz: 9a73f8413912aff654a1d17aa6d17115c7b58a031430895f432fa912d5ee9d667df58ceff63a288fa46a3cd711b0ea6fe90cd9a701d66c08cb289f5cbaa00713
6
+ metadata.gz: 9f9b6c16423ead39824635414bf1de2e5efdf80878b19d234f263663e003c90b8148411fb90498fdfa1525374bfdddf838a97bd28002aa5fcd67224a3b9ab990
7
+ data.tar.gz: e3143da9abc1cd2e17c0e1e0ed3253761af044a217246ca27978ed7321e542a5ae1c32499f6cf4e9f4ac380e889a096997774ee2ab95c18fedf78a3e8cc8b24a
data/lib/crypto.rb CHANGED
@@ -24,7 +24,8 @@ module SelfSDK
24
24
  keys = @account.otk['curve25519'].map{|k,v| {id: k, key: v}}.to_json
25
25
 
26
26
  # 1b-iv) post those keys to POST /v1/identities/<selfid>/devices/1/pre_keys/
27
- @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)
27
+ res = @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)
28
+ raise 'unable to push prekeys, please try in a few minutes' if res.code != 200
28
29
 
29
30
  # 1b-v) store the account to a file
30
31
  File.write(account_path, @account.to_pickle(storage_key))
@@ -9,7 +9,7 @@ module SelfSDK
9
9
  module Messages
10
10
  class AuthenticationMessage < Base
11
11
 
12
- def parse(input, original=nil)
12
+ def parse(input, envelope=nil)
13
13
  @input = input
14
14
  @typ = @typ
15
15
  @payload = get_payload input
@@ -21,6 +21,10 @@ module SelfSDK
21
21
  @expires = ::Time.parse(payload[:exp])
22
22
  @issued = ::Time.parse(payload[:iat])
23
23
  @status = payload[:status]
24
+ if envelope
25
+ issuer = envelope.sender.split(":")
26
+ @from_device = issuer.last
27
+ end
24
28
  end
25
29
  end
26
30
  end
data/lib/messages/base.rb CHANGED
@@ -27,10 +27,12 @@ module SelfSDK
27
27
  res
28
28
  end
29
29
 
30
- def send_message
30
+ def send_message(device_id = nil)
31
31
  check_credits!
32
+ dds = devices
33
+ dds = [device_id] if device_id
32
34
  res = []
33
- devices.each do |d|
35
+ dds.each do |d|
34
36
  res << @messaging.send_message(proto(d))
35
37
  SelfSDK.logger.info "asynchronously requested information to #{@to}:#{d}"
36
38
  end
@@ -39,7 +39,7 @@ module SelfSDK
39
39
  end
40
40
  end
41
41
 
42
- def parse(input)
42
+ def parse(input, envelope=nil)
43
43
  @input = input
44
44
  @typ = MSG_TYPE
45
45
  @payload = get_payload input
@@ -51,6 +51,11 @@ module SelfSDK
51
51
  @description = @payload.include?(:description) ? @payload[:description] : nil
52
52
  @facts = @payload[:facts]
53
53
  @options = @payload[:options]
54
+
55
+ if envelope
56
+ issuer = envelope.sender.split(":")
57
+ @from_device = issuer.last
58
+ end
54
59
  end
55
60
 
56
61
  def build_response
@@ -13,7 +13,7 @@ module SelfSDK
13
13
 
14
14
  attr_accessor :facts, :audience
15
15
 
16
- def parse(input)
16
+ def parse(input, envelope=nil)
17
17
  @input = input
18
18
  @typ = MSG_TYPE
19
19
  @payload = get_payload input
@@ -35,6 +35,10 @@ module SelfSDK
35
35
  SelfSDK.logger.info e.message
36
36
  end
37
37
  end
38
+ if envelope
39
+ issuer = envelope.sender.split(":")
40
+ @from_device = issuer.last
41
+ end
38
42
  end
39
43
 
40
44
  def fact(name)
@@ -10,11 +10,13 @@ require_relative "authentication_req"
10
10
  module SelfSDK
11
11
  module Messages
12
12
  def self.parse(input, messaging, original=nil)
13
+ envelope = nil
13
14
  body = if input.is_a? String
14
- input
15
+ input
15
16
  else
16
- issuer = input.sender.split(":")
17
- messaging.encryption_client.decrypt(input.ciphertext, issuer.first, issuer.last)
17
+ envelope = input
18
+ issuer = input.sender.split(":")
19
+ messaging.encryption_client.decrypt(input.ciphertext, issuer.first, issuer.last)
18
20
  end
19
21
 
20
22
  jwt = JSON.parse(body, symbolize_names: true)
@@ -23,16 +25,16 @@ module SelfSDK
23
25
  case payload[:typ]
24
26
  when "identities.facts.query.req"
25
27
  m = FactRequest.new(messaging)
26
- m.parse(body)
28
+ m.parse(body, envelope)
27
29
  when "identities.facts.query.resp"
28
30
  m = FactResponse.new(messaging)
29
- m.parse(body)
31
+ m.parse(body, envelope)
30
32
  when "identities.authenticate.resp"
31
33
  m = AuthenticationResp.new(messaging)
32
- m.parse(body)
34
+ m.parse(body, envelope)
33
35
  when "identities.authenticate.req"
34
36
  m = AuthenticationReq.new(messaging)
35
- m.parse(body)
37
+ m.parse(body, envelope)
36
38
  else
37
39
  raise StandardError.new("Invalid message type.")
38
40
  end
data/lib/messaging.rb CHANGED
@@ -43,6 +43,7 @@ module SelfSDK
43
43
  @client = client
44
44
  @ack_timeout = 60 # seconds
45
45
  @timeout = 120 # seconds
46
+ @auth_id = SecureRandom.uuid
46
47
  @device_id = options.fetch(:device_id, DEFAULT_DEVICE)
47
48
  @auto_reconnect = options.fetch(:auto_reconnect, DEFAULT_AUTO_RECONNECT)
48
49
  @raw_storage_dir = options.fetch(:storage_dir, DEFAULT_STORAGE_DIR)
@@ -264,8 +265,10 @@ module SelfSDK
264
265
  # Start sthe websocket listener
265
266
  def start
266
267
  SelfSDK.logger.info "starting"
268
+ auth_id = @auth_id.dup
269
+
267
270
  @mon.synchronize do
268
- @acks["authentication"] = { waiting_cond: @mon.new_cond,
271
+ @acks[auth_id] = { waiting_cond: @mon.new_cond,
269
272
  waiting: true,
270
273
  timeout: SelfSDK::Time.now + @ack_timeout }
271
274
  end
@@ -283,8 +286,16 @@ module SelfSDK
283
286
  end
284
287
 
285
288
  @mon.synchronize do
286
- @acks["authentication"][:waiting_cond].wait_while { @acks["authentication"][:waiting] }
287
- @acks.delete("authentication")
289
+ @acks[auth_id][:waiting_cond].wait_while { @acks[auth_id][:waiting] }
290
+ @acks.delete(auth_id)
291
+ end
292
+ # In case this does not succeed start the process again.
293
+ if @acks.include? auth_id
294
+ if @acks[auth_id][:waiting]
295
+ close
296
+ start_connection
297
+ end
298
+ @acks.delete(auth_id)
288
299
  end
289
300
  end
290
301
 
@@ -404,14 +415,18 @@ module SelfSDK
404
415
 
405
416
  # Authenticates current client on the websocket server.
406
417
  def authenticate
418
+ @auth_id = SecureRandom.uuid if @auth_id.nil?
419
+
407
420
  SelfSDK.logger.info "authenticating"
408
421
  send_raw Msgproto::Auth.new(
409
422
  type: Msgproto::MsgType::AUTH,
410
- id: "authentication",
423
+ id: @auth_id,
411
424
  token: @jwt.auth_token,
412
425
  device: @device_id,
413
426
  offset: @offset,
414
427
  )
428
+
429
+ @auth_id = nil
415
430
  end
416
431
 
417
432
  def send_raw(msg)
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.163
4
+ version: 0.0.168
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aldgate Ventures