session-validator-client 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.env +3 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +13 -0
- data/Gemfile +3 -0
- data/README.md +30 -0
- data/bin/console +10 -0
- data/lib/session_validator.rb +9 -0
- data/lib/session_validator/cached_client.rb +19 -0
- data/lib/session_validator/client.rb +59 -0
- data/lib/session_validator/in_memory_cache.rb +35 -0
- data/session-validator-client.gemspec +25 -0
- metadata +16 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1816744639488cc1585a2086e282cc605336199ad25b15f37dbd9813c1eb2db
|
4
|
+
data.tar.gz: e6d8d97f3d41daa67723f0e052ec48cbdfe2f4add832f368085368f76d20e3b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b99f8eb8174d308b0aa05620bd7036ff8a693bf8ec37604a8ae69033e9ca27b9251276b6a5baa9d02e79b517ceba58f2f5b9a95895bb949417978a69af9633d0
|
7
|
+
data.tar.gz: 9c6363f1a4e1654f3b327f69d0a6dc294b0ef70dec3d81db8f29504c55716e767aa3b4f71bed858a6abad2e05fbd4a5325bb5176e9e7ca3f665049d66d2fd9b2
|
data/.env
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# session-validator-client-ruby 
|
2
|
+
|
3
|
+
Ruby client for Emarsys session validator service.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install session-validator-client
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
setup up the following environment variables:
|
14
|
+
|
15
|
+
* `KEY_POOL`
|
16
|
+
* `SESSION_VALIDATOR_KEYID`
|
17
|
+
* `SESSION_VALIDATOR_URL`
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require "session_validator"
|
21
|
+
|
22
|
+
client = SessionValidator::Client.new
|
23
|
+
client.valid?("staging_int_5ad5f96f307cf9.61063404")
|
24
|
+
```
|
25
|
+
|
26
|
+
## Running tests
|
27
|
+
|
28
|
+
```bash
|
29
|
+
rspec
|
30
|
+
```
|
data/bin/console
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module SessionValidator
|
2
|
+
class CachedClient
|
3
|
+
def initialize(client, cache)
|
4
|
+
@client = client
|
5
|
+
@cache = cache
|
6
|
+
end
|
7
|
+
|
8
|
+
def valid?(msid)
|
9
|
+
@cache.cleanup
|
10
|
+
|
11
|
+
cached_result = @cache.get msid
|
12
|
+
return cached_result if cached_result
|
13
|
+
|
14
|
+
@client.valid?(msid).tap do |result|
|
15
|
+
@cache.set msid, result if result
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "escher-keypool"
|
3
|
+
require "faraday"
|
4
|
+
require "faraday_middleware"
|
5
|
+
require "faraday_middleware/escher"
|
6
|
+
|
7
|
+
class SessionValidator::Client
|
8
|
+
CREDENTIAL_SCOPE = "eu/session-validator/ems_request".freeze
|
9
|
+
ESCHER_AUTH_OPTIONS = {
|
10
|
+
algo_prefix: "EMS",
|
11
|
+
vendor_key: "EMS",
|
12
|
+
auth_header_name: "X-Ems-Auth",
|
13
|
+
date_header_name: "X-Ems-Date"
|
14
|
+
}.freeze
|
15
|
+
SERVICE_REQUEST_TIMEOUT = 2.freeze
|
16
|
+
|
17
|
+
def valid?(msid)
|
18
|
+
response_status = client.get("/sessions/#{msid}", nil, headers).status
|
19
|
+
(200..299).include?(response_status) || (500..599).include?(response_status)
|
20
|
+
rescue Faraday::TimeoutError
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def client
|
27
|
+
Faraday.new(url) do |faraday|
|
28
|
+
faraday.options[:open_timeout] = SERVICE_REQUEST_TIMEOUT
|
29
|
+
faraday.options[:timeout] = SERVICE_REQUEST_TIMEOUT
|
30
|
+
faraday.use FaradayMiddleware::Escher::RequestSigner, escher_config
|
31
|
+
faraday.adapter Faraday.default_adapter
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def url
|
36
|
+
uri.to_s
|
37
|
+
end
|
38
|
+
|
39
|
+
def host
|
40
|
+
uri.hostname
|
41
|
+
end
|
42
|
+
|
43
|
+
def uri
|
44
|
+
URI.parse(SessionValidator.base_url)
|
45
|
+
end
|
46
|
+
|
47
|
+
def escher_config
|
48
|
+
{
|
49
|
+
credential_scope: CREDENTIAL_SCOPE,
|
50
|
+
host: host,
|
51
|
+
options: ESCHER_AUTH_OPTIONS,
|
52
|
+
active_key: -> { ::Escher::Keypool.new.get_active_key("session_validator") }
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def headers
|
57
|
+
{ "content-type" => "application/json" }
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module SessionValidator
|
2
|
+
class InMemoryCache
|
3
|
+
def initialize(ttl)
|
4
|
+
@ttl = ttl
|
5
|
+
@cache = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(key)
|
9
|
+
return nil unless @cache.key? key
|
10
|
+
|
11
|
+
data = @cache[key]
|
12
|
+
return nil if expired? data
|
13
|
+
|
14
|
+
data[:value]
|
15
|
+
end
|
16
|
+
|
17
|
+
def set(key, value)
|
18
|
+
@cache[key] = { value: value, expiry: Time.now.to_i + @ttl }
|
19
|
+
end
|
20
|
+
|
21
|
+
def cleanup
|
22
|
+
@cache.delete_if { |_, data| expired? data }
|
23
|
+
end
|
24
|
+
|
25
|
+
def empty?
|
26
|
+
@cache.length == 0
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def expired?(data)
|
32
|
+
Time.now.to_i >= data[:expiry]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "session-validator-client"
|
3
|
+
s.version = "3.0.1"
|
4
|
+
s.summary = "Ruby client for Emarsys session validator service"
|
5
|
+
s.authors = ["Emarsys Technologies Ltd."]
|
6
|
+
s.email = "security@emarsys.com"
|
7
|
+
s.homepage = "https://github.com/emartech/session-validator-client-ruby"
|
8
|
+
s.licenses = ["MIT"]
|
9
|
+
|
10
|
+
s.required_ruby_version = ">= 1.9"
|
11
|
+
|
12
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
13
|
+
f.match(%r{^(test|spec|features)/})
|
14
|
+
end
|
15
|
+
s.require_paths = ['lib']
|
16
|
+
|
17
|
+
s.add_dependency "escher-keypool"
|
18
|
+
s.add_dependency "faraday"
|
19
|
+
s.add_dependency "faraday_middleware"
|
20
|
+
s.add_dependency "faraday_middleware-escher"
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "webmock"
|
24
|
+
s.add_development_dependency "dotenv"
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: session-validator-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emarsys Technologies Ltd.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: escher-keypool
|
@@ -113,8 +113,20 @@ email: security@emarsys.com
|
|
113
113
|
executables: []
|
114
114
|
extensions: []
|
115
115
|
extra_rdoc_files: []
|
116
|
-
files:
|
117
|
-
|
116
|
+
files:
|
117
|
+
- ".env"
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- README.md
|
123
|
+
- bin/console
|
124
|
+
- lib/session_validator.rb
|
125
|
+
- lib/session_validator/cached_client.rb
|
126
|
+
- lib/session_validator/client.rb
|
127
|
+
- lib/session_validator/in_memory_cache.rb
|
128
|
+
- session-validator-client.gemspec
|
129
|
+
homepage: https://github.com/emartech/session-validator-client-ruby
|
118
130
|
licenses:
|
119
131
|
- MIT
|
120
132
|
metadata: {}
|