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

included, #logger

Constructor Details

#initialize(email, password) ⇒ Session

Returns a new instance of Session.



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

def initialize(email, password)
  @email = email
  @request_id = random_request_id

  (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

#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



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

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
  @sid = Crypto.decrypt_sid(@master_key, resp['csid'], resp['privk'])
end

#optionsObject



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

def options
  Rmega.options
end

#random_request_idObject



45
46
47
# File 'lib/rmega/session.rb', line 45

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

#request(body) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
# File 'lib/rmega/session.rb', line 55

def request(body)
  @request_id += 1
  logger.debug "POST #{request_url}\n#{body.inspect}"
  response = HTTPClient.new.post(request_url, [body].to_json, timeout: api_request_timeout)
  logger.debug "#{response.code}\n#{response.body}"
  resp = JSON.parse(response.body).first
  raise RequestError.new(resp) if RequestError.error_code?(resp)
  resp
end

#request_urlObject



49
50
51
52
53
# File 'lib/rmega/session.rb', line 49

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

#storageObject



29
30
31
# File 'lib/rmega/session.rb', line 29

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