ies_auth 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 61981c5759d58c5192f4f116e39339891f1f6a6218cfcd998668434db08517a3
4
+ data.tar.gz: 0b4a2066e92071a14e1f7f6f353201b1dafcdb9d22e36e91566057def8f32188
5
+ SHA512:
6
+ metadata.gz: f38ef185e74c1e3b2a080abf04bb78896110720536f317b2e788ccdaf5e033435ed046efd56590ef62a33c103f5f0bd3ca57b28e07a2e0a5dc629461c6065c11
7
+ data.tar.gz: 74a8e70ba7732232dcff3226cabc9d225d624c13936d783a39d69ce410b826b18d85580cc6a61d29bfad1464cdfc0af532bd0128c4c6da5575307e2a6d28e506
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ **/*.swp
10
+ .idea
11
+ .ruby-version
12
+ .byebug_history
13
+ .env
14
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,191 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2022-04-26 00:35:35 UTC using RuboCop version 1.26.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: Include.
11
+ # Include: **/*.gemspec
12
+ Gemspec/RequiredRubyVersion:
13
+ Exclude:
14
+ - 'ies_auth.gemspec'
15
+
16
+ # Offense count: 1
17
+ # This cop supports safe auto-correction (--auto-correct).
18
+ Layout/EmptyLineAfterMagicComment:
19
+ Exclude:
20
+ - 'lib/ies_auth/model/token.rb'
21
+
22
+ # Offense count: 2
23
+ # This cop supports safe auto-correction (--auto-correct).
24
+ # Configuration parameters: AllowAliasSyntax, AllowedMethods.
25
+ # AllowedMethods: alias_method, public, protected, private
26
+ Layout/EmptyLinesAroundAttributeAccessor:
27
+ Exclude:
28
+ - 'lib/ies_auth.rb'
29
+ - 'lib/ies_auth/token_manager.rb'
30
+
31
+ # Offense count: 1
32
+ # This cop supports safe auto-correction (--auto-correct).
33
+ # Configuration parameters: EnforcedStyle.
34
+ # SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines
35
+ Layout/EmptyLinesAroundModuleBody:
36
+ Exclude:
37
+ - 'lib/ies_auth.rb'
38
+
39
+ # Offense count: 1
40
+ # This cop supports safe auto-correction (--auto-correct).
41
+ # Configuration parameters: EnforcedStyle, IndentationWidth.
42
+ # SupportedStyles: consistent, consistent_relative_to_receiver, special_for_inner_method_call, special_for_inner_method_call_in_parentheses
43
+ Layout/FirstArgumentIndentation:
44
+ Exclude:
45
+ - 'test/model/token_test.rb'
46
+
47
+ # Offense count: 1
48
+ # This cop supports safe auto-correction (--auto-correct).
49
+ # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
50
+ # SupportedStyles: space, no_space
51
+ # SupportedStylesForEmptyBraces: space, no_space
52
+ Layout/SpaceInsideBlockBraces:
53
+ Exclude:
54
+ - 'test/support/custom_assertions.rb'
55
+
56
+ # Offense count: 2
57
+ # This cop supports safe auto-correction (--auto-correct).
58
+ # Configuration parameters: EnforcedStyle.
59
+ # SupportedStyles: space, compact, no_space
60
+ Layout/SpaceInsideParens:
61
+ Exclude:
62
+ - 'test/rest/options_test.rb'
63
+
64
+ # Offense count: 6
65
+ # This cop supports safe auto-correction (--auto-correct).
66
+ # Configuration parameters: EnforcedStyle.
67
+ # SupportedStyles: final_newline, final_blank_line
68
+ Layout/TrailingEmptyLines:
69
+ Exclude:
70
+ - 'lib/ies_auth/rest/headers.rb'
71
+ - 'lib/ies_auth/rest/options.rb'
72
+ - 'lib/ies_auth/rest/request.rb'
73
+ - 'test/model/token_test.rb'
74
+ - 'test/support/custom_assertions.rb'
75
+ - 'test/support/custom_expectations.rb'
76
+
77
+ # Offense count: 1
78
+ # This cop supports safe auto-correction (--auto-correct).
79
+ Lint/RedundantCopDisableDirective:
80
+ Exclude:
81
+ - 'lib/ies_auth/rest/options.rb'
82
+
83
+ # Offense count: 2
84
+ # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
85
+ Metrics/AbcSize:
86
+ Max: 18
87
+
88
+ # Offense count: 3
89
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
90
+ # IgnoredMethods: refine
91
+ Metrics/BlockLength:
92
+ Max: 60
93
+
94
+ # Offense count: 2
95
+ # This cop supports unsafe auto-correction (--auto-correct-all).
96
+ Style/CaseLikeIf:
97
+ Exclude:
98
+ - 'lib/ies_auth/rest/headers.rb'
99
+ - 'lib/ies_auth/rest/options.rb'
100
+
101
+ # Offense count: 2
102
+ # This cop supports safe auto-correction (--auto-correct).
103
+ # Configuration parameters: EnforcedStyle.
104
+ # SupportedStyles: nested, compact
105
+ Style/ClassAndModuleChildren:
106
+ Exclude:
107
+ - 'test/support/custom_assertions.rb'
108
+ - 'test/support/custom_expectations.rb'
109
+
110
+ # Offense count: 1
111
+ # Configuration parameters: AllowedConstants.
112
+ Style/Documentation:
113
+ Exclude:
114
+ - 'spec/**/*'
115
+ - 'test/**/*'
116
+ - 'lib/ies_auth.rb'
117
+
118
+ # Offense count: 11
119
+ # This cop supports safe auto-correction (--auto-correct).
120
+ # Configuration parameters: EnforcedStyle.
121
+ # SupportedStyles: always, always_true, never
122
+ Style/FrozenStringLiteralComment:
123
+ Exclude:
124
+ - 'bin/console'
125
+ - 'lib/ies_auth.rb'
126
+ - 'lib/ies_auth/version.rb'
127
+ - 'test/ies_auth_test.rb'
128
+ - 'test/model/token_test.rb'
129
+ - 'test/rest/headers_test.rb'
130
+ - 'test/rest/options_test.rb'
131
+ - 'test/rest/request_test.rb'
132
+ - 'test/support/custom_assertions.rb'
133
+ - 'test/support/custom_expectations.rb'
134
+ - 'test/token_manager_test.rb'
135
+
136
+ # Offense count: 1
137
+ # This cop supports safe auto-correction (--auto-correct).
138
+ # Configuration parameters: EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
139
+ # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
140
+ # SupportedShorthandSyntax: always, never, either
141
+ Style/HashSyntax:
142
+ EnforcedStyle: ruby19
143
+
144
+ # Offense count: 1
145
+ # This cop supports safe auto-correction (--auto-correct).
146
+ # Configuration parameters: EnforcedStyle.
147
+ # SupportedStyles: literals, strict
148
+ Style/MutableConstant:
149
+ Exclude:
150
+ - 'lib/ies_auth/version.rb'
151
+
152
+ # Offense count: 1
153
+ # This cop supports unsafe auto-correction (--auto-correct-all).
154
+ # Configuration parameters: SafeForConstants.
155
+ Style/RedundantFetchBlock:
156
+ Exclude:
157
+ - 'test/rest/options_test.rb'
158
+
159
+ # Offense count: 1
160
+ # This cop supports safe auto-correction (--auto-correct).
161
+ # Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods.
162
+ # AllowedMethods: present?, blank?, presence, try, try!
163
+ Style/SafeNavigation:
164
+ Exclude:
165
+ - 'lib/ies_auth/token_manager.rb'
166
+
167
+ # Offense count: 49
168
+ # This cop supports safe auto-correction (--auto-correct).
169
+ # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
170
+ # SupportedStyles: single_quotes, double_quotes
171
+ Style/StringLiterals:
172
+ Exclude:
173
+ - 'Gemfile'
174
+ - 'bin/console'
175
+ - 'lib/ies_auth.rb'
176
+ - 'lib/ies_auth/version.rb'
177
+ - 'test/ies_auth_test.rb'
178
+ - 'test/model/token_test.rb'
179
+ - 'test/rest/headers_test.rb'
180
+ - 'test/rest/options_test.rb'
181
+ - 'test/rest/request_test.rb'
182
+ - 'test/support/custom_assertions.rb'
183
+ - 'test/test_helper.rb'
184
+ - 'test/token_manager_test.rb'
185
+
186
+ # Offense count: 4
187
+ # This cop supports safe auto-correction (--auto-correct).
188
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
189
+ # URISchemes: http, https
190
+ Layout/LineLength:
191
+ Max: 1207
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem"s dependencies in ies_auth.gemspec
6
+ gemspec
7
+
8
+ gem "byebug"
9
+ gem "jwt", "~> 2.2", ">=2.2.2"
10
+ gem "rake", "~> 12.0"
11
+ gem "rubocop"
12
+
13
+ group :test do
14
+ gem "color_pound_spec_reporter", "~> 0.0.6"
15
+ gem "dotenv"
16
+ gem "minitest", "~> 5.10"
17
+ gem "minitest-reporters", "~> 1.1"
18
+ gem "minitest-vcr"
19
+ gem "vcr"
20
+ gem "webmock"
21
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ies_auth (0.1.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ansi (1.5.0)
12
+ ast (2.4.2)
13
+ builder (3.2.4)
14
+ byebug (11.1.3)
15
+ color_pound_spec_reporter (0.0.9)
16
+ bundler
17
+ minitest
18
+ minitest-reporters (>= 0.14.24)
19
+ rake
20
+ crack (0.4.5)
21
+ rexml
22
+ dotenv (2.7.6)
23
+ hashdiff (1.0.1)
24
+ jwt (2.3.0)
25
+ minispec-metadata (2.0.0)
26
+ minitest
27
+ minitest (5.15.0)
28
+ minitest-reporters (1.5.0)
29
+ ansi
30
+ builder
31
+ minitest (>= 5.0)
32
+ ruby-progressbar
33
+ minitest-vcr (1.4.0)
34
+ minispec-metadata (~> 2.0)
35
+ minitest (>= 4.7.5)
36
+ vcr (>= 2.9)
37
+ parallel (1.22.1)
38
+ parser (3.1.1.0)
39
+ ast (~> 2.4.1)
40
+ public_suffix (4.0.6)
41
+ rainbow (3.1.1)
42
+ rake (12.3.3)
43
+ regexp_parser (2.2.1)
44
+ rexml (3.2.5)
45
+ rubocop (1.26.1)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.1.0.0)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml
51
+ rubocop-ast (>= 1.16.0, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 1.4.0, < 3.0)
54
+ rubocop-ast (1.16.0)
55
+ parser (>= 3.1.1.0)
56
+ ruby-progressbar (1.11.0)
57
+ unicode-display_width (2.1.0)
58
+ vcr (6.1.0)
59
+ webmock (3.14.0)
60
+ addressable (>= 2.8.0)
61
+ crack (>= 0.3.2)
62
+ hashdiff (>= 0.4.0, < 2.0.0)
63
+
64
+ PLATFORMS
65
+ universal-darwin-20
66
+
67
+ DEPENDENCIES
68
+ byebug
69
+ color_pound_spec_reporter (~> 0.0.6)
70
+ dotenv
71
+ ies_auth!
72
+ jwt (~> 2.2, >= 2.2.2)
73
+ minitest (~> 5.10)
74
+ minitest-reporters (~> 1.1)
75
+ minitest-vcr
76
+ rake (~> 12.0)
77
+ rubocop
78
+ vcr
79
+ webmock
80
+
81
+ BUNDLED WITH
82
+ 2.3.10
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # IesAuth
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ies_auth'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ies_auth
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ies_auth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/env.example ADDED
@@ -0,0 +1,3 @@
1
+ IES_API_PASSWORD=
2
+ IES_API_URL=
3
+ IES_API_USER_NAME=
data/ies_auth.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ies_auth/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ies_auth'
7
+ spec.version = IesAuth::VERSION
8
+ spec.authors = ['Daniel P. Clark']
9
+ spec.email = ['6ftdan@gmail.com']
10
+
11
+ spec.summary = 'Write a short summary, because RubyGems requires one.'
12
+ spec.description = 'Write a longer description or delete this line.'
13
+ spec.required_ruby_version = '>= 2.5'
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ module IesAuth
3
+ # Usage
4
+ # example = Token.new
5
+ # example.build(message["body"])
6
+ class Token
7
+ attr_reader :data, :enc, :session
8
+ attr_writer :client
9
+ def initialize(data)
10
+ @data = data
11
+ @session, @enc = JWT.decode(data, nil, false, { algorithm: 'RS512' })
12
+ end
13
+
14
+ def algorithm
15
+ enc["alg"]
16
+ end
17
+
18
+ def client
19
+ @client ||= "ies_auth"
20
+ end
21
+
22
+ def expiration
23
+ Time.at(session["exp"])
24
+ end
25
+
26
+ def hcc
27
+ session["hcc"]
28
+ end
29
+
30
+ def issued_at
31
+ Time.at(session["iat"])
32
+ end
33
+
34
+ def kid
35
+ enc["kid"]
36
+ end
37
+
38
+ def session_id
39
+ session["sessid"]
40
+ end
41
+
42
+ def sub
43
+ session["sub"]
44
+ end
45
+
46
+ def type
47
+ session["type"]
48
+ end
49
+
50
+ def user_id
51
+ sub
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ies_auth/rest/headers'
4
+
5
+ module IesAuth
6
+ # TLD
7
+ module RequestDefaults
8
+ def ies_api_url(*args, &block)
9
+ IesAuth::IES.ies_api_url(*args, &block)
10
+ end
11
+
12
+ def get(*args, **kwargs)
13
+ Rest::Request.get(*args, **kwargs)
14
+ end
15
+
16
+ def headers
17
+ ::IesAuth::DEFAULT_HEADERS.dup
18
+ end
19
+
20
+ def post(*args, **kwargs)
21
+ Rest::Request.post(*args, **kwargs)
22
+ end
23
+
24
+ def put(*args, **kwargs)
25
+ Rest::Request.put(*args, **kwargs)
26
+ end
27
+
28
+ def delete(*args, **kwargs)
29
+ Rest::Request.delete(*args, **kwargs)
30
+ end
31
+ end
32
+
33
+ DEFAULT_HEADERS = Rest::Headers.new(
34
+ 'Content-Type': 'application/json',
35
+ 'Accept': 'application/json'
36
+ ).freeze
37
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module IesAuth
6
+ # TLD
7
+ module Rest
8
+ # A header builder class for outgoing requests
9
+ class Headers
10
+ extend Forwardable
11
+ # @!macro [attach] def_delegators
12
+ # @!method each_pair
13
+ # Forwards to $1.
14
+ # @see Hash#each_pair
15
+ # @!method fetch
16
+ # Forwards to $1.
17
+ # @see Hash#fetch
18
+ def_delegators :@heads, :each_pair, :fetch
19
+
20
+ def initialize(args = {})
21
+ build(args)
22
+ end
23
+
24
+ # Add or update the request headers
25
+ #
26
+ # @return [Headers] self
27
+ def build(args = {})
28
+ @heads ||= {}
29
+
30
+ args.to_h.each do |(key, value)|
31
+ @heads[key] = value
32
+ end
33
+
34
+ self
35
+ end
36
+
37
+ # Assign each header to object via :[]
38
+ def assign_each_to(obj)
39
+ each_pair do |header, value|
40
+ obj[header] = value
41
+ end
42
+
43
+ obj
44
+ end
45
+
46
+ # Remove key/value from headers via key
47
+ #
48
+ # @param key [Symbol, String] key to look up
49
+ # @return [String, Symbol, nil] returns value if key found, `nil` otherwise.
50
+ def remove(key)
51
+ @heads.delete(key)
52
+ end
53
+
54
+ # Add the values of one `Headers` into another
55
+ #
56
+ # @param other [Headers] instance of `Headers`
57
+ # @return [Headers]
58
+ def +(other)
59
+ raise TypeError, "Headers type expected, #{other.class} given" \
60
+ unless other.is_a? Headers
61
+
62
+ @heads.merge(other.instance_variable_get(:@heads))
63
+
64
+ self
65
+ end
66
+
67
+ # @return [Hash] hash of the `Headers`
68
+ def to_h
69
+ @heads
70
+ end
71
+ end
72
+
73
+ def self.Headers(obj)
74
+ if obj.is_a? Rest::Headers
75
+ obj
76
+ elsif obj.is_a? Hash
77
+ Rest::Headers.new(**obj)
78
+ elsif obj.is_a? Array
79
+ Rest::Headers.new(**obj.to_h)
80
+ else
81
+ raise 'Invalid object type for Headers!'
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IesAuth
4
+ # TLD
5
+ module Rest
6
+ # A url options builder class for outgoing requests.
7
+ class Options
8
+ def initialize(args = {})
9
+ build(args)
10
+ end
11
+
12
+ # url safe rendering of options for the url
13
+ #
14
+ # @return [String] url options
15
+ def opts
16
+ if @opts.empty?
17
+ ''
18
+ else
19
+ "?#{@opts.join('&')}"
20
+ end
21
+ end
22
+
23
+ # Add or update url options
24
+ #
25
+ # @return [Options] self
26
+ def build(args = {})
27
+ @opts ||= []
28
+
29
+ args.to_h.each do |(key, value)|
30
+ remove(key)
31
+ @opts.push("#{key}=#{value}")
32
+ end
33
+
34
+ self
35
+ end
36
+
37
+ # Fetch the `key=value`
38
+ #
39
+ # @param [Symbol, String] key of the key/value pair to fetch
40
+ # @return [String]
41
+ def fetch(key)
42
+ @opts.each do |item|
43
+ return item if key.to_s == split.call(item).first
44
+ end
45
+
46
+ raise KeyError, "key not found #{key}" unless block_given?
47
+
48
+ yield
49
+ end
50
+
51
+ # Fetch and remove `key=value`. Modifies `Options`.
52
+ #
53
+ # @param [Symbol, String] key of the key/value pair to fetch
54
+ # @return [String]
55
+ def fetch!(key, &block)
56
+ result = fetch(key, &block)
57
+ remove(key)
58
+ result
59
+ end
60
+
61
+ # Execute a block of code and restore original `Options` state afterwards
62
+ # @yield [Options]
63
+ def immutable
64
+ old = @opts
65
+ result = yield self
66
+ @opts = old
67
+ result
68
+ end
69
+
70
+ # Remove key/value from options via key
71
+ #
72
+ # @param key [Symbol, String] key to look up
73
+ # @return [String, nil] returns a `String` if key found, `nil` otherwise.
74
+ def remove(key)
75
+ return_value = nil
76
+
77
+ @opts = @opts.delete_if do |item|
78
+ head, tail = split.call item
79
+
80
+ return_value = tail if head == key.to_s
81
+ end
82
+
83
+ return_value
84
+ end
85
+
86
+ # this purges all options
87
+ #
88
+ # @return [Options] self
89
+ def reset!
90
+ @opts = []
91
+
92
+ self
93
+ end
94
+
95
+ # Add the values of one `Options` into another
96
+ #
97
+ # @param other [Options] instance of `Options`
98
+ # @return [Options]
99
+ def +(other)
100
+ raise TypeError, "Options type expected, #{other.class} given" unless other.is_a? Options # rubocop:disable Metrics/LineLength
101
+
102
+ update other.instance_variable_get(:@opts)
103
+
104
+ self
105
+ end
106
+
107
+ # (see #opts)
108
+ def to_s
109
+ opts
110
+ end
111
+
112
+ # @return [Hash] hash of the `Options`
113
+ def to_h
114
+ @opts.map(&split).to_h
115
+ end
116
+
117
+ private # @private
118
+
119
+ def split
120
+ ->(entry) { entry.split('=') }
121
+ end
122
+
123
+ def parse(other)
124
+ return other.split('&').map(&split).to_h if other.is_a? String
125
+
126
+ other.map(&split).to_h
127
+ end
128
+
129
+ def update(other)
130
+ return self unless other
131
+
132
+ build(parse(other))
133
+ end
134
+ end
135
+
136
+ def self.Options(obj)
137
+ if obj.is_a? Rest::Options
138
+ obj
139
+ elsif obj.is_a? Hash
140
+ Rest::Options.new(**obj)
141
+ elsif obj.is_a? Array
142
+ Rest::Options.new(**obj.to_h)
143
+ else
144
+ raise 'Invalid object type for Options!'
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require 'json'
5
+ require 'net/http'
6
+ require_relative './headers'
7
+ require_relative './options'
8
+
9
+ module IesAuth
10
+ module Rest
11
+ # Outgoing request methods
12
+ module Request
13
+ class << self
14
+ def get(url, **kwargs)
15
+ request(url, **kwargs) do |uri|
16
+ Net::HTTP::Get.new(uri)
17
+ end
18
+ end
19
+
20
+ def post(url, **kwargs)
21
+ request(url, **kwargs) do |uri|
22
+ Net::HTTP::Post.new(uri)
23
+ end
24
+ end
25
+
26
+ def put(url, **kwargs)
27
+ request(url, **kwargs) do |uri|
28
+ Net::HTTP::Put.new(uri)
29
+ end
30
+ end
31
+
32
+ def delete(url, **kwargs)
33
+ request(url, **kwargs) do |uri|
34
+ Net::HTTP::Delete.new(uri)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def request(url, body: nil, headers: nil, options: nil, auth: nil)
41
+ options = Rest.Options(options) if options
42
+
43
+ uri = URI("#{url}#{options}")
44
+
45
+ request = yield uri.request_uri
46
+ auth && request.basic_auth(auth[:username], auth[:password])
47
+
48
+ Rest.Headers(headers).assign_each_to(request) if headers
49
+
50
+ request.body = body ? JSON.generate(body) : ''
51
+
52
+ http = Net::HTTP.new(uri.host, uri.port)
53
+ http.use_ssl = true # (uri.scheme == 'https')
54
+
55
+ http.request(request)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ies_auth/model/token'
4
+
5
+ module IesAuth
6
+ # TLD
7
+ class TokenManager
8
+ attr_reader :token
9
+ include RequestDefaults
10
+ def initialize(token = nil)
11
+ @token = token || create_token
12
+ raise InvalidTokenStructureError unless token_duck?(@token)
13
+ raise ExpiredTokenError if expired?
14
+ end
15
+
16
+ def data
17
+ token.data
18
+ end
19
+
20
+ def expiration
21
+ token.expiration
22
+ end
23
+
24
+ def expired?
25
+ (Time.now <=> expiration).positive?
26
+ end
27
+
28
+ def user_id
29
+ token.user_id
30
+ end
31
+
32
+ private
33
+
34
+ def create_token
35
+ response = post(api_url, body: token_body, headers: headers)
36
+ data = JSON.parse(response.body).fetch('data')
37
+
38
+ @token = IesAuth::Token.new(data)
39
+ end
40
+
41
+ def token_body
42
+ {
43
+ userName: IesAuth::IES.username,
44
+ password: IesAuth::IES.password
45
+ }
46
+ end
47
+
48
+ def headers
49
+ { 'Content-Type': 'application/json' }
50
+ end
51
+
52
+ def api_url
53
+ ies_api_url 'tokens'
54
+ end
55
+
56
+ def token_duck?(token)
57
+ token &&
58
+ token.respond_to?(:data) &&
59
+ token.respond_to?(:client) &&
60
+ token.respond_to?(:expiration)
61
+ end
62
+
63
+ end
64
+
65
+ class InvalidTokenStructureError < StandardError; end
66
+
67
+ class ExpiredTokenError < StandardError; end
68
+ end
@@ -0,0 +1,3 @@
1
+ module IesAuth
2
+ VERSION = "0.1.3"
3
+ end
data/lib/ies_auth.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "ies_auth/version"
2
+ require "ies_auth/request_defaults"
3
+ require "ies_auth/model/token"
4
+ require "ies_auth/token_manager"
5
+ require "ies_auth/rest/headers"
6
+ require "ies_auth/rest/options"
7
+ require "ies_auth/rest/request"
8
+ require "date_core"
9
+ require "jwt"
10
+
11
+ module IesAuth
12
+ class Ies
13
+ attr_reader :api_endpoint
14
+ def initialize
15
+ @api_endpoint = ENV.fetch('IES_API_URL') { 'https://int-piapi-internal.stg-openclass.com' }
16
+ end
17
+
18
+ def ies_api_url(str)
19
+ [api_endpoint.chomp('/'), *str.split('/')].join('/')
20
+ end
21
+
22
+ def username
23
+ ENV.fetch('IES_API_USER_NAME')
24
+ end
25
+
26
+ def password
27
+ ENV.fetch('IES_API_PASSWORD')
28
+ end
29
+ end
30
+
31
+ IES = Ies.new
32
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ies_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Daniel P. Clark
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Write a longer description or delete this line.
14
+ email:
15
+ - 6ftdan@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rubocop.yml"
22
+ - ".rubocop_todo.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - env.example
30
+ - ies_auth.gemspec
31
+ - lib/ies_auth.rb
32
+ - lib/ies_auth/model/token.rb
33
+ - lib/ies_auth/request_defaults.rb
34
+ - lib/ies_auth/rest/headers.rb
35
+ - lib/ies_auth/rest/options.rb
36
+ - lib/ies_auth/rest/request.rb
37
+ - lib/ies_auth/token_manager.rb
38
+ - lib/ies_auth/version.rb
39
+ homepage:
40
+ licenses: []
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '2.5'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.3.7
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Write a short summary, because RubyGems requires one.
61
+ test_files: []