oauth_im 0.10.2 → 0.11.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: 3157351b7b2f1b010967a23541e3661835a7dc600a4e37d75d26f512e71169d6
4
- data.tar.gz: b54781e3b5e79d8f167a09e9d1c36e09a4f975a3bf47d2a8279d9fb0adb8be74
3
+ metadata.gz: 3e55554bb19c99609987221e3459cdfa64e081a00bca42e5cdd716dbba825c3e
4
+ data.tar.gz: 5e00ecc4178482ab08985758e8e8da6610c3a73f8a8508b85ecda6caaa3dfe7b
5
5
  SHA512:
6
- metadata.gz: 14694090ee6a56ad6498adeb3980ac65405437953a603507a699eb40d987d9938baadf1e293c58f7c5b0869f2c0bea8fa674eed97eefa9c229ce76c32925d066
7
- data.tar.gz: 3a03bee203ffc14fc59cf50319fae33d4dbdc4603d93d68a93fe12ca78dd89d2e6b8e1415fa1fca441e39f8108a6f171f7d056cff6618df9b7cefad05f143922
6
+ metadata.gz: 63e320019d26c54d1d12b805e2d30f2a158f3cabf2d67a762d82319e793522e7c3f84c71072062060b917e78d8a7cbb77763d93e45438310fac7e37a6586195e
7
+ data.tar.gz: 2183f526608c15a64e801653a5fc8eeee58b75d8123de3dbfc74d5c1b4868890fc976315acb813b3c7febcdcc7b0c5179a58e75e091fde58409eec372698a260
@@ -15,10 +15,14 @@ module OauthIm
15
15
  end
16
16
 
17
17
  def proxy_user_for(user_id:)
18
+ ProxyUser.new proxy_attrs_for(user_id: user_id)
19
+ end
20
+
21
+ def proxy_attrs_for(user_id:)
18
22
  response = client.retrieve_user(user_id).success_response
19
23
  raise "No user for id #{user_id}" if response.blank?
20
24
 
21
- ProxyUser.new response.user
25
+ response.user
22
26
  end
23
27
 
24
28
  private
@@ -2,6 +2,13 @@
2
2
 
3
3
  require 'fusionauth/fusionauth_client'
4
4
 
5
+ ############################################################
6
+ # # EDC: memoizing state for these methods is problematic. #
7
+ # # without care, an object's local state can diverge from #
8
+ # # the remote state returned by the API. so for now #
9
+ # # it seems safest to leave things unmemoized #
10
+ ############################################################
11
+
5
12
  module OauthIm
6
13
  module HasRegistrationData
7
14
  delegate :email, to: :attrs
@@ -18,8 +25,20 @@ module OauthIm
18
25
  delegate :name, :id,
19
26
  to: :state, prefix: true
20
27
 
28
+ def registrations
29
+ attrs[:registrations] || []
30
+ end
31
+
32
+ def registration
33
+ registrations.first
34
+ end
35
+
21
36
  def registration_data
22
- @registration_data ||= attrs[:registrations]&.first&.data || {}
37
+ registration&.data || {}
38
+ end
39
+
40
+ def application_id
41
+ registration&.applicationId
23
42
  end
24
43
 
25
44
  def active?
@@ -47,7 +66,7 @@ module OauthIm
47
66
  end
48
67
 
49
68
  def full_name
50
- @full_name ||= "#{first_name} #{last_name}"
69
+ "#{first_name} #{last_name}"
51
70
  end
52
71
  end
53
72
  end
@@ -1,36 +1,120 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ ############################################################
4
+ # # EDC: memoizing state for these methods is problematic. #
5
+ # # without care, an object's local state can diverge from #
6
+ # # the remote state returned by the API. so for now #
7
+ # # it seems safest to leave things unmemoized #
8
+ ############################################################
9
+
3
10
  module OauthIm
4
11
  class ProxyUser < IdpClient
5
12
  include HasRegistrationData
6
13
 
7
- attr_reader :attrs
8
-
9
- delegate :to_h, to: :attrs
10
-
11
14
  def self.for(user_id:)
12
15
  AdminClient.new.proxy_user_for user_id: user_id
13
16
  end
14
17
 
18
+ attr_reader :attrs
19
+
15
20
  def initialize(attrs)
16
21
  @attrs = attrs
17
22
  super()
18
23
  end
19
24
 
25
+ def grant_sponsorship
26
+ update_registration { |data| data[:sponsor] = 'true' }
27
+ end
28
+
29
+ def revoke_sponsorship
30
+ update_registration { |data| data[:sponsor] = 'false' }
31
+ end
32
+
33
+ def to_h
34
+ os_to_h attrs
35
+ end
36
+
20
37
  def user_id
21
- @user_id ||= attrs[:id]
38
+ attrs[:id]
22
39
  end
23
40
 
24
41
  def send_reset_password_email
25
42
  client.forgot_password loginId: login_id
43
+ reset_attrs
44
+ end
45
+
46
+ def set_password(password, change_required: true)
47
+ update_user(password: password, passwordChangeRequired: change_required)
26
48
  end
27
49
 
28
50
  def deactivate
29
51
  client.deactivate_user user_id
52
+ reset_attrs
30
53
  end
31
54
 
32
55
  def reactivate
33
56
  client.reactivate_user user_id
57
+ reset_attrs
58
+ end
59
+
60
+ private
61
+
62
+ def update_registration
63
+ os_to_h(registration_data).then do |data|
64
+ yield data
65
+ client.patch_registration user_id, params_for(data)
66
+ end
67
+ reset_attrs
68
+ end
69
+
70
+ def update_user(data)
71
+ client.patch_user user_id, params_for_user(data)
72
+ reset_attrs
73
+ end
74
+
75
+ def params_for(data)
76
+ { registration:
77
+ { applicationId: application_id,
78
+ data: data } }
79
+ end
80
+
81
+ def params_for_user(data)
82
+ { applicationId: application_id, user: data }
83
+ end
84
+
85
+ def reset_attrs
86
+ @attrs = AdminClient.new.proxy_attrs_for(user_id: user_id)
87
+ self
88
+ end
89
+
90
+ ########################################################
91
+ # # EDC: even though the FusionAuth client returns #
92
+ # # an OpenStruct with nested OpenStruct values, the #
93
+ # # patch_registration method does not seem to handle #
94
+ # # nested OpenStruct objects. #
95
+ # # #
96
+ # # this method recurses over these structures and #
97
+ # # turns them into ordinary nested hashes that seem #
98
+ # # to be correctly handled. #
99
+ # # #
100
+ # # see this SO thread for inspiration: #
101
+ # # https://stackoverflow.com/a/62804063/778174 #
102
+ ########################################################
103
+ def os_to_h(obj) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
104
+ case obj
105
+ when Hash, OpenStruct # rubocop:disable Style/OpenStructUse
106
+ obj.to_h.transform_values do |val|
107
+ case val
108
+ when OpenStruct then os_to_h(val) # rubocop:disable Style/OpenStructUse
109
+ when Array
110
+ val.map { |entry| os_to_h(entry) }
111
+ else val
112
+ end
113
+ end
114
+ when Array
115
+ obj.map { |entry| os_to_h(entry) }
116
+ else obj
117
+ end
34
118
  end
35
119
  end
36
120
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OauthIm
4
- VERSION = '0.10.2'
4
+ VERSION = '0.11.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth_im
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Connally
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-08 00:00:00.000000000 Z
11
+ date: 2022-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fusionauth_client