3
3
module Users
4
4
# Controller that handles callbacks from OmniAuth integrations (e.g. Shibboleth and ORCID)
5
5
class OmniauthCallbacksController < Devise ::OmniauthCallbacksController
6
- ##
7
- # Dynamically build a handler for each omniauth provider
8
- # -------------------------------------------------------------
9
- IdentifierScheme . for_authentication . each do |scheme |
10
- define_method ( scheme . name . downcase ) do
11
- handle_omniauth ( scheme )
12
- end
13
- end
14
-
15
-
16
- # def openid_connect
17
- # @user = User.from_omniauth(request.env["omniauth.auth"])
18
-
19
- # if @user.present?
20
- # sign_in_and_redirect @user, event: :authentication
21
- # set_flash_message(:notice, :success, kind: "OpenID Connect") if is_navigational_format?
22
- # else
23
- # session["devise.openid_connect_data"] = request.env["omniauth.auth"]
24
- # redirect_to new_user_registration_url
25
- # end
26
- # end
27
-
28
-
29
-
30
-
31
- #This is for the OpenidConnect CILogon
32
-
6
+ # This is for the OpenidConnect CILogon
33
7
def openid_connect
34
8
# First or create
35
9
auth = request . env [ 'omniauth.auth' ]
36
10
user = User . from_omniauth ( auth )
37
- identifier_scheme = IdentifierScheme . find_by_name ( auth . provider )
38
11
39
12
if auth . info . email . nil? && user . nil?
40
- #If email is missing we need to request the user to register with DMP.
41
- #User email can be missing if the user email id is set to private or trusted clients only we won't get the value.
42
- #USer email id is one of the mandatory field which is must required.
43
- flash [ :notice ] = 'Please try sign-up with DMP assistant.'
13
+ # If email is missing we need to request the user to register with DMP.
14
+ # User email can be missing if the user email id is set to private or
15
+ # trusted clients only we won't get the value.
16
+ # User email id is one of the mandatory field which is must required.
17
+
18
+ flash [ :notice ] =
19
+ "Your institution's current settings do not provide an email address, which is necessary for registration. " \
20
+ 'To proceed, please update your settings to make your email address visible. Alternatively, you can ' \
21
+ 'create an account directly on DMP Assistant.'
44
22
redirect_to new_user_registration_path
45
- elsif current_user . nil?
23
+ end
24
+
25
+ identifier_scheme = IdentifierScheme . find_by_name ( auth . provider )
26
+
27
+ if current_user . nil?
46
28
# We need to register
47
29
if user . nil?
48
30
# Register and sign in
49
31
user = User . create_from_provider_data ( auth )
50
- Identifier . create ( identifier_scheme : identifier_scheme , #auth.provider, #scheme, #IdentifierScheme.last.id,
32
+ Identifier . create ( identifier_scheme : identifier_scheme , # auth.provider, #scheme, #IdentifierScheme.last.id,
51
33
value : auth . uid ,
52
34
attrs : auth ,
53
35
identifiable : user )
@@ -59,15 +41,13 @@ def openid_connect
59
41
Identifier . create ( identifier_scheme : identifier_scheme ,
60
42
value : auth . uid ,
61
43
attrs : auth ,
62
- identifiable : current_user )
44
+ identifiable : user )
63
45
64
46
flash [ :notice ] = 'linked succesfully'
65
47
redirect_to root_path
66
48
end
67
49
end
68
50
69
-
70
-
71
51
# Processes callbacks from an omniauth provider and directs the user to
72
52
# the appropriate page:
73
53
# Not logged in and uid had no match ---> Sign Up page
@@ -82,33 +62,23 @@ def openid_connect
82
62
def handle_omniauth ( scheme )
83
63
user = if request . env [ 'omniauth.auth' ] . nil?
84
64
User . from_omniauth ( request . env )
85
- else
86
- User . from_omniauth ( request . env [ 'rack.session' ] )
65
+ else
66
+ User . from_omniauth ( request . env [ 'omniauth.auth' ] )
87
67
end
88
68
89
69
# If the user isn't logged in
90
70
if current_user . nil?
91
71
# If the uid didn't have a match in the system send them to register
92
72
if user . nil?
93
73
session [ "devise.#{ scheme . name . downcase } _data" ] = request . env [ 'omniauth.auth' ]
74
+
94
75
redirect_to new_user_registration_url
95
76
96
77
# Otherwise sign them in
97
78
elsif scheme . name == 'shibboleth'
98
79
# Until ORCID becomes supported as a login method
99
80
set_flash_message ( :notice , :success , kind : scheme . description ) if is_navigational_format?
100
81
sign_in_and_redirect user , event : :authentication
101
- elsif schema . name == "openid_connect"
102
- @user = User . from_omniauth ( request . env [ "omniauth.auth" ] )
103
- Rails . logger . info "OmniAuth Auth Hash: #{ request . env [ "omniauth.auth" ] } "
104
-
105
- if @user . persisted?
106
- sign_in_and_redirect @user , event : :authentication
107
- set_flash_message ( :notice , :success , kind : "OpenID Connect" ) if is_navigational_format?
108
- else
109
- session [ "devise.openid_connect_data" ] = request . env [ "omniauth.auth" ]
110
- redirect_to new_user_registration_url
111
- end
112
82
else
113
83
flash [ :notice ] = _ ( 'Successfully signed in' )
114
84
redirect_to new_user_registration_url
@@ -119,13 +89,12 @@ def handle_omniauth(scheme)
119
89
# If the user could not be found by that uid then attach it to their record
120
90
if user . nil?
121
91
if Identifier . create ( identifier_scheme : scheme ,
122
- value : request . env [ 'rack.session' ] [ 'omniauth.state' ] , #request.env[' omniauth.auth'].uid,
123
- attrs : request . env [ 'rack.session' ] [ 'omniauth.nonce' ] , #request.env[' omniauth.auth'],
92
+ value : request . env [ 'omniauth.auth' ] . uid ,
93
+ attrs : request . env [ 'omniauth.auth' ] ,
124
94
identifiable : current_user )
125
95
flash [ :notice ] =
126
96
format ( _ ( 'Your account has been successfully linked to %{scheme}.' ) ,
127
97
scheme : scheme . description )
128
- redirect_to new_user_registration_url
129
98
130
99
else
131
100
flash [ :alert ] = format ( _ ( 'Unable to link your account to %{scheme}.' ) ,
@@ -145,7 +114,13 @@ def handle_omniauth(scheme)
145
114
end
146
115
end
147
116
117
+ def orcid
118
+ handle_omniauth ( IdentifierScheme . for_authentication . find_by ( name : 'orcid' ) )
119
+ end
148
120
121
+ def shibboleth
122
+ handle_omniauth ( IdentifierScheme . for_authentication . find_by ( name : 'shibboleth' ) )
123
+ end
149
124
150
125
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
151
126
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
0 commit comments