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, opts = {}) ⇒ Client

Returns a new instance of Client.



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

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

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

#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

#authorize!Object



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

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

  authorize

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

  raise Userbin::ChallengeRequiredError if mfa_required?
end

#authorized?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/userbin/client.rb', line 67

def authorized?
  !!session_token
end

#identify(user_id) ⇒ Object



52
53
54
55
56
57
# File 'lib/userbin/client.rb', line 52

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



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

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.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/userbin/client.rb', line 104

def logout
  return unless session_token

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

  # Clear the session token
  self.session_token = nil
end

#mfa_enabled?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/userbin/client.rb', line 117

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

#mfa_in_progress?Boolean

Returns:

  • (Boolean)


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

def mfa_in_progress?
  session_token ? session_token.has_challenge? : false
end

#mfa_required?Boolean

Returns:

  • (Boolean)


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

def mfa_required?
  session_token ? session_token.needs_challenge? : false
end

#session_tokenObject



47
48
49
50
# File 'lib/userbin/client.rb', line 47

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

#session_token=(value) ⇒ Object



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

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