vp-login 0.3.0 → 0.4.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: c4ae09a37c684fabb1ad0785244543d06f2dedd5677c082326e98cf4dfaca76b
4
- data.tar.gz: 95ffa6c00de23343793deca3cc8d22c3205ba6686d41f35727b7aa8290ea8444
3
+ metadata.gz: 1145318a725170296d20d3c9c87d2f7a02d9dd39890887d80381c03d66aac4e9
4
+ data.tar.gz: 98b6270e3389985bd3fb28b5c1c046f726da2ff97fffecb80028ac22ab8ea435
5
5
  SHA512:
6
- metadata.gz: 8e30bffd9319f637b6e780e52858edb1becc44029e2a9d40e62c4d9a4d3405a63db236c260ef205e07952ea6f3813d72a2ee06a95b62ea0eb03ee3abc922b077
7
- data.tar.gz: 6742ac424b91f9fb5f03cd2d023d0461874e61f818314a3e7aaca18c84bb64b6c8e3bc1e337497034f7970e3b3aa980136e8084be024ad838bd6cbcfffd82c76
6
+ metadata.gz: 24586aad73f4340ff7385c6cd424fe659132f02ac6f8ece3dee400839fd1705bef4297879fb2d2d5e7f7d201bb568bc1a199bfeca9d3d21aa83a6147d44e33fc
7
+ data.tar.gz: 2df05077edc9d4c001968b59ffb409f9c2bb64cd0a13014d17877deaf703c292b9961187d6de2cd2663b8224e6818afc8c095e8ade09280c4ae82a57b7a7ee22
data/README.md CHANGED
@@ -6,7 +6,7 @@ ID-token checks, Rails login endpoints, and the vpLogin admin API. Used by volo-
6
6
  ## Install
7
7
 
8
8
  ```ruby
9
- gem 'vp-login', '~> 0.3', require: 'vplogin'
9
+ gem 'vp-login', '~> 0.4', require: 'vplogin'
10
10
  ```
11
11
 
12
12
  It needs Ruby >= 3.1 and jwt 2.5–3.x; the controller needs Rails >= 7. It's published on
@@ -68,8 +68,26 @@ end
68
68
  user.vplogin_provision_later # on invite: create the vpLogin identity
