Class: Rmega::Session

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/rmega/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(email, password) ⇒ Session

Returns a new instance of Session.


16
17
18
19
20
21
22
# File 'lib/rmega/session.rb', line 16

def initialize(email, password)
  @email = email
  @request_id = random_request_id
  @shared_keys = {}

  (password)
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email


14
15
16
# File 'lib/rmega/session.rb', line 14

def email
  @email
end

#master_keyObject (readonly)

Returns the value of attribute master_key


14
15
16
# File 'lib/rmega/session.rb', line 14

def master_key
  @master_key
end

#request_idObject (readonly)

Returns the value of attribute request_id


14
15
16
# File 'lib/rmega/session.rb', line 14

def request_id
  @request_id
end

#rsa_privkObject (readonly)

Returns the value of attribute rsa_privk


14
15
16
# File 'lib/rmega/session.rb', line 14

def rsa_privk
  @rsa_privk
end

#shared_keysObject (readonly)

Returns the value of attribute shared_keys


14
15
16
# File 'lib/rmega/session.rb', line 14

def shared_keys
  @shared_keys
end

#sidObject (readonly)

Returns the value of attribute sid


14
15
16
# File 'lib/rmega/session.rb', line 14

def sid
  @sid
end

Instance Method Details

#login(password) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rmega/session.rb', line 35

def (password)
  uh = Crypto.stringhash Crypto.prepare_key_pw(password), email.downcase
  resp = request(a: 'us', user: email, uh: uh)

  # Decrypts the master key
  encrypted_key = Crypto.prepare_key_pw(password)
  @master_key = Crypto.decrypt_key(encrypted_key, Utils.base64_to_a32(resp['k']))

  # Generates the session id
  @rsa_privk, @sid = Crypto.rsa_decrypt_privk(@master_key, resp['privk'], resp['csid'])
  @sid
end

#optionsObject


24
25
26
# File 'lib/rmega/session.rb', line 24

def options
  Rmega.options
end

#random_request_idObject


48
49
50
# File 'lib/rmega/session.rb', line 48

def random_request_id
  rand(1E7..1E9).to_i
end

#request(content, retries = max_retries) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rmega/session.rb', line 58

def request(content, retries = max_retries)
  @request_id += 1
  logger.debug "POST #{request_url} #{content.inspect}"

  response = HTTPClient.new.post(request_url, [content].to_json, timeout: api_request_timeout)
  code, body = response.code.to_i, response.body

  logger.debug("#{code} #{body}")

  if code == 500 && body.to_s.empty?
    raise Errors::ServerError.new("Server too busy", temporary: true)
  else
    json = JSON.parse(body).first
    raise Errors::ServerError.new(json) if json.to_s =~ /\A\-\d+\z/
    json
  end
rescue SocketError, Errors::ServerError => error
  raise(error) if retries < 0
  raise(error) if error.respond_to?(:temporary?) && !error.temporary?
  retries -= 1
  sleep(retry_interval)
  retry
end

#request_urlObject


52
53
54
55
56
# File 'lib/rmega/session.rb', line 52

def request_url
  "#{api_url}?id=#{@request_id}".tap do |url|
    url << "&sid=#{@sid}" if @sid
  end
end

#storageObject


31
32
33
# File 'lib/rmega/session.rb', line 31

def storage
  @storage ||= Storage.new(self)
end