foxlogin 0.6.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45b3f6949d9aeefe571bc6be1e3207b2d594d5da644702de3b61b4168378869a
4
- data.tar.gz: 0af28f5b947ddcd403bae76e59743532b2968a92af99a52df8a405a52d924204
3
+ metadata.gz: 2c8110530b327a6833d54807ec9c80c20609f160ff4c0684068f2141aab187d7
4
+ data.tar.gz: 5184532bad1a43c269160410391dc386983edc772d66d12d54c91927d848f964
5
5
  SHA512:
6
- metadata.gz: 96149f2f5890fcd41542f270de5f9399380437fe19108ee990319cf9a70972254b902e2fbf07251796cc77efd0d2680a0b0c31d94b5c8bd5e307c24e22df3d1f
7
- data.tar.gz: d70fff22282090b272de972fb51a46932631098707b4d28883142df1032c9a051263d62d5cd0b770a90f1999dbac8249de60ae416f3a782bd3411432269564f5
6
+ metadata.gz: f68865a1546671487326d7d0348a4a59091ed3f4574fd86c172191bfc953141e622ade70a28be6e802eb43d8a370a78f85303b6c0e93fcf6f91b3812d085b293
7
+ data.tar.gz: 210d914d4db462e768053bf7527479c3a28806067cd61b57f64eea43077b0cd1ea68c28805e4720d529d69416d8f1def397cf12587549255cd0f9862511cb910
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0 — 2026-12-04
4
+
5
+ Covers JS SDK 0.7.0–0.9.0 (this gem skipped 0.7.0 and 0.8.0).
6
+
7
+ - Two-factor: `setup_totp` (`{"secret", "otpauth_url"}`), `enable_totp(code:)`
8
+ (`{"recovery_codes", "user"}`) and `disable_totp(code:)` for the signed-in user. Sign-in can
9
+ return `mfa_setup_required` (application policy "required"); `setup_totp(mfa_token:)` and
10
+ `enable_totp(code:, mfa_token:)` then finish the sign-in and store the session.
11
+ `recovery_codes` is empty when the application turned them off.
12
+ - Google: new `handle_redirect(url_or_params, state:)` reads the return parameters and signs in,
13
+ or returns `country_required` / `profile_required` (with `fields` and `country`, nil when
14
+ unknown); raises on `fox_error` and on a different `state` (`invalid_state`).
15
+ `complete_google_sign_up(ticket:, country: nil, profile: nil)`: `country` is optional when the
16
+ redirect carried one; a malformed profile phone raises `invalid_field` before sending.
17
+ - `check_ip`: the visitor's IP country and whether the application allows sign-up and sign-in
18
+ from there (`{"country", "sign_up_allowed", "sign_in_allowed", "ip_check"}`). The server
19
+ refuses with `ip_country_not_allowed` (403).
20
+ - SMS sign-in: `send_sms_code(phone:)` (normalises the number, `invalid_field` when malformed,
21
+ sends to the region of the user with that number) and `sign_in_with_sms(phone:, code:)`.
22
+
3
23
  ## 0.6.0 — 2026-12-04
4
24
 
5
25
  This gem skipped 0.5.0; its profile calls ship here.
data/README.md CHANGED
@@ -39,8 +39,8 @@ token = fox.get_token
39
39
  fox.sign_out
40
40
  ```
41
41
 
42
- Passwordless sign-in and password reset return the same `{"status" => "signed_in" | "mfa_required"}`
43
- shape:
42
+ Passwordless sign-in and password reset return the same
43
+ `{"status" => "signed_in" | "mfa_required" | "mfa_setup_required"}` shape:
44
44
 
45
45
  ```ruby
46
46
  fox.send_sign_in_code(email: email) # always "code_sent"
@@ -105,9 +105,68 @@ elsif params["fox_error"]
105
105
  end
