Class: Userbin::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/userbin/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response, opts = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/userbin/client.rb', line 20

def initialize(request, response, opts = {})
  # Save a reference in the per-request store so that the request
  # middleware in request.rb can access it
  RequestStore.store[:userbin] = self

  if response.class.name == 'ActionDispatch::Cookies::CookieJar'
    cookies = Userbin::CookieStore::Rack.new(response)
  else
    cookies = Userbin::CookieStore::Base.new(request, response)
  end

  @store = Userbin::TokenStore.new(cookies)

  @request_context = {
    ip: request.ip,
    user_agent: request.user_agent,
    cookie_id: cookies['__cid']
  }
end

Instance Attribute Details

#request_contextObject

Returns the value of attribute request_context.



4
5
6
# File 'lib/userbin/client.rb', line 4

def request_context
  @request_context
end

Class Method Details

.install_proxy_methods(*names) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/userbin/client.rb', line 6

def self.install_proxy_methods(*names)
  names.each do |name|
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{name}(*args)
        Userbin::User.new('$current').#{name}(*args)
      end
    RUBY
  end
end

Instance Method Details

#authorize!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/userbin/client.rb', line 48

def authorize!
  unless @store.session_token
    raise Userbin::UserUnauthorizedError,
      'Need to call login before authorize'
  end

  if @store.session_token.expired?
    Userbin::Monitoring.heartbeat
  end

  if mfa_in_progress?
    logout
    raise Userbin::UserUnauthorizedError,
        'Logged out due to being unverified'
  end

  if mfa_required? && !device_trusted?
    raise Userbin::ChallengeRequiredError
  end
end

#authorized?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/userbin/client.rb', line 69

def authorized?
  !!@store.session_token
end

#device_trusted?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/userbin/client.rb', line 115

def device_trusted?
  @store.session_token ? @store.session_token.device_trusted? : false
end

#has_default_pairing?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/userbin/client.rb', line 127

def has_default_pairing?
  @store.session_token ? @store.session_token.has_default_pairing? : false
end

#login(user_id, user_attrs = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/userbin/client.rb', line 73

def (user_id, user_attrs = {})
  # Clear the session token if any
  @store.session_token = nil

  user = Userbin::User.new(user_id.to_s)
  session = user.sessions.create(
    user: user_attrs, trusted_device_token: @store.trusted_device_token)

  # Set the session token for use in all subsequent requests
  @store.session_token = session.token

  session
end

#logoutObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/userbin/client.rb', line 87

def logout
  return unless @store.session_token

  # Destroy the current session specified in the session token
  begin
    sessions.destroy('$current')
  rescue Userbin::ApiError # ignored
  end

  # Clear the session token
  @store.session_token = nil
end

#mfa_enabled?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/userbin/client.rb', line 111

def mfa_enabled?
  @store.session_token ? @store.session_token.mfa_enabled? : false
end

#mfa_in_progress?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/userbin/client.rb', line 119

def mfa_in_progress?
  @store.session_token ? @store.session_token.mfa_in_progress? : false
end

#mfa_required?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/userbin/client.rb', line 123

def mfa_required?
  @store.session_token ? @store.session_token.mfa_required? : false
end

#session_tokenObject



40
41
42
# File 'lib/userbin/client.rb', line 40

def session_token
  @store.session_token
end

#session_token=(session_token) ⇒ Object



44
45
46
# File 'lib/userbin/client.rb', line 44

def session_token=(session_token)
  @store.session_token = session_token
end

#track(opts = {}) ⇒ Object



131
132
133
# File 'lib/userbin/client.rb', line 131

def track(opts = {})
  Userbin::Event.post('/v1/events', opts)
end

#trust_device(attrs = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/userbin/client.rb', line 100

def trust_device(attrs = {})
  unless @store.session_token
    raise Userbin::UserUnauthorizedError,
      'Need to call login before trusting device'
  end
  trusted_device = trusted_devices.create(attrs)

  # Set the session token for use in all subsequent requests
  @store.trusted_device_token = trusted_device.token
end