Module: PrxAuth::Rails::Controller
- Defined in:
- lib/prx_auth/rails/ext/controller.rb
Defined Under Namespace
Classes: SessionTokenExpiredError
Constant Summary
collapse
- PRX_AUTH_ENV_KEY =
"prx.auth".freeze
- PRX_JWT_SESSION_KEY =
"prx.auth.jwt".freeze
- PRX_JWT_REFRESH_TTL =
subtracted from the JWT ttl
300
- PRX_ACCOUNT_MAPPING_SESSION_KEY =
"prx.auth.account.mapping".freeze
- PRX_USER_INFO_SESSION_KEY =
"prx.auth.info".freeze
- PRX_REFRESH_BACK_KEY =
"prx.auth.back".freeze
Instance Method Summary
collapse
Instance Method Details
#account_for(account_id) ⇒ Object
96
97
98
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 96
def account_for(account_id)
lookup_accounts([account_id]).first
end
|
#account_name_for(account_id) ⇒ Object
92
93
94
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 92
def account_name_for(account_id)
account_for(account_id).try(:[], "name")
end
|
#accounts_for(account_ids) ⇒ Object
100
101
102
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 100
def accounts_for(account_ids)
lookup_accounts(account_ids)
end
|
#after_sign_in_user_redirect ⇒ Object
84
85
86
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 84
def after_sign_in_user_redirect
session[PRX_REFRESH_BACK_KEY]
end
|
#authenticate! ⇒ Object
40
41
42
43
44
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 40
def authenticate!
return true if current_user.present?
redirect_to PrxAuth::Rails::Engine.routes.url_helpers.new_sessions_path
end
|
#current_user ⇒ Object
50
51
52
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 50
def current_user
prx_auth_token
end
|
#current_user_apps ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 65
def current_user_apps
apps = (current_user_info.try(:[], "apps") || []).map do |name, url|
label = name.sub(/^https?:\/\//, "").sub(/\..+/, "").capitalize
["PRX #{label}", url]
end
if ::Rails.env.production? || ::Rails.env.staging?
apps.to_h.select { |k, v| v.match?(/\.(org|tech)/) }
else
apps.to_h
end
end
|
#current_user_info ⇒ Object
54
55
56
57
58
59
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 54
def current_user_info
session[PRX_USER_INFO_SESSION_KEY] ||= begin
info = fetch_userinfo
info.slice("name", "preferred_username", "email", "image_href", "apps")
end
end
|
#current_user_name ⇒ Object
61
62
63
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 61
def current_user_name
current_user_info["name"] || current_user_info["preferred_username"] || current_user_info["email"]
end
|
#prx_auth_needs_refresh?(jwt_ttl) ⇒ Boolean
46
47
48
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 46
def prx_auth_needs_refresh?(jwt_ttl)
request.get? && jwt_ttl < PRX_JWT_REFRESH_TTL
end
|
#prx_auth_token ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 17
def prx_auth_token
env_token || session_token
rescue SessionTokenExpiredError
session.delete(PRX_JWT_SESSION_KEY)
session.delete(PRX_ACCOUNT_MAPPING_SESSION_KEY)
session.delete(PRX_USER_INFO_SESSION_KEY)
nil
end
|
#prx_authenticated? ⇒ Boolean
36
37
38
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 36
def prx_authenticated?
!!prx_auth_token
end
|
#prx_jwt ⇒ Object
32
33
34
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 32
def prx_jwt
session[PRX_JWT_SESSION_KEY]
end
|
#set_after_sign_in_path ⇒ Object
26
27
28
29
30
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 26
def set_after_sign_in_path
return if instance_of?(PrxAuth::Rails::SessionsController)
session[PRX_REFRESH_BACK_KEY] = request.fullpath
end
|
#sign_in_user(token) ⇒ Object
79
80
81
82
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 79
def sign_in_user(token)
session[PRX_JWT_SESSION_KEY] = token
accounts_for(current_user.resources)
end
|
#sign_out_user ⇒ Object
88
89
90
|
# File 'lib/prx_auth/rails/ext/controller.rb', line 88
def sign_out_user
reset_session
end
|