rodauth-select-account 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9496ecf9a3647c6371feef06b8cb81403ff9c076012650f740778f246b66180e
4
+ data.tar.gz: aeb014bfa0376f53fc34bc4fe7870fc4ac23b080da8b8ce11457c3ca43404765
5
+ SHA512:
6
+ metadata.gz: ea5fb45c7bf98c22827756fc9c5b0f684f7a76be9843a1f0a46d0d4088f2f4957161b562b1c4c1fb91ff952e4783f6cde6cdccd456401fb51281a25486e9d317
7
+ data.tar.gz: 66e79a5a50fc9df83aa63228b8079df3cbee160e56f23c05e84efbb2201c9c1400c48e7eabd9c788d602946aa15e3aa8f215c6519601a9ceb6cfcf9d8c8e2b7b
@@ -0,0 +1,3 @@
1
+ # CHANGELOG
2
+
3
+ ## master
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ https://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2013-2017 Docker, Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ https://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
@@ -0,0 +1,139 @@
1
+ # Rodauth::SelectAccount
2
+
3
+ [![pipeline status](https://gitlab.com/honeyryderchuck/rodauth-select-account/badges/master/pipeline.svg)](https://gitlab.com/honeyryderchuck/rodauth-select-account/-/pipelines?page=1&ref=master)
4
+ [![coverage report](https://gitlab.com/honeyryderchuck/rodauth-select-account/badges/master/coverage.svg)](https://honeyryderchuck.gitlab.io/rodauth-select-account/coverage/#_AllFiles)
5
+
6
+ This gem adds a feature to `rodauth` to support the management of multiple accounts in the same session. The behaviour is similar to how the "Google Sign-In" widget works, where you can sign-in with a different account, or switch to an already authenticated account.
7
+
8
+ ## Features
9
+
10
+ This feature implements multiple accounts on the same session seamlessly, so that you can use the remaining `rodauth` features (they'll always refer to the currently selected account). Besides that, you'll be able to:
11
+
12
+ * Select previously used account to login;
13
+ * Sign in with a different account;
14
+ * Switch to an already authennticated account;
15
+ * Log out from all accounts;
16
+
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'rodauth-select-account'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle install
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install rodauth-select-account
33
+
34
+
35
+ ## Usage
36
+
37
+ Usage of this feature should be as simple as:
38
+
39
+ ```ruby
40
+ plugin :rodauth do
41
+ # enable it in the plugin
42
+ enable :select_account, :login, :logout
43
+ end
44
+
45
+ # then, inside roda
46
+
47
+ route do |r|
48
+ r.rodauth
49
+ # ...
50
+ end
51
+ ```
52
+
53
+ ## Example
54
+
55
+ There's an example application in the project [under the examples directory](/examples). You can clone de project, install the dependencies and then you can run it with:
56
+
57
+ `> bundle exec ruby examples/goggles.rb`
58
+
59
+ Then you can open your browser under `http://localhost:9292`.
60
+
61
+ Here's a screenshot:
62
+
63
+ ![rodauth-select-account example](/examples/select-account-example.png)
64
+
65
+ ## Add Account
66
+
67
+ The URLs provided are:
68
+
69
+ * `GET /add-account`: In renders a form, similar to the login form, where you'll input the login and password of the account you want to add.
70
+ * `POST /add-account`: When the aforementioned form gets submitted; This will both add the new account to the session, and use it as the current account.
71
+
72
+ ## Select Account
73
+
74
+ The URLs provided are:
75
+
76
+ * `GET /select-account`: It renders a page with recently used accounts, to switch to.
77
+ * `POST /select-account`: Submitted with a login to switch the current account to; If you're not logged in, It'll ask you to log in with the chosen account; If logged in, but the account hasn't been authenticated yet, it'll ask you to add the account to the session; otherwise, it'll switch the current account to the selected one.
78
+
79
+
80
+ The accounts a user can switch are: accounts the user recently logged into, or added. Seamless switching only happens if the account has been logged in for the current session, otherwise the user will be prompted to log in. When the user logs out, it logs out from all accounts. However, the accounts will be able to be select, for a time, in the `/select-account` page, although the user will always have to log in before the account can be selected.
81
+
82
+ # Options
83
+
84
+ You'll be able to tweak the following options
85
+
86
+ * `selected_account_key` (default: `:selected_account_id`) key from session where the current account is stored;
87
+ * `accounts_key` (default: `:accounts`) key from session where all authenticated accounts are stored;
88
+ * `add_account_redirect_session_key` (default: `:add_account_redirect`) key from session where the uri to redirect after adding an account is stored;
89
+ * `select_account_redirect_session_key` (default: `:select_account_redirect`) key from session where the uri to redirect after selecting an account is stored;
90
+ * `require_selected_account_cookie_key` (default: `"_require_selected_account"`) key from cookies where it stored if an account has been selected for a flow requiring it;
91
+ * `accounts_cookie_key` (default: `"_accounts"`) key from cookies where all previously used accounts are stored;
92
+ * `accounts_cookie_options` (default: `{}.freeze`) accounts cookie options;
93
+ * `require_selected_account_cookie_interval` (default: 5 minutes): for how much time the selected account will hold, for a flow requiring it;
94
+ * `accounts_cookie_interval` (default: `14 + 60 * 60 * 24`, 14 days): for how much time the previously used accounts in the user agent are going to be remembered;
95
+ * `no_account_error_status`: (default: 409) the status code used when no account is found for an existing account in session (for example, if an account has been closed or deleted);
96
+ * `select_account_required_error_status`: (default: 403) the status code indicating that an account needs to be selected;
97
+ * `add_account_required_error_status`: (default: 403) the status code indicating that an account needs to be added;
98
+ * `no_account_message`: flash error message when no account is found;
99
+
100
+ The following options and methods are also available to override, and names should be self-explanatory according to rodauth conventions:
101
+
102
+ * `add_account_notice_flash`
103
+ * `add_account_error_flash`
104
+ * `require_add_account_error_flash`
105
+ * `add_account_view`
106
+ * `before_add_account_route`
107
+ * `before_add_account`
108
+ * `after_add_account`
109
+ * `add_account_button`
110
+ * `add_account_redirect`
111
+ * `select_account_notice_flash`
112
+ * `select_account_error_flash`
113
+ * `require_select_account_error_flash
114
+ * `before_select_account_route`
115
+ * `before_select_account`
116
+ * `after_select_account`
117
+ * `select_account_redirect`
118
+ * `add_account_path`
119
+ * `add_account_url`
120
+ * `select_account_path`
121
+ * `select_account_url`
122
+
123
+ These are also available methods:
124
+
125
+ * `accounts_in_session`: returns all the available accounts in session
126
+ * `require_select_account`: to be used like `require_account`, as a filter to actions that require a user to explicitly select an account before allowing it.
127
+
128
+ ## Ruby support policy
129
+
130
+ The minimum Ruby version required to run `rodauth-select-account` is 2.4 . Besides that, it should support all rubies that rodauth and roda support, including JRuby and (potentially, I don't know yet) truffleruby.
131
+
132
+ ## Development
133
+
134
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests, and `rake rubocop` to run the linter.
135
+
136
+ ## Contributing
137
+
138
+ Bug reports and pull requests are welcome on Gitlab at https://gitlab.com/honeyryderchuck/rodauth-select-account.
139
+
@@ -0,0 +1,275 @@
1
+ # frozen-string-literal: true
2
+
3
+ module Rodauth
4
+ Feature.define(:select_account) do
5
+ depends :login
6
+
7
+ # add-account
8
+ notice_flash "You have added a new account", "add_account"
9
+ error_flash "There was an error adding the new account", "add_account"
10
+ error_flash "Please add account to continue", "require_add_account"
11
+ view "add-account", "Add Account", "add_account"
12
+ before "add_account"
13
+ after "add_account"
14
+ button "Add Account", "add_account"
15
+ redirect "add_account"
16
+
17
+ # select-account
18
+ notice_flash "You have selected an account"
19
+ error_flash "There was an error selecting the account"
20
+ error_flash "Please select account to continue", "require_select_account"
21
+ before
22
+ after
23
+ redirect
24
+ view "select-account", "Select Account"
25
+
26
+ session_key :selected_account_key, :selected_account_id
27
+ session_key :accounts_key, :accounts
28
+ session_key :add_account_redirect_session_key, :add_account_redirect
29
+ session_key :select_account_redirect_session_key, :select_account_redirect
30
+ auth_value_method :require_selected_account_cookie_key, "_require_selected_account"
31
+ auth_value_method :accounts_cookie_key, "_accounts"
32
+ auth_value_method :accounts_cookie_options, {}.freeze
33
+ auth_value_method :accounts_cookie_interval, 14 * 60 * 60 * 24 # 14 days
34
+ auth_value_method :require_selected_account_cookie_interval, 60 * 5 # 5 minutes
35
+ auth_value_method :no_account_error_status, 409
36
+ auth_value_method :select_account_required_error_status, 403
37
+ auth_value_method :add_account_required_error_status, 403
38
+ translatable_method :no_account_message, "could not select account"
39
+
40
+ def accounts_in_session
41
+ @accounts_in_session ||= _get_accounts_in_session
42
+ end
43
+
44
+ def require_login_redirect
45
+ redirect_uri = super
46
+
47
+ return redirect_uri unless request.path == select_account_path && (login = param_or_nil(login_param))
48
+
49
+ redirect_uri = URI(redirect_uri)
50
+ redirect_uri.query = [redirect_uri.query, "#{login_param}=#{login}"].join("&")
51
+ redirect_uri.to_s
52
+ end
53
+
54
+ def require_select_account
55
+ # whether an account has been selected for a certain workflow will be driven by a short-lived
56
+ # cookie, which will hopefully be active during the duration of account selection
57
+ if request.cookies[require_selected_account_cookie_key]
58
+ ::Rack::Utils.delete_cookie_header!(response.headers, require_selected_account_cookie_key)
59
+ return
60
+ end
61
+
62
+ opts = {
63
+ value: true,
64
+ expires: Time.now + require_selected_account_cookie_interval
65
+ }
66
+ ::Rack::Utils.set_cookie_header!(response.headers, require_selected_account_cookie_key, opts)
67
+
68
+ # should redirect to the accounts page, and set this as the page to return to
69
+ redirect_uri = request.fullpath if request.get?
70
+ set_session_value(select_account_redirect_session_key, redirect_uri)
71
+ set_redirect_error_status(select_account_required_error_status)
72
+ set_redirect_error_flash require_select_account_error_flash
73
+ redirect select_account_path
74
+ end
75
+
76
+ def skip_login_field_on_login?
77
+ return super unless param_or_nil(login_param) && !field_error(login_param)
78
+
79
+ @valid_login_entered = true
80
+ true
81
+ end
82
+
83
+ private
84
+
85
+ # when selecting an account and requiring login, you'll want the user to go back to where it was
86
+ # before selecting accounts
87
+ def login_after_select_account_required
88
+ set_session_value(login_redirect_session_key, remove_session_value(select_account_redirect_session_key))
89
+ set_redirect_error_status(login_required_error_status)
90
+ set_redirect_error_flash require_login_error_flash
91
+ redirect require_login_redirect
92
+ end
93
+
94
+ # when selecting an account and requiring login, you'll want the user to go back to where it was
95
+ # before selecting accounts
96
+ def add_account_after_select_account_required
97
+ set_session_value(add_account_redirect_session_key, remove_session_value(select_account_redirect_session_key))
98
+ set_redirect_error_status(add_account_required_error_status)
99
+ set_redirect_error_flash require_add_account_error_flash
100
+ redirect(add_account_path + "?#{login_param}=#{param(login_param)}")
101
+ end
102
+
103
+ def after_login
104
+ super
105
+ _set_account_in_cookie
106
+ end
107
+
108
+ def _after_add_account
109
+ _set_account_in_cookie
110
+ end
111
+
112
+ def session
113
+ core_session = super
114
+ account_value = core_session[selected_account_key]
115
+
116
+ return core_session unless account_value
117
+
118
+ (core_session[accounts_key] ||= {})[account_value.to_s] ||= {}
119
+ end
120
+
121
+ def update_session
122
+ # clear_session
123
+ set_session_value(session_key, account_session_value)
124
+ end
125
+
126
+ def set_session_value(key, value)
127
+ return super unless key == session_key
128
+
129
+ full_scope = method(:session).super_method.call
130
+ full_scope[selected_account_key] = value
131
+ super
132
+ end
133
+
134
+ def authenticated_session?(account_id); end
135
+
136
+ def _get_accounts_in_session
137
+ ds = account_ds(_get_account_ids_in_session_and_cookies)
138
+ ds = ds.where(account_session_status_filter) unless skip_status_checks?
139
+ ds
140
+ end
141
+
142
+ def _get_account_ids_in_session_and_cookies
143
+ (_account_ids_from_session + _account_ids_from_cookie).uniq.map(&:to_i)
144
+ end
145
+
146
+ def _account_ids_from_session
147
+ account_ids = method(:session).super_method.call[accounts_key]
148
+ return [] unless account_ids
149
+
150
+ account_ids.reject do |_, session|
151
+ session.nil? || session.empty?
152
+ end.keys.map(&:to_i)
153
+ end
154
+
155
+ def _account_ids_from_cookie
156
+ if (in_cookies = request.cookies[accounts_cookie_key])
157
+ in_cookies.split(",").map(&:to_i)
158
+ else
159
+ []
160
+ end
161
+ end
162
+
163
+ def _set_account_in_cookie
164
+ accounts_cookie = _account_ids_from_cookie
165
+
166
+ return if accounts_cookie.include?(account_id)
167
+
168
+ accounts_cookie << account_id
169
+
170
+ opts = Hash[accounts_cookie_options]
171
+ opts[:value] = accounts_cookie.join(",")
172
+ opts[:expires] = Time.now + accounts_cookie_interval
173
+ ::Rack::Utils.set_cookie_header!(response.headers, accounts_cookie_key, opts)
174
+ end
175
+
176
+ def template_path(page)
177
+ path = File.join(File.dirname(__FILE__), "../../../templates", "#{page}.str")
178
+ return super unless File.exist?(path)
179
+
180
+ path
181
+ end
182
+
183
+ route(:select_account) do |r|
184
+ login_after_select_account_required if accounts_in_session.empty?
185
+ before_select_account_route
186
+
187
+ r.get do
188
+ select_account_view
189
+ end
190
+
191
+ r.post do
192
+ login_after_select_account_required unless logged_in?
193
+
194
+ catch_error do
195
+ @account = accounts_in_session.where(login_column => param(login_param)).first
196
+
197
+ throw_error_status(no_account_error_status, login_param, no_account_message) unless @account
198
+
199
+ unless _account_ids_from_session.include?(account_session_value)
200
+ # but he has no acknowledged session, redirect to /add-account
201
+ add_account_after_select_account_required
202
+ end
203
+
204
+ transaction do
205
+ before_select_account
206
+ # has a valid session, update session
207
+ update_session
208
+ after_select_account
209
+ end
210
+ set_notice_flash select_account_notice_flash
211
+ redirect(select_account_redirect)
212
+ end
213
+
214
+ set_error_flash select_account_error_flash
215
+ redirect(remove_session_value(select_account_redirect_session_key) || default_redirect)
216
+ end
217
+ end
218
+
219
+ # /add-account
220
+ route(:add_account) do |r|
221
+ require_account
222
+ before_add_account_route
223
+
224
+ r.get do
225
+ set_session_value(add_account_redirect_session_key, request.referer) if login_return_to_requested_location?
226
+ add_account_view
227
+ end
228
+
229
+ r.post do
230
+ # This is a copy of the login routine
231
+ skip_error_flash = false
232
+ view = :add_account
233
+
234
+ catch_error do
235
+ # this instruction will load the new account
236
+ unless account_from_login(param(login_param))
237
+ throw_error_status(no_matching_login_error_status, login_param, no_matching_login_message)
238
+ end
239
+
240
+ # this will add the same attempt constraints as login has
241
+ before_login_attempt
242
+
243
+ throw_error_status(unopen_account_error_status, login_param, unverified_account_message) unless open_account?
244
+
245
+ if use_multi_phase_login?
246
+ @valid_login_entered = true
247
+ view = :multi_phase_login_view
248
+
249
+ unless param_or_nil(password_param)
250
+ after_login_entered_during_multi_phase_login
251
+ skip_error_flash = true
252
+ next
253
+ end
254
+ end
255
+
256
+ unless password_match?(param(password_param))
257
+ after_login_failure
258
+ throw_error_status(login_error_status, password_param, invalid_password_message)
259
+ end
260
+
261
+ transaction do
262
+ before_add_account
263
+ login_session(accounts_key)
264
+ after_add_account
265
+ end
266
+ set_notice_flash add_account_notice_flash
267
+ redirect(remove_session_value(add_account_redirect_session_key) || add_account_redirect)
268
+ end
269
+
270
+ set_error_flash add_account_error_flash unless skip_error_flash
271
+ send("#{view}_view")
272
+ end
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rodauth"
4
+
5
+ require "rodauth/select-account/version"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rodauth
4
+ module SelectAccount
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rodauth-select-account
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tiago Cardoso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Multiple authenticated accounts per session in rodauth.
14
+ email:
15
+ - cardoso_tiago@hotmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - CHANGELOG.md
22
+ files:
23
+ - CHANGELOG.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - lib/rodauth/features/select_account.rb
27
+ - lib/rodauth/select-account.rb
28
+ - lib/rodauth/select-account/version.rb
29
+ homepage: https://gitlab.com/honeyryderchuck/rodauth-select-account
30
+ licenses: []
31
+ metadata:
32
+ homepage_uri: https://gitlab.com/honeyryderchuck/rodauth-select-account
33
+ source_code_uri: https://gitlab.com/honeyryderchuck/rodauth-select-account
34
+ changelog_uri: https://gitlab.com/honeyryderchuck/rodauth-select-account/-/blob/master/CHANGELOG.md
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.1.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Multiple authenticated accounts per session in rodauth.
54
+ test_files: []