Class: Authsignal::Client

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

Constant Summary collapse

USER_AGENT =
'authsignal-ruby'
NO_API_KEY_MESSAGE =
'No Authsignal API Secret Key Set'

Instance Method Summary collapse

Constructor Details

#initialize(retry_options: RETRY_OPTIONS) ⇒ Client

Returns a new instance of Client.



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

def initialize(retry_options: RETRY_OPTIONS)
  @api_key = require_api_key

  @client = Faraday.new do |builder|
    builder.url_prefix = Authsignal.configuration.api_url
    builder.adapter :net_http
    builder.request :authorization, :basic, @api_key, nil

    builder.headers['Accept'] = 'application/json'
    builder.headers['Content-Type'] = 'application/json'
    builder.headers['User-Agent'] = USER_AGENT
    builder.headers['X-Authsignal-Version'] = Authsignal::VERSION

    builder.request :json
    builder.response :json, parser_options: { symbolize_names: true }

    builder.use Middleware::JsonRequest
    builder.use Middleware::JsonResponse

    builder.request :retry, retry_options if Authsignal.configuration.retry
    builder.response :logger, ::Logger.new($stdout), bodies: true if Authsignal.configuration.debug
  end
end

Instance Method Details

#challenge(verification_method: nil, action: nil, challenge_id: nil, idempotency_key: nil, user_id: nil, email: nil, phone_number: nil, sms_channel: nil, locale: nil, device_id: nil, ip_address: nil, user_agent: nil, custom: nil, scope: nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/authsignal/client.rb', line 126

def challenge(
  verification_method: nil,
  action: nil,
  challenge_id: nil,
  idempotency_key: nil,
  user_id: nil,
  email: nil,
  phone_number: nil,
  sms_channel: nil,
  locale: nil,
  device_id: nil,
  ip_address: nil,
  user_agent: nil,
  custom: nil,
  scope: nil
)
  body = {
    verification_method: verification_method,
    action: action,
    challenge_id: challenge_id,
    idempotency_key: idempotency_key,
    user_id: user_id,
    email: email,
    phone_number: phone_number,
    sms_channel: sms_channel,
    locale: locale,
    device_id: device_id,
    ip_address: ip_address,
    user_agent: user_agent,
    custom: custom,
    scope: scope
  }
  make_request(:post, 'challenge', body: body)
end

#claim_challenge(challenge_id:, user_id:, skip_verification_check: nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/authsignal/client.rb', line 169

def claim_challenge(
  challenge_id:,
  user_id:,
  skip_verification_check: nil
)
  body = {
    challenge_id: challenge_id,
    user_id: user_id,
    skip_verification_check: skip_verification_check
  }
  make_request(:post, 'claim', body: body)
end

#create_session(client_id:, token:, action: nil) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/authsignal/client.rb', line 200

def create_session(client_id:, token:, action: nil)
  body = {
    client_id: client_id,
    token: token,
    action: action
  }.compact
  make_request(:post, 'sessions', body: body)
end

#delete_authenticator(user_id:, user_authenticator_id:) ⇒ Object



84
85
86
# File 'lib/authsignal/client.rb', line 84

def delete_authenticator(user_id:, user_authenticator_id:)
  make_request(:delete, "users/#{url_encode(user_id)}/authenticators/#{url_encode(user_authenticator_id)}")
end

#delete_user(user_id:) ⇒ Object



51
52
53
# File 'lib/authsignal/client.rb', line 51

def delete_user(user_id:)
  make_request(:delete, "users/#{url_encode(user_id)}")
end

#enroll_verified_authenticator(user_id:, attributes:) ⇒ Object



80
81
82
# File 'lib/authsignal/client.rb', line 80

def enroll_verified_authenticator(user_id:, attributes:)
  make_request(:post, "users/#{url_encode(user_id)}/authenticators", body: attributes)
end

#get_action(user_id:, action:, idempotency_key:) ⇒ Object



101
102
103
# File 'lib/authsignal/client.rb', line 101

def get_action(user_id:, action:, idempotency_key:)
  make_request(:get, "users/#{url_encode(user_id)}/actions/#{action}/#{url_encode(idempotency_key)}")
end

#get_authenticators(user_id:) ⇒ Object



76
77
78
# File 'lib/authsignal/client.rb', line 76

def get_authenticators(user_id:)
  make_request(:get, "users/#{url_encode(user_id)}/authenticators")
end

#get_challenge(challenge_id: nil, user_id: nil, action: nil, verification_method: nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/authsignal/client.rb', line 182

