prx_auth-rails 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3405e4e72d2c39585111bef4fca339a73ce11ec86a2c288f5c82d8934c0620fe
4
- data.tar.gz: 3450063f4c4fdae2464f755222fafee08c627eda3aa8fee1d267db1474db58de
3
+ metadata.gz: 16f02bc649e6a16709b4a504649a925d3ead05209f8b3bd5c5e4aa28416bc57e
4
+ data.tar.gz: fbc0d03cd674fa514541f23d9c6da6ac46db9031c110ddc3f604e66e073c9a8e
5
5
  SHA512:
6
- metadata.gz: edf33f5cf4bc105818bb069554506f0b2d9ef5b8bb18cd1602b2ceb6384991804b5b3268c6bd5c519c08e8503df51c132a9bec045f13995c1d55088194fff489
7
- data.tar.gz: 4aa62706ee892c2335756e2c3ae61171b77acc69bfe0ec06433449726f8e884cee4af2031e55e418c6780e9aedc8bfb1c78359c072c089389422a2d4aa15c95b
6
+ metadata.gz: 0d16efe0713bb453b5a4238f0ff6983898b5b5872d07396be5c66cf88ca7f4e2f23834a1abc8462c9a2cf0e8f40f1416d7e8cf82ec48611492deddcd52b8812c
7
+ data.tar.gz: d3eacf2afcc2b9af1a741988bdb8e70c7c664672c487c6515ca336e65ac5f3db42adf63fc05b6fe89b39ef2ff6c45ad1e8dccaa0e5204b3bfba50d2388e6a07f
@@ -1,5 +1,3 @@
1
- require 'open-uri'
2
-
3
1
  module PrxAuth::Rails
4
2
  class SessionsController < ApplicationController
5
3
  include PrxAuth::Rails::Engine.routes.url_helpers
@@ -42,6 +40,7 @@ module PrxAuth::Rails
42
40
  result_path = if valid_nonce?(jwt_id_claims['nonce']) &&
43
41
  users_match?(jwt_id_claims, jwt_access_claims)
44
42
  sign_in_user(jwt_access_claims)
43
+ lookup_and_register_accounts_names
45
44
  after_sign_in_path_for(current_user)
46
45
  else
47
46
  auth_error_sessions_path(error: 'verification_failed')
@@ -1,5 +1,9 @@
1
1
  module PrxAuth
2
2
  module Rails
3
- class Engine < ::Rails::Engine; end
3
+ class Engine < ::Rails::Engine
4
+ config.to_prepare do
5
+ ::ApplicationController.helper_method [:current_user, :account_name_for]
6
+ end
7
+ end
4
8
  end
5
9
  end
@@ -1,8 +1,12 @@
1
1
  require 'prx_auth/rails/token'
2
+ require 'open-uri'
2
3
 
3
4
  module PrxAuth
4
5
  module Rails
5
6
  module Controller
7
+
8
+ PRX_ACCOUNT_NAME_MAPPING_KEY = 'prx.account.name.mapping'.freeze
9
+
6
10
  def prx_auth_token
7
11
  rack_auth_token = env_prx_auth_token
8
12
  return rack_auth_token if rack_auth_token.present?
@@ -26,12 +30,53 @@ module PrxAuth
26
30
  PrxAuth::Rails::Token.new(prx_auth_token)
27
31
  end
28
32
 
33
+ def lookup_and_register_accounts_names
34
+ session[PRX_ACCOUNT_NAME_MAPPING_KEY] =
35
+ lookup_account_names_mapping
36
+ end
37
+
38
+ def account_name_for(id)
39
+ id = id.to_i
40
+
41
+ name =
42
+ if session[PRX_ACCOUNT_NAME_MAPPING_KEY].has_key?(id)
43
+ session[PRX_ACCOUNT_NAME_MAPPING_KEY][id]
44
+ else
45
+ session[PRX_ACCOUNT_NAME_MAPPING_KEY][id] = lookup_account_name_for(id)
46
+ end
47
+
48
+ name = "[#{id}] Unknown Account Name" unless name.present?
49
+
50
+ name
51
+ end
52
+
29
53
  def sign_in_user(token)
30
54
  session['prx.auth'] = token
31
55
  end
32
56
 
33
57
  private
34
58
 
59
+ def lookup_account_name_for(id)
60
+ id = id.to_i
61
+
62
+ res = lookup_account_names_mapping([id])
63
+ res[id]
64
+ end
65
+
66
+ def lookup_account_names_mapping(ids=current_user.resources)
67
+ id_host = PrxAuth::Rails.configuration.id_host
68
+ ids_param = ids.map(&:to_s).join(',')
69
+
70
+ options = {}
71
+ options[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE if ::Rails.env.development?
72
+
73
+ accounts = URI.open("https://#{id_host}/api/v1/accounts?account_ids=#{ids_param}", options).read
74
+
75
+ mapping = JSON.parse(accounts)['accounts'].map { |acct| [acct['id'], acct['display_name']] }.to_h
76
+
77
+ mapping
78
+ end
79
+
35
80
  def env_prx_auth_token
36
81
  if !defined? @_prx_auth_token
37
82
  @_prx_auth_token = request.env['prx.auth'] && PrxAuth::Rails::Token.new(request.env['prx.auth'])
@@ -1,5 +1,5 @@
1
1
  module PrxAuth
2
2
  module Rails
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prx_auth-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Rhoden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-19 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack