rodauth-omniauth 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19c3567386c82344beabaf49bfb59614694fd8fecb212e72583cea29ceed613b
4
- data.tar.gz: 7a6b4e6cac612f61c07e11163e98be55ff0d4ab0d0a7815f2a156c0a4bc51577
3
+ metadata.gz: 21b889e9c675d3fd02f65444f581e44965cb84982dd136fb912a5c9e6fb67c8c
4
+ data.tar.gz: 12a5f50598b671065998b012b9c54997dffa225d5d208d0a25ea90a48bfe3e8b
5
5
  SHA512:
6
- metadata.gz: bf3cb6c85645361488496d881dc10f35b09ea088a22b340264b7a1b5d42009af0b08f9783e850e04fffcaf2541685755d8cb6fa87ce2c8202016c2478a7e4c61
7
- data.tar.gz: ebb120c5a3c61935f8c230f9418f977fafeb693af0180805d1103c22d93308dc763253f476575c5b48b2dee157fef30b500443a8f8becfe23b78e665c92e0984
6
+ metadata.gz: f54a94233c789532139f2e07173e5162209674cf8f90c441df0e603b49b5840b4edad89a0b2e7f9a48c25856ba2d64c9dfed31ff6e61ad219d1ee2725a0d8c03
7
+ data.tar.gz: c7d3c6db0088e544890cf7be482038d6c9f52c42a95082a5cc5d5bc1ba2dcf92ec7614d6c23b793f1814ac4ffb1272d280d67c527d023d1d437705a5383b608d
data/README.md CHANGED
@@ -71,11 +71,18 @@ You can now add authentication links to your login form:
71
71
  Assuming you configured the providers correctly, you should now be able to authenticate via an external provider. The `omniauth` feature handles the callback request, automatically creating new identities and verified accounts from those identities as needed.
72
72
 
73
73
  ```rb
74
- DB[:accounts].all
75
- #=> [{ id: 123, status_id: 2, email: "user@example.com" }]
76
- DB[:account_identities].all
77
- #=> [{ id: 456, account_id: 123, provider: "facebook", uid: "984346198764" },
78
- # { id: 789, account_id: 123, provider: "google", uid: "5871623487134"}]
74
+ Account.all
75
+ #=> [#<Account @values={ id: 123, status_id: 2, email: "user@example.com" }>]
76
+ Account::Identity.all
77
+ #=> [#<Account::Identity @values={ id: 456, account_id: 123, provider: "facebook", uid: "984346198764" }>,
78
+ # #<Account::Identity @values={ id: 789, account_id: 123, provider: "google", uid: "5871623487134"}>]
79
+ ```
80
+
81
+ The example above assumes you're using [rodauth-model] (automatically setup with [rodauth-rails]), which will define `Account::Identity` model for the `account_identities` table, along with the `identities` association on the `Account` model.
82
+
83
+ ```rb
84
+ account = Account.first
85
+ account.identities #=> [#<Account::Identity ...>, ...]
79
86
  ```
80
87
 
81
88
  Currently, provider login is required to return the user's email address, and account creation is assumed not to require additional fields that need to be entered manually. There is currently also no built-in functionality for connecting/removing external identities when signed in. Both features are planned for future versions.
@@ -100,7 +107,17 @@ end
100
107
  account_from_omniauth {} # disable finding existing accounts for new identities
101
108
  ```
102
109
 
103
- If the account associated to the external identity exists and is unverified (e.g. it was created through normal registration), the callback phase will return an error response, as only verified accounts can be logged into. You can change the default error flash and redirect location in this case:
110
+ #### Account verification
111
+
112
+ If the account associated to the external identity exists and is unverified (e.g. it was created through normal registration), the callback phase will automatically verify the account and login, assuming the `verify_account` feature is enabled and external email is the same.
113
+
114
+ If you wish to disallow OmniAuth login into unverified accounts, set the following:
115
+
116
+ ```rb
117
+ omniauth_verify_account? false
118
+ ```
119
+
120
+ You can change the default error flash and redirect location in this case:
104
121
 
105
122
  ```rb
106
123
  omniauth_login_unverified_account_error_flash "The account matching the external identity is currently awaiting verification"
@@ -166,25 +183,6 @@ omniauth_identities_provider_column :provider
166
183
  omniauth_identities_uid_column :uid
167
184
  ```
168
185
 
169
- ### Model associations
170
-
171
- When using the [rodauth-model] gem, an `identities` one-to-many association will be defined on the account model:
172
-
173
- ```rb
174
- require "rodauth/model"
175
-
176
- class Account < Sequel::Model
177
- include Rodauth::Model(RodauthApp.rodauth)
178
- end
179
- ```
180
- ```rb
181
- Account.first.identities #=>
182
- # [
183
- # #<Account::Identity id=123 provider="facebook" uid="987434628">,
184
- # #<Account::Identity id=456 provider="google" uid="274673644">
185
- # ]
186
- ```
187
-
188
186
  ## Base
189
187
 
190
188
  The `omniauth` feature builds on top of the `omniauth_base` feature, which sets up OmniAuth and routes its requests, but has no interaction with the database. So, if you would prefer to handle external logins differently, you can load just the `omniauth_base` feature, and implement your own callback phase.
@@ -422,4 +420,5 @@ Everyone interacting in the rodauth-omniauth project's codebases, issue trackers
422
420
  [Rodauth]: https://github.com/jeremyevans/rodauth
423
421
  [OmniAuth]: https://github.com/omniauth/omniauth
424
422
  [rodauth-model]: https://github.com/janko/rodauth-model
423
+ [rodauth-rails]: https://github.com/janko/rodauth-rails
425
424
  [omniauth-oauth2]: https://github.com/omniauth/omniauth-oauth2
@@ -20,6 +20,10 @@ module Rodauth
20
20
  auth_value_method :omniauth_identities_provider_column, :provider
21
21
  auth_value_method :omniauth_identities_uid_column, :uid
22
22
 
23
+ auth_value_methods(
24
+ :omniauth_verify_account?,
25
+ )
26
+
23
27
  auth_methods(
24
28
  :create_omniauth_identity,
25
29
  :omniauth_identity_insert_hash,
@@ -38,7 +42,7 @@ module Rodauth
38
42
 
39
43
  def route_omniauth!
40
44
  result = super
41
- handle_omniauth_callback if omniauth_request?
45
+ handle_omniauth_callback if omniauth_strategy&.on_callback_path?
42
46
  result
43
47
  end
44
48
 
@@ -62,9 +66,13 @@ module Rodauth
62
66
  end
63
67
 
64
68
  if account && !open_account?
65
- set_response_error_reason_status(:unverified_account, unopen_account_error_status)
66
- set_redirect_error_flash omniauth_login_unverified_account_error_flash
67
- redirect omniauth_login_failure_redirect
69
+ if omniauth_verify_account?
70
+ omniauth_verify_account
71
+ else
72
+ set_response_error_reason_status(:unverified_account, unopen_account_error_status)
73
+ set_redirect_error_flash omniauth_login_unverified_account_error_flash
74
+ redirect omniauth_login_failure_redirect
75
+ end
68
76
  end
69
77
 
70
78
  transaction do
@@ -133,6 +141,17 @@ module Rodauth
133
141
 
134
142
  attr_reader :omniauth_identity
135
143
 
144
+ def omniauth_verify_account?
145
+ features.include?(:verify_account) && account[login_column] == omniauth_email
146
+ end
147
+
148
+ def omniauth_verify_account
149
+ transaction do
150
+ verify_account
151
+ remove_verify_account_key
152
+ end
153
+ end
154
+
136
155
  def _omniauth_new_account(login)
137
156
  acc = { login_column => login }
138
157
  unless skip_status_checks?
@@ -66,19 +66,19 @@ module Rodauth
66
66
 
67
67
  %w[email name].each do |info_key|
68
68
  define_method(:"omniauth_#{info_key}") do
69
- omniauth_info[info_key]
69
+ omniauth_info[info_key] if omniauth_info
70
70
  end
71
71
  end
72
72
 
73
73
  %w[provider uid info credentials extra].each do |auth_key|
74
74
  define_method(:"omniauth_#{auth_key}") do
75
- omniauth_auth.fetch(auth_key)
75
+ omniauth_auth[auth_key] if omniauth_auth
76
76
  end
77
77
  end
78
78
 
79
79
  %w[auth params strategy origin error error_type error_strategy].each do |data|
80
80
  define_method(:"omniauth_#{data}") do
81
- request.env.fetch("omniauth.#{data.tr("_", ".")}")
81
+ request.env["omniauth.#{data.tr("_", ".")}"]
82
82
  end
83
83
  end
84
84
 
@@ -166,12 +166,13 @@ module Rodauth
166
166
  # Makes OmniAuth strategies use the JWT session hash.
167
167
  def set_omniauth_jwt_session
168
168
  rack_session = request.env["rack.session"]
169
- session.keys.each { |k| session[k.to_s] = session.delete(k) } unless scope.opts[:sessions_convert_symbols]
169
+ session.transform_keys!(&:to_s) unless scope.opts[:sessions_convert_symbols]
170
170
  request.env["rack.session"] = session
171
171
  yield
172
172
  ensure
173
- session.keys.each { |k| session[k.to_sym] = session.delete(k) } unless scope.opts[:sessions_convert_symbols]
174
- request.env["rack.session"] = rack_session
173
+ session.transform_keys!(&:to_sym) unless scope.opts[:sessions_convert_symbols]
174
+ request.env.delete("rack.session")
175
+ request.env["rack.session"] = rack_session if rack_session
175
176
  end
176
177
 
177
178
  # Makes the Rodauth instance accessible inside OmniAuth strategies
@@ -193,10 +194,6 @@ module Rodauth
193
194
  end
194
195
  end
195
196
 
196
- def omniauth_request?
197
- request.env.key?("omniauth.strategy")
198
- end
199
-
200
197
  def self.included(auth)
201
198
  auth.extend ClassMethods
202
199
  auth.instance_variable_set(:@omniauth_providers, [])
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "rodauth-omniauth"
3
- spec.version = "0.3.3"
3
+ spec.version = "0.4.0"
4
4
  spec.authors = ["Janko Marohnić"]
5
5
  spec.email = ["janko@hey.com"]
6
6
 
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.homepage = "https://github.com/janko/rodauth-omniauth"
10
10
  spec.license = "MIT"
11
11
 
12
- spec.required_ruby_version = ">= 2.3"
12
+ spec.required_ruby_version = ">= 2.5"
13
13
 
14
14
  spec.metadata["homepage_uri"] = spec.homepage
15
15
  spec.metadata["source_code_uri"] = spec.homepage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodauth-omniauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-14 00:00:00.000000000 Z
11
+ date: 2024-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rodauth
@@ -205,14 +205,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - ">="
207
207
  - !ruby/object:Gem::Version
208
- version: '2.3'
208
+ version: '2.5'
209
209
  required_rubygems_version: !ruby/object:Gem::Requirement
210
210
  requirements:
211
211
  - - ">="
212
212
  - !ruby/object:Gem::Version
213
213
  version: '0'
214
214
  requirements: []
215
- rubygems_version: 3.4.7
215
+ rubygems_version: 3.5.11
216
216
  signing_key:
217
217
  specification_version: 4
218
218
  summary: Rodauth extension for logging in and creating account via OmniAuth authentication.