session-validator-client 5.1.0 → 6.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d091acd762f22b286f6c92cde9e72642b8ff3919cbbc0df99e48a159c111d65a
4
- data.tar.gz: af944e85aa7a0199f1ec0d115582046826f838124e04440e4106550512cde3c3
3
+ metadata.gz: 2498a668e8f669382a1a88f6555356823e7115d6978d8c9331bf84d1395190cc
4
+ data.tar.gz: 2ea169bd1b520056db1b7cc8fd65c0bac5aa206c8e7899e533267c104bec45e4
5
5
  SHA512:
6
- metadata.gz: 2bb739c2ae71f993ed3e7ed6a1309b8be0e25163afd858d09960071ba737e9ea380e2ab720ae7cd5993cc37307497f9311c8291761226dcc9ce46940300df310
7
- data.tar.gz: 44cfd9d65bd547f96895971d5f796568a30a3e42115eb457a0a28b18e8ba717d573601bee8fa85428e85ad45c08b61d42758820bf771038b1149e17ec883c320
6
+ metadata.gz: 2e0884a245a05850cc5412fe3f710ca875467093431f4514d591bcfcc9a8c8fc4373cede425204ccf80268e52c7f2ac9f3440fcb66bf32ab37fa4dba63833575
7
+ data.tar.gz: 023273a70c9d99daa8768ba44b1e50abc8cb8e76f39991e3def548f4bcebecf2771c704f9ffbb9e142cedb16c5cdee995ba1037b17a1ba39e276bb9ef2453e56
@@ -7,10 +7,10 @@ jobs:
7
7
  runs-on: ubuntu-latest
8
8
  strategy:
9
9
  matrix:
10
- ruby-versions: ['2.6', '2.7', '3.0']
10
+ ruby-versions: ['3.2', '3.3', '3.4']
11
11
 
12
12
  steps:
13
- - uses: actions/checkout@v2
13
+ - uses: actions/checkout@v4
14
14
  - uses: ruby/setup-ruby@v1
15
15
  with:
16
16
  ruby-version: ${{ matrix.ruby-versions }}
@@ -19,7 +19,7 @@ jobs:
19
19
  - name: Run tests
20
20
  run: bundle exec rspec
21
21
  - name: Deploy
22
- if : github.event_name == 'push' && startsWith(github.ref, 'refs/tags') && matrix.ruby-versions == '3.0'
22
+ if : github.event_name == 'push' && startsWith(github.ref, 'refs/tags') && matrix.ruby-versions == '3.2'
23
23
  run: |
24
24
  mkdir -p $HOME/.gem
25
25
  touch $HOME/.gem/credentials
@@ -28,4 +28,4 @@ jobs:
28
28
  gem build *.gemspec
29
29
  gem push *.gem
30
30
  env:
31
- GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
31
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  *.gem
2
2
  Gemfile.lock
3
3
  .ruby-version
4
+ .env
5
+ .bundle
data/Dockerfile ADDED
@@ -0,0 +1,20 @@
1
+ FROM ruby:3.2-alpine
2
+
3
+ RUN addgroup -g 1000 ruby && \
4
+ adduser -u 1000 -G ruby -s /bin/sh -D ruby && \
5
+ mkdir /app && \
6
+ chown ruby:ruby /app
7
+
8
+ RUN apk update && \
9
+ apk upgrade
10
+ RUN apk add --virtual .build-deps build-base git
11
+
12
+ RUN gem update --system && \
13
+ gem update && \
14
+ gem cleanup && \
15
+ gem install bundler
16
+
17
+ USER ruby
18
+ WORKDIR /app
19
+
20
+ RUN bundle config --local path .bundle
data/Makefile ADDED
@@ -0,0 +1,6 @@
1
+ .PHONY: build install test sh
2
+
3
+ build: ; docker compose build
4
+ install: ; docker compose run --rm app bundle install
5
+ test: ; docker compose run --rm app bundle exec rspec
6
+ sh: ; docker compose run --rm app sh
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # session-validator-client-ruby ![Build status](https://travis-ci.org/emartech/session-validator-client-ruby.svg?branch=master)
1
+ # Session Validator Client Ruby ![Build Status](https://github.com/emartech/session-validator-client-ruby/actions/workflows/ruby.yml/badge.svg)
2
2
 
