Module: PrxAuth::Rails::Controller

Extended by:
ActiveSupport::Concern
Includes:
AccountInfo, UserInfo
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 =
60
PRX_REFRESH_BACK_KEY =
"prx.auth.back".freeze

Constants included from UserInfo

UserInfo::PRX_ADMIN_SCOPE, UserInfo::PRX_USER_INFO_SESSION_KEY

Constants included from AccountInfo

AccountInfo::PRX_ACCOUNT_MAPPING_SESSION_KEY

Instance Method Summary collapse

Methods included from UserInfo

#current_user, #current_user_access?, #current_user_admin?, #current_user_apps, #current_user_info, #current_user_name, #current_user_wildcard?

Methods included from AccountInfo

#account_for, #account_name_for, #accounts_for

Instance Method Details

#after_sign_in_user_redirectObject



72
73
74
# File 'lib/prx_auth/rails/ext/controller.rb', line 72

def 
  session[PRX_REFRESH_BACK_KEY]
end

#authenticate!Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/prx_auth/rails/ext/controller.rb', line 45

def authenticate!
  if !current_user
    
    redirect_to new_sessions_path
  elsif !current_user_access?
    redirect_to access_error_sessions_path
  else
    true
  end
end

#prx_auth_needs_refresh?(jwt_ttl) ⇒ Boolean

trigger refresh on a non-turbo GET request, if possible

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
# File 'lib/prx_auth/rails/ext/controller.rb', line 57

def prx_auth_needs_refresh?(jwt_ttl)
  if jwt_ttl < 0
    true
  elsif jwt_ttl < PRX_JWT_REFRESH_TTL
    request.get? && !request.headers["Turbo-Frame"]
  else
    false
  end
end

#prx_auth_tokenObject



24
25
26
27
28
29
30
31
# File 'lib/prx_auth/rails/ext/controller.rb', line 24

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/prx_auth/rails/ext/controller.rb', line 41

def prx_authenticated?
  !!prx_auth_token
end

#prx_jwtObject



37
38
39
# File 'lib/prx_auth/rails/ext/controller.rb', line 37

def prx_jwt
  session[PRX_JWT_SESSION_KEY]
end

#set_after_sign_in_path(path = nil) ⇒ Object



33
34
35
# File 'lib/prx_auth/rails/ext/controller.rb', line 33

def (path = nil)
  session[PRX_REFRESH_BACK_KEY] = path || request.fullpath
end

#sign_in_user(token) ⇒ Object



67
68
69
70
# File 'lib/prx_auth/rails/ext/controller.rb', line 67

def (token)
  session[PRX_JWT_SESSION_KEY] = token
  accounts_for(current_user.resources)
end

#sign_out_userObject



76
77
78
# File 'lib/prx_auth/rails/ext/controller.rb', line 76

def sign_out_user
  reset_session
end