Class: Userbin::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/userbin/client.rb', line 6

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

  # By default the session token is persisted in the Rack store, which may
  # in turn point to any source. But this option gives you an option to
  # use any store, such as Redis or Memcached to store your Userbin tokens.
  if opts[:session_store]
    @session_store = opts[:session_store]
  else
    @session_store = Userbin::SessionStore::Rack.new(request.session)
  end

  @request_context = {
    ip: request.ip,
    user_agent: request.user_agent
  }
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

Instance Method Details

#authorizeObject



59
60
61
62
63
64
65
# File 'lib/userbin/client.rb', line 59

def authorize
  return unless session_token

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

#authorized?Boolean

Returns:

  • (Boolean)


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

def authorized?
  !!session_token
end

#channelsObject



153
154
155
# File 'lib/userbin/client.rb', line 153

def channels
  Userbin::User.new('current').channels
end

#disable_mfaObject



165
166
167
# File 'lib/userbin/client.rb', line 165

def disable_mfa
  Userbin::User.new('current').disable_mfa
end

#enable_mfaObject



161
162
163
# File 'lib/userbin/client.rb', line 161

def enable_mfa
  Userbin::User.new('current').enable_mfa
end

#eventsObject



141
142
143
# File 'lib/userbin/client.rb', line 141

def events
  Userbin::User.new('current').events
end

#identify(user_id) ⇒ Object



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

def identify(user_id)
  # The user identifier is used in API paths so it needs to be cleaned
  user_id = URI.encode(user_id.to_s)

  @session_store.user_id = user_id
end

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



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/userbin/client.rb', line 46

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

  identify(user_id)

  session = Userbin::Session.post(
    "users/#{@session_store.user_id}/sessions", user: user_attrs)

  # Set the session token for use in all subsequent requests
  self.session_token = session.token
end

#logoutObject

This method ends the current monitoring session. It should be called whenever the user logs out from your system.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/userbin/client.rb', line 70

def logout
  return unless session_token

  # Destroy the current session specified in the session token
  begin
    Userbin::Session.destroy_existing('current')
  rescue Userbin::Error # ignored
  end

  # Clear the session token
  self.session_token = nil
end

#pairingsObject



149
150
151
# File 'lib/userbin/client.rb', line 149

def pairings
  Userbin::User.new('current').pairings
end

#recovery_codesObject



157
158
159
# File 'lib/userbin/client.rb', line 157

def recovery_codes
  Userbin::User.new('current').recovery_codes
end

#security_settings_urlObject

Raises:



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

def security_settings_url
  raise Userbin::Error unless session_token
  return "https://security.userbin.com/?session_token=#{session_token}"
end

#session_tokenObject



34
35
36
37
# File 'lib/userbin/client.rb', line 34

def session_token
  token = @session_store.read
  Userbin::SessionToken.new(token) if token
end

#session_token=(value) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/userbin/client.rb', line 26

def session_token=(value)
  if value && value != @session_store.read
    @session_store.write(value)
  elsif !value
    @session_store.destroy
  end
end

#sessionsObject



145
146
147
# File 'lib/userbin/client.rb', line 145

def sessions
  Userbin::User.new('current').sessions
end

#two_factor_authenticate!Object

This method creates a two-factor challenge for the current user, if the user has enabled a device for authentication.

If there already exists a challenge on the current session, it will be returned. Otherwise a new will be created.



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

def two_factor_authenticate!
  return unless session_token

  if session_token.needs_challenge?
    Userbin::Challenge.post("users/current/challenges")
    return two_factor_method
  end
end

#two_factor_enabled?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/userbin/client.rb', line 133

def two_factor_enabled?
  session_token.mfa_enabled?
end

#two_factor_in_progress?Boolean

Returns:

  • (Boolean)


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

def two_factor_in_progress?
  return false unless session_token
  session_token.has_challenge?
end

#two_factor_methodObject

If a two-factor authentication process has been started, this method will return the method which is used to perform the authentication. Eg. :authenticator or :sms



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

def two_factor_method
  return unless session_token
  return session_token.challenge_type
end

#two_factor_required?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/userbin/client.rb', line 137

def two_factor_required?
  session_token.needs_challenge?
end

#two_factor_verify(response) ⇒ Object

Once a two factor challenge has been created using two_factor_authenticate!, the response code from the user is verified using this method.



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

def two_factor_verify(response)
  # Need to have an active challenge to verify it
  return unless session_token && session_token.has_challenge?

  challenge = Userbin::Challenge.new('current')
  challenge.verify(response: response)
end