def get_challenge(
  challenge_id: nil,
  user_id: nil,
  action: nil,
  verification_method: nil
)
  params = {}
  params[:challengeId] = challenge_id if challenge_id
  params[:userId] = user_id if user_id
  params[:action] = action if action
  params[:verificationMethod] = verification_method if verification_method

  query_string = URI.encode_www_form(params) unless params.empty?
  path = query_string ? "challenges?#{query_string}" : 'challenges'

  make_request(:get, path)
end

#get_user(user_id:) ⇒ Object



42
43
44
45
# File 'lib/authsignal/client.rb', line 42

def get_user(user_id:)
  path = "users/#{url_encode(user_id)}"
  make_request(:get, path)
end

#identify(user_id, user_payload) ⇒ Object

TODO: delete identify?



234
235
236
# File 'lib/authsignal/client.rb', line 234

def identify(user_id, user_payload)
  make_request(:post, "users/#{url_encode(user_id)}", body: user_payload)
end

#query_user_actions(user_id:, from_date: nil, action_codes: [], state: nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/authsignal/client.rb', line 105

def (
  user_id:,
  from_date: nil,
  action_codes: [],
  state: nil
)
  params = {
    fromDate: from_date,
    codes: action_codes.empty? ? nil : action_codes.join(','),
    state: state
  }.compact

  base_path = "users/#{url_encode(user_id)}/actions"
  path = params.empty? ? base_path : "#{base_path}?#{URI.encode_www_form(params)}"
  make_request(:get, path)
end

#query_users(username: nil, email: nil, phone_number: nil, token: nil, limit: nil, last_evaluated_user_id: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/authsignal/client.rb', line 55

def query_users(
  username: nil,
  email: nil,
  phone_number: nil,
  token: nil,
  limit: nil,
  last_evaluated_user_id: nil
)
  params = {
    username: username,
    email: email,
    phoneNumber: phone_number,
    token: token,
    limit: limit&.to_s,
    lastEvaluatedUserId: last_evaluated_user_id
  }.compact

  path = params.empty? ? 'users' : "users?#{URI.encode_www_form(params)}"
  make_request(:get, path)
end

#refresh_session(refresh_token:) ⇒ Object



217
218
219
220
# File 'lib/authsignal/client.rb', line 217

def refresh_session(refresh_token:)
  body = { refresh_token: refresh_token }
  make_request(:post, 'sessions/refresh', body: body)
end

#revoke_session(access_token:) ⇒ Object



222
223
224
225
# File 'lib/authsignal/client.rb', line 222

def revoke_session(access_token:)
  body = { access_token: access_token }
  make_request(:post, 'sessions/revoke', body: body)
end

#revoke_user_sessions(user_id:) ⇒ Object



227
228
229
230
# File 'lib/authsignal/client.rb', line 227

def revoke_user_sessions(user_id:)
  body = { user_id: user_id }
  make_request(:post, 'sessions/user/revoke', body: body)
end

#track(user_id:, action:, attributes:) ⇒ Object



88
89
90
91
92
# File 'lib/authsignal/client.rb', line 88

def track(user_id:, action:, attributes:)
  path = "users/#{user_id}/actions/#{action}"

  make_request(:post, path, body: attributes)
end

#update_action(user_id:, action:, idempotency_key:, attributes:) ⇒ Object



122
123
124
# File 'lib/authsignal/client.rb', line 122

def update_action(user_id:, action:, idempotency_key:, attributes:)
  make_request(:patch, "users/#{url_encode(user_id)}/actions/#{action}/#{url_encode(idempotency_key)}", body: attributes)
end

#update_user(user_id:, attributes:) ⇒ Object



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

def update_user(user_id:, attributes:)
  make_request(:post, "users/#{url_encode(user_id)}", body: attributes)
end

#validate_challenge(token:, user_id: nil, action: nil) ⇒ Object



94
95
96
97
98
99
# File 'lib/authsignal/client.rb', line 94

def validate_challenge(token:, user_id: nil, action: nil)
  path = 'validate'
  body = { user_id: user_id, token: token, action: action }

  make_request(:post, path, body: body)
end

#validate_session(access_token:, client_ids: nil) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/authsignal/client.rb', line 209

def validate_session(access_token:, client_ids: nil)
  body = {
    access_token: access_token,
    client_ids: client_ids
  }.compact
  make_request(:post, 'sessions/validate', body: body)
end

#verify(challenge_id:, verification_code:) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/authsignal/client.rb', line 161

def verify(challenge_id:, verification_code:)
  body = {
    challenge_id: challenge_id,
    verification_code: verification_code
  }
  make_request(:post, 'verify', body: body)
end