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 ()
  lookup_accounts([]).first
end

#account_name_for(account_id) ⇒ Object



92
93
94
# File 'lib/prx_auth/rails/ext/controller.rb', line 92

def ()
  ().try(:[], "name")
end

#accounts_for(account_ids) ⇒ Object



100
101
102
# File 'lib/prx_auth/rails/ext/controller.rb', line 100

def accounts_for()
  lookup_accounts()
end

#after_sign_in_user_redirectObject



84
85
86
# File 'lib/prx_auth/rails/ext/controller.rb', line 84

def 
  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_userObject



50
51
52
# File 'lib/prx_auth/rails/ext/controller.rb', line 50

def current_user
  prx_auth_token
end

#current_user_appsObject



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 = (.try(:[], "apps") || []).map do |name, url|
    label = name.sub(/^https?:\/\//, "").sub(/\..+/, "").capitalize
    ["PRX #{label}", url]
  end

  # only return entire list in development
  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_infoObject



54
55
56
57
58
59
# File 'lib/prx_auth/rails/ext/controller.rb', line 54

def 
  session[] ||= begin
    info = fetch_userinfo
    info.slice("name", "preferred_username", "email", "image_href", "apps")
  end
end

#current_user_nameObject



61
62
63
# File 'lib/prx_auth/rails/ext/controller.rb', line 61

def current_user_name
  ["name"] || ["preferred_username"] || ["email"]
end

#prx_auth_needs_refresh?(jwt_ttl) ⇒ Boolean

Returns:

  • (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_tokenObject



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()
  session.delete()
  nil
end

#prx_authenticated?Boolean

Returns:

  • (Boolean)


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

def prx_authenticated?
  !!prx_auth_token
end

#prx_jwtObject



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_pathObject



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

def 
  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 (token)
  session[PRX_JWT_SESSION_KEY] = token
  accounts_for(current_user.resources)
end

#sign_out_userObject



88
89
90
# File 'lib/prx_auth/rails/ext/controller.rb', line 88

def sign_out_user
  reset_session
end