69
69
  ```
70
70
 
71
- Both run as ActiveJob jobs on `config.job_queue`. Override `vplogin_name` /
72
- `vplogin_password_set?` on the model where the defaults don't fit.
71
+ Both run as ActiveJob jobs on `config.job_queue`.
72
+
73
+ ### Migrating existing users
74
+
75
+ ```bash
76
+ DRY_RUN=true bin/rails vplogin:migrate_users # count only
77
+ bin/rails vplogin:migrate_users # one provisioning job per user (IDS=1,2 to limit)
78
+ bin/rails vplogin:migration_status # identity + password / no password / missing
79
+ ```
80
+
81
+ Safe to re-run. A password or MFA device the person already has in vpLogin is never overwritten.
82
+ Model hooks for where the defaults don't fit:
83
+
84
+ | Hook | Default |
85
+ |---|---|
86
+ | `self.vplogin_migration_scope` | `all` |
87
+ | `vplogin_name` | `full_name`, `name`, then email |
88
+ | `vplogin_password_set?` | `false`: no password imported |
89
+ | `vplogin_mfa_devices` | `nil`, or `{ totp_secret_base32:, phone_number:, email_device: }` |
90
+ | `vplogin_identity_linked(pk)` | no-op; store the vpLogin user id here |
73
91
 
74
92
  ## Without the controller
75
93
 
@@ -6,18 +6,28 @@ module VpLogin
6
6
  # Include in the app's Devise user model to keep its vpLogin identity in step:
7
7
  #
8
8
  # user.vplogin_provision_later # e.g. on invite: create (or find) the vpLogin identity
9
+ # rake vplogin:migrate_users # the same for every existing user (vplogin/tasks.rake)
9
10
  #
10
11
  # and, automatically, any saved password change (set, change, reset) is copied to vpLogin as its
11
12
  # bcrypt hash. Creates are not synced: an app's new-user password is typically a throwaway.
12
- # Both run as ActiveJob jobs and do nothing without the admin API.
13
+ # All of it runs as ActiveJob jobs and does nothing without the admin API.
13
14
  #
14
15
  # Overridable:
15
- # vplogin_name -> display name for a new identity (full_name / name / email)
16
- # vplogin_password_set? -> true once the user chose their own password, so provisioning
17
- # copies it too (default false)
16
+ # vplogin_name -> display name for a new identity (full_name / name / email)
17
+ # vplogin_password_set? -> true once the user chose their own password, so provisioning
18
+ # copies it to an identity that has none (default false)
19
+ # vplogin_mfa_devices -> { totp_secret_base32:, phone_number:, email_device: } to import
20
+ # into an identity with no MFA device yet (default nil: none)
21
+ # vplogin_identity_linked(pk) -> store the vpLogin user id, if the app keeps one (default no-op)
22
+ # self.vplogin_migration_scope -> users rake vplogin:migrate_users covers (default all)
18
23
  module DeviseUser
19
24
  def self.included(base)
20
25
  base.after_commit :vplogin_sync_password, on: :update, if: :saved_change_to_encrypted_password?
26
+ base.extend(ClassMethods)
27
+ end
28
+
29
+ module ClassMethods
30
+ def vplogin_migration_scope = all
21
31
  end
22
32
 
23
33
  # Enqueued after the surrounding transactions commit, so the job can find the user.
@@ -41,6 +51,8 @@ module VpLogin
41
51
  end
42
52
 
43
53
  def vplogin_password_set? = false
54
+ def vplogin_mfa_devices = nil
55
+ def vplogin_identity_linked(_pk) = nil
44
56
 
45
57
  private
46
58
 
@@ -71,6 +83,9 @@ module VpLogin
71
83
  end
72
84
  end
73
85
 
86
+ # Creates (or finds) the user's vpLogin identity, then fills in what it lacks: the password and MFA
87
+ # devices are imported only into an identity without them, so a password or factor the person
88
+ # already has in vpLogin (e.g. from another Volopay app) is never overwritten. Safe to re-run.
74
89
  class ProvisionJob < UserJob
75
90
  def perform(user_class_name, user_id)
76
91
  user = find_user(user_class_name, user_id)
@@ -79,11 +94,31 @@ module VpLogin
79
94
  admin = VpLogin::Admin.new
80
95
  result = admin.provision_user!(email: user.email, name: user.vplogin_name,
81
96
  email_mfa: VpLogin.configuration.provision_email_mfa)
82
- if result.mfa_error
83
- VpLogin.configuration.logger.warn("[VpLogin] email MFA factor failed for #{user.email}: #{result.mfa_error.message}")
84
- end
85
- # A password chosen before the identity existed had nothing to sync to.
86
- sync_password(admin, user) if user.vplogin_password_set?
97
+ warn_mfa_error(user, result.mfa_error) if result.mfa_error
98
+ user.vplogin_identity_linked(result.pk)
99
+
100
+ import_password(admin, user, result) if user.vplogin_password_set?
101
+ import_mfa(admin, user, result.pk)
102
+ end
103
+
104
+ private
105
+
106
+ def import_password(admin, user, result)
107
+ return if user.encrypted_password.to_s.empty?
108
+ return unless result.created || result.identity['has_usable_password'] == false
109
+
110
+ admin.set_password_hash!(result.pk, password_hash: "bcrypt$#{user.encrypted_password}")
111
+ end
112
+
113
+ def import_mfa(admin, user, pk)
114
+ devices = user.vplogin_mfa_devices.to_h.compact
115
+ return if devices.empty? || admin.mfa_devices(pk).any?
116
+
117
+ admin.import_mfa_devices!(pk, **devices)
118
+ end
119
+
120
+ def warn_mfa_error(user, error)
121
+ VpLogin.configuration.logger.warn("[VpLogin] email MFA factor failed for #{user.email}: #{error.message}")
87
122
  end
88
123
  end
89
124
 
@@ -6,5 +6,9 @@ module VpLogin
6
6
  require 'action_dispatch/routing/mapper'
7
7
  ActionDispatch::Routing::Mapper.include(VpLogin::Routes)
8
8
  end
9
+
10
+ rake_tasks do
11
+ load File.expand_path('tasks.rake', __dir__)
12
+ end
9
13
  end
10
14
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Bulk migration of an app's existing users into vpLogin, for models that include
4
+ # VpLogin::DeviseUser. One VpLogin::ProvisionJob per user; every step is a no-op when already done,
5
+ # so re-running after a partial run only finishes what's left.
6
+ namespace :vplogin do
7
+ vplogin_scope = lambda do
8
+ model = VpLogin.configuration.user_model
9
+ model = model.constantize if model.is_a?(String)
10
+ scope = model.vplogin_migration_scope
11
+ ids = ENV['IDS'].to_s.split(',').map(&:strip).reject(&:empty?)
12
+ ids.empty? ? scope : scope.where(id: ids)
13
+ end
14
+
15
+ desc 'Create vpLogin identities for existing users (DRY_RUN=true to only count; IDS=1,2 to limit)'
16
+ task migrate_users: :environment do
17
+ abort 'vpLogin admin API is not configured (VPLOGIN_ADMIN_API_URL / VPLOGIN_API_TOKEN)' unless VpLogin.configuration.admin_api?
18
+
19
+ scope = vplogin_scope.call
20
+ if ENV['DRY_RUN'] == 'true'
21
+ puts "DRY_RUN: would enqueue #{scope.count} #{scope.model.name} users for vpLogin migration"
22
+ next
23
+ end
24
+
25
+ count = 0
26
+ scope.find_each do |user|
27
+ VpLogin::ProvisionJob.perform_later(user.class.name, user.id)
28
+ count += 1
29
+ end
30
+ puts "Enqueued #{count} VpLogin::ProvisionJob jobs on #{VpLogin.configuration.job_queue}"
31
+ end
32
+
33
+ desc 'Report which users have a vpLogin identity and password (one admin API call per user; IDS=1,2 to limit)'
34
+ task migration_status: :environment do
35
+ abort 'vpLogin admin API is not configured (VPLOGIN_ADMIN_API_URL / VPLOGIN_API_TOKEN)' unless VpLogin.configuration.admin_api?
36
+
37
+ admin = VpLogin::Admin.new
38
+ counts = Hash.new(0)
39
+ missing = []
40
+ vplogin_scope.call.find_each do |user|
41
+ identity = admin.find_user(email: user.email)
42
+ if identity.nil?
43
+ counts[:missing] += 1
44
+ missing << user.email
45
+ elsif identity['has_usable_password'] == false
46
+ counts[:no_password] += 1
47
+ else
48
+ counts[:migrated] += 1
49
+ end
50
+ rescue VpLogin::Error => e
51
+ counts[:errors] += 1
52
+ warn "#{user.email}: #{e.class}: #{e.message}"
53
+ end
54
+
55
+ puts "migrated (identity + password): #{counts[:migrated]}"
56
+ puts "identity without a password: #{counts[:no_password]}"
57
+ puts "no vpLogin identity: #{counts[:missing]}"
58
+ puts "errors: #{counts[:errors]}"
59
+ puts "missing: #{missing.first(20).join(', ')}#{' ...' if missing.size > 20}" if missing.any?
60
+ end
61
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VpLogin
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vp-login
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volopay
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: '7.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '13'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '13'
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: rspec
63
77
  requirement: !ruby/object:Gem::Requirement
@@ -112,6 +126,7 @@ files:
112
126
  - lib/vplogin/railtie.rb
113
127
  - lib/vplogin/routes.rb
114
128
  - lib/vplogin/state_store.rb
129
+ - lib/vplogin/tasks.rake
115
130
  - lib/vplogin/version.rb
116
131
  homepage:
117
132
  licenses: