mn_utils_gem 1.12.0 → 1.13.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: c6daecaf1ae8bec82271e17084b64c76fdb4a2407061313355478e6715159c7c
4
- data.tar.gz: 23919160053b06362060c66ea94c90d035b02df313a4f011a614efc179de2269
3
+ metadata.gz: cc951b6447f533eb913ad2e69d94a30e18de54a44678fa5aec138d97aa891089
4
+ data.tar.gz: 37b4f3d995e9fd5cfe864ee3f3ce8841c31cff76a25b5d0fa0a6003d0e09529e
5
5
  SHA512:
6
- metadata.gz: 429b621b31b51360587957429c9628bd9b7e569faef56c2bc2c643ce96f940b3b909a4925960be58622db3f4ecca60cf1df721efb7667c698482375737da99fc
7
- data.tar.gz: '099451860af73ff9bc6a05c0f23bb32f57dcdcd717916dd839a278746a6274da5ce3b85a46c164878ebff246ad7bf8c8315c885a356514558cd40ff87f51d862'
6
+ metadata.gz: dc4f6fe72838e4745875b2e3f2cb2809578eab86632df52242a5a0ee131671d02a8ddf8ba2b275bc05160bee4ba776f4253418ccf7ba892fbec2ced4883c0862
7
+ data.tar.gz: 85738b850340c890e770b32bb037bbeb2652def06b0259e91a081df8181c73da674a1539661088529dd5bf4b6c47656e61133aa652e990c29f6ba81f466dc472
data/lib/mn_utils_gem.rb CHANGED
@@ -2,6 +2,7 @@ require "mn_utils_gem/version"
2
2
  require "mn_utils_gem/site_action"
3
3
  require "mn_utils_gem/gui"
4
4
  require "mn_utils_gem/transactional_email"
5
+ require "mn_utils_gem/sso"
5
6
 
6
7
  module MnUtilsGem
7
8
  class Error < StandardError; end
@@ -0,0 +1,86 @@
1
+ require 'singleton'
2
+ require 'json'
3
+
4
+ module MnUtilsAuth
5
+ class Sso
6
+
7
+ include Singleton
8
+
9
+ SSO_COOKIE_NAME = :mnsso.freeze
10
+ SSO_COOKIE_VALUE_PREFIX = "#{SSO_COOKIE_NAME}_".freeze
11
+
12
+ def initialize
13
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
14
+ end
15
+
16
+ def get_sso_user_id(cookies)
17
+ parsed = get_sso cookies
18
+ return nil if parsed.nil?
19
+ parsed[:user_id]
20
+ rescue Exception => e
21
+ @logger.error e
22
+ nil
23
+ end
24
+
25
+ def get_sso(cookies)
26
+ return nil if cookies.nil? || cookies[SSO_COOKIE_NAME].nil? || redis_server.nil?
27
+ json = redis_server.get(cookies[SSO_COOKIE_NAME].to_s)
28
+ return nil if json.nil?
29
+ parsed = JSON.parse(json, symbolize_names: true)
30
+ return nil unless parsed.is_a?(Hash)
31
+ parsed
32
+ rescue Exception => e
33
+ @logger.error e
34
+ nil
35
+ end
36
+
37
+ def set_sso(cookies, user_id, other_attributes = {})
38
+ return if cookies.nil? || user_id.blank? || redis_server.nil?
39
+ if cookies[SSO_COOKIE_NAME].nil?
40
+ cookie = set_sso_cookie cookies
41
+ redis_server.set(cookie, JSON[other_attributes.reverse_merge({ user_id: user_id })])
42
+ end
43
+ end
44
+
45
+ def delete_sso(cookies)
46
+ if cookies[SSO_COOKIE_NAME]
47
+ redis_server.del(cookies[SSO_COOKIE_NAME])
48
+ cookies.delete SSO_COOKIE_NAME
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def set_sso_cookie(cookies)
55
+ cookies[SSO_COOKIE_NAME] = {
56
+ value: cookie_value,
57
+ expires: 1.year.from_now,
58
+ secure: true,
59
+ httponly: true
60
+ }
61
+ end
62
+
63
+ def redis_server
64
+ @redis_server ||= Redis.new(
65
+ url: ENV['MN_REDIS_URL'],
66
+ ssl: SharedService.string_to_bool(ENV['MN_REDIS_SSL']),
67
+ semian: {
68
+ name: 'user-service',
69
+ tickets: 4,
70
+ success_threshold: 1,
71
+ error_threshold: 4,
72
+ error_timeout: 10,
73
+ bulkhead: false
74
+ }
75
+ )
76
+ rescue StandardError => e
77
+ @logger.error e
78
+ nil
79
+ end
80
+
81
+ def cookie_value
82
+ "#{SSO_COOKIE_VALUE_PREFIX}#{SecureRandom.uuid}"
83
+ end
84
+
85
+ end
86
+ end
@@ -1,3 +1,3 @@
1
1
  module MnUtilsGem
2
- VERSION = "1.12.0"
2
+ VERSION = "1.13.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mn_utils_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.0
4
+ version: 1.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shamim Mirzai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-17 00:00:00.000000000 Z
11
+ date: 2019-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gelf
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: semian
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: bundler
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +173,7 @@ files:
145
173
  - lib/mn_utils_gem.rb
146
174
  - lib/mn_utils_gem/gui.rb
147
175
  - lib/mn_utils_gem/site_action.rb
176
+ - lib/mn_utils_gem/sso.rb
148
177
  - lib/mn_utils_gem/transactional_email.rb
149
178
  - lib/mn_utils_gem/version.rb
150
179
  homepage: https://github.com/mumsnet/mn_utils_gem