106
106
  ```
107
107
 
108
+ Or let `handle_redirect` read the parameters (a URL string or a params Hash) and do all of it:
109
+
110
+ ```ruby
111
+ r = fox.handle_redirect(request.url, state: session[:fox_state]) # nil when not a Fox Login return
112
+ case r&.fetch("status")
113
+ when "country_required"
114
+ fox.complete_google_sign_up(ticket: r["ticket"], country: "TR") # ask the user first
115
+ when "profile_required"
116
+ # The application requires fields Google doesn't give (r["fields"], e.g. ["username"]).
117
+ # Ask for them (and the country when r["country"] is nil); fix errors and send again.
118
+ fox.complete_google_sign_up(ticket: r["ticket"], country: r["country"] || "TR",
119
+ profile: { "username" => "ada" })
120
+ when "mfa_required"
121
+ fox.verify_mfa(mfa_token: r["mfa_token"], code: code)
122
+ end
123
+ ```
124
+
125
+ `fox_error` raises `FoxLogin::Error` with that code; a different `state` raises `invalid_state`.
126
+
108
127
  Whether Google shows Fox Login's app or your own Google app is set in the dashboard; the code is
109
128
  the same in both cases.
110
129
 
130
+ ### Sign in with an SMS code
131
+
132
+ ```ruby
133
+ # Dashboard > Login settings > SMS. Users need a phone number in their profile.
134
+ fox.send_sms_code(phone: "+905321234567") # always "code_sent"
135
+ result = fox.sign_in_with_sms(phone: "+905321234567", code: code)
136
+ ```
137
+
138
+ A malformed number raises `invalid_field` before sending. The code is sent from, and checked
139
+ in, the region of the user with that number.
140
+
141
+ ### Only some countries (IP check)
142
+
143
+ ```ruby
144
+ # Dashboard > Login settings > General: "Only listed countries" + IP check (sign-up, sign-in).
145
+ ip = fox.check_ip # => {"country" => "US", "sign_up_allowed" => false, "sign_in_allowed" => true, "ip_check" => true}
146
+ puts "Sign-up is not available in your country." unless ip["sign_up_allowed"]
147
+ # The server refuses anyway with FoxLogin::Error code "ip_country_not_allowed" (403).
148
+ ```
149
+
150
+ ### Two-factor authentication
151
+
152
+ ```ruby
153
+ # The signed-in user turns 2FA on in their settings:
154
+ setup = fox.setup_totp # show setup["otpauth_url"] as a QR code
155
+ done = fox.enable_totp(code: code) # first code from the app
156
+ done["recovery_codes"] # show them once
157
+ fox.disable_totp(code: code)
158
+
159
+ # Application policy "required" (dashboard > Login settings > 2FA): a user without 2FA sets
160
+ # it up during sign-in, before getting a session.
161
+ result = fox.sign_in(email: email, password: password)
162
+ if result["status"] == "mfa_setup_required"
163
+ setup = fox.setup_totp(mfa_token: result["mfa_token"])
164
+ done = fox.enable_totp(code: code, mfa_token: result["mfa_token"]) # signed in: done["user"]
165
+ end
166
+ ```
167
+
168
+ `recovery_codes` is empty when the application turned recovery codes off.
169
+
111
170
  ### Sign-up fields
112
171
 
113
172
  ```ruby
@@ -135,12 +135,8 @@ module FoxLogin
135
135
  raise FoxLogin::Error.new(status: 0, code: "invalid_email", message: "Enter a valid email address.",
136
136
  details: { "field" => "email" })
137
137
  end
138
- phone = profile.is_a?(Hash) ? (profile.key?("phone") ? profile["phone"] : profile[:phone]) : nil
139
- if phone.is_a?(String) && !phone.strip.empty? && FoxLogin.normalize_phone(phone).nil?
140
- raise FoxLogin::Error.new(status: 0, code: "invalid_field",
141
- message: "phone: an international number such as +905321234567.",
142
- details: { "field" => "phone" })
143
- end
138
+ phone = profile_phone(profile)
139
+ raise invalid_phone_error if phone.is_a?(String) && !phone.strip.empty? && FoxLogin.normalize_phone(phone).nil?
144
140
 
145
141
  body = { email: email, password: password, country: country }
146
142
  body[:name] = name if name
@@ -213,7 +209,8 @@ module FoxLogin
213
209
  end
214
210
 
