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 +4 -4
- data/lib/mn_utils_gem.rb +1 -0
- data/lib/mn_utils_gem/sso.rb +86 -0
- data/lib/mn_utils_gem/version.rb +1 -1
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc951b6447f533eb913ad2e69d94a30e18de54a44678fa5aec138d97aa891089
|
4
|
+
data.tar.gz: 37b4f3d995e9fd5cfe864ee3f3ce8841c31cff76a25b5d0fa0a6003d0e09529e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc4f6fe72838e4745875b2e3f2cb2809578eab86632df52242a5a0ee131671d02a8ddf8ba2b275bc05160bee4ba776f4253418ccf7ba892fbec2ced4883c0862
|
7
|
+
data.tar.gz: 85738b850340c890e770b32bb037bbeb2652def06b0259e91a081df8181c73da674a1539661088529dd5bf4b6c47656e61133aa652e990c29f6ba81f466dc472
|
data/lib/mn_utils_gem.rb
CHANGED
@@ -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
|
data/lib/mn_utils_gem/version.rb
CHANGED
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.
|
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-
|
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
|