3
3
  Ruby client for Emarsys session validator service.
4
4
 
@@ -10,35 +10,38 @@ gem install session-validator-client
10
10
 
11
11
  ## Usage
12
12
 
13
- Setup the following environment variables:
14
-
15
- * `KEY_POOL`
16
- * `SESSION_VALIDATOR_KEYID`
17
- * `SESSION_VALIDATOR_URL`
18
-
19
- ### Validating a single Msid
20
- `valid?(msid)` returns `true` if `msid` is valid
13
+ Copy `.env.example` to `.env` and set the necessary values for usage in your service.
21
14
 
15
+ ### Create client
22
16
  ```ruby
23
17
  require "session_validator"
24
18
 
25
19
  client = SessionValidator::Client.new
26
- client.valid?("staging_int_5ad5f96f307cf9.61063404")
27
20
  ```
28
21
 
29
- ### Batch validating multiple MSIDS
30
- `filter_invalid(msids)` returns an array of the invalid MSIDS.
22
+ ### Requests without Escher
23
+ For mTLS on GAP.
31
24
 
32
25
  ```ruby
33
26
  require "session_validator"
34
27
 
35
- client = SessionValidator::Client.new
28
+ client = SessionValidator::Client.new(use_escher: false)
29
+ ```
30
+
31
+ ### Validating a single MSID
32
+ ```ruby
33
+ client.valid?("staging_int_5ad5f96f307cf9.61063404")
34
+ ```
35
+
36
+ ### Batch validating multiple MSIDs
37
+ Returns an array of the invalid MSIDs.
38
+ ```ruby
36
39
  client.filter_invalid(["staging_int_5ad5f96f307cf9.61063404", "staging_int_5ad5f96f307cf9.61063405"])
37
40
  ```
38
41
 
39
- ## Running tests
42
+ ## Local development
40
43
 
44
+ ### Running tests
41
45
  ```bash
42
- $ bundle install
43
- $ rspec
46
+ make test
44
47
  ```
@@ -0,0 +1,5 @@
1
+ services:
2
+ app:
3
+ build: .
4
+ volumes:
5
+ - .:/app
@@ -5,6 +5,7 @@ require "faraday/retry"
5
5
  require "faraday_middleware/escher"
6
6
 
7
7
  class SessionValidator::Client
8
+ MSID_PATTERN = /^[a-z0-9._]+_[0-9a-f]{14}\.[0-9]{8}$/.freeze
8
9
  CREDENTIAL_SCOPE = "eu/session-validator/ems_request".freeze