215
211
  # On the redirect URI: exchanges the +fox_code+ query parameter for a
216
- # session; returns "signed_in" or "mfa_required" like #sign_in. When the
212
+ # session; returns "signed_in", "mfa_required" or "mfa_setup_required"
213
+ # like #sign_in (#handle_redirect does this and the other cases for you). When the
217
214
  # return carries fox_status=country_required and +fox_ticket+ instead,
218
215
  # ask the user's country and call #complete_google_sign_up; +fox_error+
219
216
  # means the sign-in did not finish. Pass the +fox_region+ query
@@ -223,9 +220,129 @@ module FoxLogin
223
220
  complete_sign_in("/v1/client/oauth/token", { code: code }, host)
224
221
  end
225
222
 
226
- # New Google user: creates the account in the chosen country and signs in.
227
- def complete_google_sign_up(ticket:, country:)
228
- complete_sign_in("/v1/client/oauth/complete", { ticket: ticket, country: country })
223
+ # On the redirect URI: reads the Fox Login query parameters (pass the
224
+ # full URL, or the query parameters as a Hash such as Rails' +params+)
225
+ # and finishes what they ask for. Returns +nil+ when they are not a Fox
226
+ # Login return; otherwise
227
+ # - signs in (+fox_code+, exchanged at +fox_region+): "signed_in",
228
+ # "mfa_required" or "mfa_setup_required" like #sign_in;
229
+ # - {"status" => "country_required", "ticket" => ...}: ask the user's
230
+ # country, then call #complete_google_sign_up;
231
+ # - {"status" => "profile_required", "ticket" => ..., "fields" => [...],
232
+ # "country" => "DE" or nil}: the application requires fields Google
233
+ # does not give; ask for them (and the country when nil), then call
234
+ # #complete_google_sign_up.
235
+ # +fox_error+ raises FoxLogin::Error with that code. Pass the +state+ you
236
+ # kept in the user's session as +state:+; a different one raises
237
+ # +invalid_state+.
238
+ def handle_redirect(url_or_params, state: nil)
239
+ q = redirect_params(url_or_params)
240
+ return nil unless q.key?("fox_code") || q.key?("fox_status") || q.key?("fox_error")
241
+
242
+ if state && !state.to_s.empty? && q["state"] != state.to_s
243
+ raise FoxLogin::Error.new(status: 400, code: "invalid_state",
244
+ message: "The sign-in did not start in this browser. Start again.")
245
+ end
246
+ err = q["fox_error"]
247
+ if err && !err.empty?
248
+ raise FoxLogin::Error.new(status: 400, code: err, message: "Google sign-in did not finish (#{err}).",
249
+ retryable: err != "access_denied")
250
+ end
251
+ case q["fox_status"]
252
+ when "country_required"
253
+ return { "status" => "country_required", "ticket" => q["fox_ticket"] || "" }
254
+ when "profile_required"
255
+ country = q["fox_country"]
256
+ return { "status" => "profile_required", "ticket" => q["fox_ticket"] || "",
257
+ "fields" => (q["fox_fields"] || "").split(",").reject(&:empty?),
258
+ "country" => country && !country.empty? ? country : nil }
259
+ end
260
+ exchange_google_code(code: q["fox_code"], region: q["fox_region"])
261
+ end
262
+
263
+ # New Google user: creates the account and signs in. +country+ is needed
264
+ # unless the redirect carried one (+profile_required+ with a country);
265
+ # +profile+ holds the required fields from +profile_required+ (and
266
+ # optional ones). Field errors (+missing_field+, +invalid_field+,
267
+ # +field_taken+) can be fixed and sent again with the same ticket. A
268
+ # malformed profile phone raises +invalid_field+ before sending.
269
+ def complete_google_sign_up(ticket:, country: nil, profile: nil)
270
+ phone = profile_phone(profile)
271
+ raise invalid_phone_error if phone.is_a?(String) && !phone.strip.empty? && FoxLogin.normalize_phone(phone).nil?
272
+
273
+ body = { ticket: ticket }
274
+ body[:country] = country if country
275
+ body[:profile] = profile if profile
276
+ complete_sign_in("/v1/client/oauth/complete", body)
277
+ end
278
+
279
+ # The visitor's IP country and whether the application lets them sign up
280
+ # / sign in from there (dashboard: Login settings > General > only listed
281
+ # countries + IP check). Use it to explain before the form; the server
282
+ # enforces it anyway (+ip_country_not_allowed+, 403). Returns
283
+ # {"country" => "US" or nil, "sign_up_allowed" => bool,
284
+ # "sign_in_allowed" => bool, "ip_check" => bool}.
285
+ def check_ip
286
+ r = Http.request(@transport, "GET", "#{@base_url}/v1/client/ip", headers: { "x-fox-publishable-key" => @key })
287
+ r = {} unless r.is_a?(Hash)
288
+ {
289
+ "country" => r["country"],
290
+ "sign_up_allowed" => r["sign_up_allowed"],
291
+ "sign_in_allowed" => r["sign_in_allowed"],
292
+ "ip_check" => r["ip_check"]
293
+ }
294
+ end
295
+
296
+ # Starts two-factor setup: returns {"secret" => ..., "otpauth_url" =>
297
+ # "otpauth://..."} (show the URL as a QR code). For the signed-in user,
298
+ # or with +mfa_token+ from +mfa_setup_required+ during a sign-in in an
299
+ # application that requires 2FA. Not active until #enable_totp.
300
+ def setup_totp(mfa_token: nil)
301
+ r = mfa_call("/v1/client/mfa/totp/setup", {}, mfa_token) || {}
302
+ { "secret" => r["secret"], "otpauth_url" => r["otpauth_url"] }
303
+ end
304
+
305
+ # Turns 2FA on with the first code from the authenticator app. Returns
306
+ # {"recovery_codes" => [...], "user" => user or nil}: the one-time
307
+ # recovery codes (empty when the application turned them off), show
308
+ # them once. With +mfa_token+ the waiting sign-in finishes too: the
309
+ # session is stored and "user" is the signed-in user.
310
+ def enable_totp(code:, mfa_token: nil)
311
+ r = mfa_call("/v1/client/mfa/totp/enable", { code: code }, mfa_token) || {}
312
+ user = nil
313
+ if mfa_token && r["user"] && r["access_token"]
314
+ host = @mutex.synchronize { @mfa_hosts.delete(mfa_token) } || @base_url
315
+ save(r["user"], r, host)
316
+ user = r["user"]
317
+ end
318
+ { "recovery_codes" => r["recovery_codes"] || [], "user" => user }
319
+ end
320
+
321
+ # Turns 2FA off for the signed-in user; needs a current code (or a
322
+ # recovery code).
323
+ def disable_totp(code:)
324
+ mfa_call("/v1/client/mfa/totp/disable", { code: code })
325
+ nil
326
+ end
327
+
328
+ # SMS sign-in, step 1: sends a 6-digit code to the user with this phone
329
+ # number (international format, e.g. "+905321234567"). Always resolves
330
+ # to {"status" => "code_sent"}, also when no account has the number.
331
+ # Needs SMS sign-in turned on for the application. A malformed number
332
+ # raises +invalid_field+ (status 0, nothing sent).
333
+ def send_sms_code(phone:)
334
+ normalized = FoxLogin.normalize_phone(phone)
335
+ raise invalid_phone_error unless normalized
336
+
337
+ post("/v1/client/sign_in/sms", { phone: normalized }, host_for_phone(normalized))
338
+ end
339
+
340
+ # SMS sign-in, step 2: the code from the SMS signs the user in (or asks
341
+ # for 2FA), like #sign_in.
342
+ def sign_in_with_sms(phone:, code:)
343
+ normalized = FoxLogin.normalize_phone(phone) || phone
344
+ complete_sign_in("/v1/client/sign_in/sms/verify", { phone: normalized, code: code },
345
+ host_for_phone(normalized))
229
346
  end
