11class OrcidController < ApplicationController
2+ include SpaceRedirect
3+
24 before_action :orcid_auth_enabled
35 before_action :authenticate_user!
46 before_action :set_oauth_client , only : [ :authenticate , :callback ]
@@ -9,12 +11,17 @@ class OrcidController < ApplicationController
911 end
1012
1113 def authenticate
12- redirect_to @oauth2_client . authorization_uri ( scope : '/authenticate' ) , allow_other_host : true
14+ params = Space . current_space &.default? ? { } : { state : "space_id:#{ Space . current_space . id } " }
15+ redirect_to @oauth2_client . authorization_uri ( scope : '/authenticate' , **params ) , allow_other_host : true
1316 end
1417
1518 def callback
1619 @oauth2_client . authorization_code = params [ :code ]
1720 token = Rack ::OAuth2 ::AccessToken ::Bearer . new ( access_token : @oauth2_client . access_token! )
21+ if params [ :state ] . present?
22+ m = params [ :state ] . match ( /space_id:(\d +)/ )
23+ space = Space . find_by_id ( m [ 1 ] ) if m
24+ end
1825 orcid = token . access_token &.raw_attributes [ 'orcid' ]
1926 respond_to do |format |
2027 profile = current_user . profile
@@ -27,7 +34,7 @@ def callback
2734 else
2835 flash [ :error ] = t ( 'orcid.authentication_failure' )
2936 end
30- format . html { redirect_to current_user }
37+ format . html { redirect_to_space ( user_path ( current_user ) , space ) }
3138 end
3239 end
3340
@@ -38,7 +45,7 @@ def set_oauth_client
3845 @oauth2_client ||= Rack ::OAuth2 ::Client . new (
3946 identifier : config [ :client_id ] ,
4047 secret : config [ :secret ] ,
41- redirect_uri : config [ :redirect_uri ] . presence || orcid_callback_url ,
48+ redirect_uri : config [ :redirect_uri ] . presence || orcid_callback_url ( host : TeSS :: Config . base_uri . host ) ,
4249 authorization_endpoint : '/oauth/authorize' ,
4350 token_endpoint : '/oauth/token' ,
4451 host : config [ :host ] . presence || ( Rails . env . production? ? 'orcid.org' : 'sandbox.orcid.org' )
0 commit comments