Class: Rmega::Session
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#master_key ⇒ Object
readonly
Returns the value of attribute master_key.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#rsa_privk ⇒ Object
readonly
Returns the value of attribute rsa_privk.
-
#shared_keys ⇒ Object
readonly
Returns the value of attribute shared_keys.
-
#sid ⇒ Object
readonly
Returns the value of attribute sid.
Instance Method Summary collapse
-
#initialize(email, password) ⇒ Session
constructor
A new instance of Session.
- #login(password) ⇒ Object
- #options ⇒ Object
- #random_request_id ⇒ Object
- #request(content, retries = max_retries) ⇒ Object
- #request_url ⇒ Object
- #storage ⇒ Object
Methods included from Loggable
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 = {} login(password) end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
14 15 16 |
# File 'lib/rmega/session.rb', line 14 def email @email end |
#master_key ⇒ Object (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_id ⇒ Object (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_privk ⇒ Object (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_keys ⇒ Object (readonly)
Returns the value of attribute shared_keys.
14 15 16 |
# File 'lib/rmega/session.rb', line 14 def shared_keys @shared_keys end |
#sid ⇒ Object (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 login(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 = Crypto.decrypt_rsa_privk(@master_key, resp['privk']) @sid = Crypto.decrypt_sid(@rsa_privk, resp['csid']) end |
#options ⇒ Object
24 25 26 |
# File 'lib/rmega/session.rb', line 24 def Rmega. end |
#random_request_id ⇒ Object
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_url ⇒ Object
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 |
#storage ⇒ Object
31 32 33 |
# File 'lib/rmega/session.rb', line 31 def storage @storage ||= Storage.new(self) end |