230
347
 
231
348
  # A valid access token, refreshed first when it expires within 60
@@ -332,12 +449,13 @@ module FoxLogin
332
449
 
333
450
  # Posts a sign-in-shaped request and returns either
334
451
  # {"status" => "signed_in", "user" => user} (and stores the session) or
335
- # {"status" => "mfa_required", "mfa_token" => token} (stores nothing).
452
+ # {"status" => "mfa_required" | "mfa_setup_required", "mfa_token" => token}
453
+ # (stores nothing).
336
454
  def complete_sign_in(path, body, host = @base_url)
337
455
  result = post(path, body, host)
338
- if result["status"] == "mfa_required"
456
+ if %w[mfa_required mfa_setup_required].include?(result["status"])
339
457
  @mutex.synchronize { @mfa_hosts[result["mfa_token"]] = host }
340
- return { "status" => "mfa_required", "mfa_token" => result["mfa_token"] }
458
+ return { "status" => result["status"], "mfa_token" => result["mfa_token"] }
341
459
  end
342
460
 
343
461
  # The session belongs to the user's region (a Google sign-up may be created elsewhere).
@@ -388,6 +506,68 @@ module FoxLogin
388
506
  host
389
507
  end
390
508
 
509
+ # The region host of the user with this phone number (SMS sign-in),
510
+ # located once per number like #host_for_email.
511
+ def host_for_phone(phone)
512
+ key = "phone:#{FoxLogin.normalize_phone(phone) || phone.to_s.strip}"
513
+ known = @mutex.synchronize { @email_hosts[key] }
514
+ return known if known
515
+
516
+ begin
517
+ located = post("/v1/client/locate", { phone: phone })
518
+ rescue FoxLogin::Error => e
519
+ raise if %w[rate_limited invalid_publishable_key].include?(e.code)
520
+
521
+ return @base_url
522
+ end
523
+ return @base_url unless located.is_a?(Hash) && located["region"].is_a?(String) && located["host"].is_a?(String)
524
+
525
+ host = @host_overrides[located["region"].upcase] || located["host"].sub(%r{/\z}, "")
526
+ @mutex.synchronize { @email_hosts[key] = host }
527
+ host
528
+ end
529
+
530
+ # A 2FA call with the access token, or (during a sign-in) with the setup
531
+ # +mfa_token+ in the body, sent to the host that issued it.
532
+ def mfa_call(path, body, mfa_token = nil)
533
+ if mfa_token
534
+ host = @mutex.synchronize { @mfa_hosts[mfa_token] } || @base_url
535
+ return post(path, body.merge(mfa_token: mfa_token), host)
536
+ end
537
+ token = get_token
538
+ raise signed_out_error unless token
539
+
540
+ Http.request(@transport, "POST", "#{session_host}#{path}",
541
+ headers: { "x-fox-publishable-key" => @key, "authorization" => "Bearer #{token}" },
542
+ json_body: body)
543
+ end
544
+
545
+ # Query parameters of a redirect URL (String) or a params Hash, as a
546
+ # Hash with String keys and String values.
547
+ def redirect_params(url_or_params)
548
+ if url_or_params.respond_to?(:to_unsafe_h)
549
+ url_or_params = url_or_params.to_unsafe_h
550
+ end
551
+ if url_or_params.is_a?(Hash)
552
+ return url_or_params.each_with_object({}) { |(k, v), h| h[k.to_s] = v.nil? ? nil : v.to_s }
553
+ end
554
+
555
+ query = URI.parse(url_or_params.to_s).query
556
+ query ? URI.decode_www_form(query).to_h : {}
557
+ end
558
+
559
+ def profile_phone(profile)
560
+ return nil unless profile.is_a?(Hash)
561
+
562
+ profile.key?("phone") ? profile["phone"] : profile[:phone]
563
+ end
564
+
565
+ def invalid_phone_error
566
+ FoxLogin::Error.new(status: 0, code: "invalid_field",
567
+ message: "phone: an international number such as +905321234567.",
568
+ details: { "field" => "phone" })
569
+ end
570
+
391
571
  def email_key(email)
392
572
  email.to_s.strip.downcase
393
573
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FoxLogin
4
- VERSION = "0.6.0"
4
+ VERSION = "0.9.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foxlogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - METANETSOFT
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-12-03 00:00:00.000000000 Z
11
+ date: 2026-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt