Class: Actn::Api::Client
- Inherits:
-
DB::Mod
- Object
- DB::Mod
- Actn::Api::Client
- Defined in:
- lib/actn/api/client.rb
Constant Summary collapse
- DEFAULT_ACL =
{allow: ['*'], disallow: []}
- TTL =
360
Instance Attribute Summary collapse
-
#secret ⇒ Object
Returns the value of attribute secret.
Class Method Summary collapse
Instance Method Summary collapse
- #auth_by_secret(secret) ⇒ Object
- #auth_by_session(session_id) ⇒ Object
- #can?(resource) ⇒ Boolean
- #credentials ⇒ Object
- #reset_credentials! ⇒ Object
- #set_session(session_id) ⇒ Object
- #to_json(options = {}) ⇒ Object
Instance Attribute Details
#secret ⇒ Object
Returns the value of attribute secret.
17 18 19 |
# File 'lib/actn/api/client.rb', line 17 def secret @secret end |
Class Method Details
.find_for_auth(domain, apikey) ⇒ Object
25 26 27 28 |
# File 'lib/actn/api/client.rb', line 25 def self.find_for_auth domain, apikey client = self.find_by(domain: domain, apikey: apikey) client end |
Instance Method Details
#auth_by_secret(secret) ⇒ Object
30 31 32 |
# File 'lib/actn/api/client.rb', line 30 def auth_by_secret secret self.secret == secret end |
#auth_by_session(session_id) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/actn/api/client.rb', line 34 def auth_by_session session_id return unless client_session = self.sessions[session_id] if BCrypt::Password.new(client_session[0]) == session_id if Time.now.to_f - client_session[1] > TTL invalidated = self.update(sessions: self.sessions.tap{|s| s.delete session_id }) return false else return true end end end |
#can?(resource) ⇒ Boolean
64 65 66 67 |
# File 'lib/actn/api/client.rb', line 64 def can? resource return if self.acl['disallow'].include?("*") || self.acl['disallow'].include?(resource) self.acl['allow'].include?("*") || self.acl['allow'].include?(resource) end |
#credentials ⇒ Object
54 55 56 |
# File 'lib/actn/api/client.rb', line 54 def credentials {'apikey' => self.apikey, 'secret' => @secret} end |
#reset_credentials! ⇒ Object
58 59 60 61 62 |
# File 'lib/actn/api/client.rb', line 58 def reset_credentials! reset_credentials _update self end |
#set_session(session_id) ⇒ Object
46 47 48 |
# File 'lib/actn/api/client.rb', line 46 def set_session session_id self.update( { sessions: {session_id => [BCrypt::Password.create(session_id), Time.now.to_f] }} ) end |
#to_json(options = {}) ⇒ Object
69 70 71 |
# File 'lib/actn/api/client.rb', line 69 def to_json = {} super(.merge(methods: [:credentials], exclude: [:sessions, :secret_hash])) end |