9
10
  ESCHER_AUTH_OPTIONS = {
10
11
  algo_prefix: "EMS",
@@ -19,13 +20,26 @@ class SessionValidator::Client
19
20
  @use_escher = use_escher
20
21
  end
21
22
 
22
- def valid?(msid)
23
- response_status = client.get("/sessions/#{msid}", nil, headers).status
24
- (200..299).include?(response_status) || (500..599).include?(response_status)
23
+ def valid?(id)
24
+ if id.match(MSID_PATTERN)
25
+ valid_by_msid? id
26
+ else
27
+ valid_by_session_data_token? id
28
+ end
29
+ end
30
+
31
+ def session_data(token)
32
+ response = client.get("/sessions", nil, headers.merge(authorization_header token))
33
+ case response.status
34
+ when 200 then JSON.parse(response.body)
35
+ when 400..499 then raise SessionValidator::SessionDataNotFound
36
+ when 500.. then raise SessionValidator::SessionDataError, "Service unreachable"
37
+ end
25
38
  rescue *NETWORK_ERRORS
26
- true
39
+ raise SessionValidator::SessionDataError, "Service unreachable"
27
40
  end
28
41
 
42
+ # @deprecated
29
43
  def filter_invalid(msids)
30
44
  response = client.post("/sessions/filter", JSON.generate({ msids: msids }), headers)
31
45
  if response.status == 200
@@ -39,11 +53,29 @@ class SessionValidator::Client
39
53
 
40
54
  private
41
55
 
56
+ def valid_by_msid?(msid)
57
+ response_status = client.get("/sessions/#{msid}", nil, headers).status
58
+ (200..299).include?(response_status) || (500..599).include?(response_status)
59
+ rescue *NETWORK_ERRORS
60
+ true
61
+ end
62
+
63
+ def valid_by_session_data_token?(token)
64
+ response_status = client.head("/sessions", nil, headers.merge(authorization_header token)).status
65
+ case response_status
66
+ when 200 then true
67
+ when 400..499 then false
68
+ when 500.. then raise SessionValidator::SessionDataError, "Service unreachable"
69
+ end
70
+ rescue *NETWORK_ERRORS
71
+ raise SessionValidator::SessionDataError, "Service unreachable"
72
+ end
73
+
42
74
  def client
43
75
  Faraday.new(url) do |faraday|
44
76
  faraday.options[:open_timeout] = SERVICE_REQUEST_TIMEOUT
45
77
  faraday.options[:timeout] = SERVICE_REQUEST_TIMEOUT
46
- faraday.request :retry, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2, methods: [:get, :post], exceptions: NETWORK_ERRORS
78
+ faraday.request :retry, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2, methods: [:head, :get, :post], exceptions: NETWORK_ERRORS
47
79
  faraday.use(Faraday::Middleware::Escher::RequestSigner, escher_config) if @use_escher
48
80
  faraday.adapter Faraday.default_adapter
49
81
  end
@@ -73,4 +105,8 @@ class SessionValidator::Client
73
105
  def headers
74
106
  { "content-type" => "application/json" }
75
107
  end
108
+
109
+ def authorization_header(token)
110
+ { "Authorization" => "Bearer #{token}" }
111
+ end
76
112
  end
@@ -3,6 +3,10 @@ module SessionValidator
3
3
  autoload :CachedClient, "session_validator/cached_client"
4
4
  autoload :InMemoryCache, "session_validator/in_memory_cache"
5
5
 
6
+ class SessionDataError < StandardError; end
7
+
8
+ class SessionDataNotFound < SessionDataError; end
9
+
6
10
  def self.base_url
7
11
  ENV['SESSION_VALIDATOR_URL']
8
12
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "session-validator-client"
3
- s.version = "5.1.0"
3
+ s.version = "6.0.0"
4
4
  s.summary = "Ruby client for Emarsys session validator service"
5
5
  s.authors = ["Emarsys Technologies Ltd."]
6
6
  s.email = "security@emarsys.com"
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: 5.1.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emarsys Technologies Ltd.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-06 00:00:00.000000000 Z
11
+ date: 2025-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escher-keypool
@@ -128,20 +128,23 @@ dependencies:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
- description:
131
+ description:
132
132
  email: security@emarsys.com
133
133
  executables: []
134
134
  extensions: []
135
135
  extra_rdoc_files: []
136
136
  files:
137
- - ".env"
137
+ - ".env.example"
138
138
  - ".github/workflows/ruby.yml"
139
139
  - ".gitignore"
140
140
  - ".rspec"
141
+ - Dockerfile
141
142
  - Gemfile
142
143
  - LICENSE.txt
144
+ - Makefile
143
145
  - README.md
144
146
  - bin/console
147
+ - docker-compose.yml
145
148
  - lib/session_validator.rb
146
149
  - lib/session_validator/cached_client.rb
147
150
  - lib/session_validator/client.rb
@@ -152,7 +155,7 @@ homepage: https://github.com/emartech/session-validator-client-ruby
152
155
  licenses:
153
156
  - MIT
154
157
  metadata: {}
155
- post_install_message:
158
+ post_install_message:
156
159
  rdoc_options: []
157
160
  require_paths:
158
161
  - lib
@@ -167,8 +170,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
170
  - !ruby/object:Gem::Version
168
171
  version: '0'
169
172
  requirements: []
170
- rubygems_version: 3.2.33
171
- signing_key:
173
+ rubygems_version: 3.4.19
174
+ signing_key:
172
175
  specification_version: 4
173
176
  summary: Ruby client for Emarsys session validator service
174
177
  test_files: []
File without changes