kindtap-platform-ruby 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f548b3974570d50307e6fa7d326ec1b51ad02fa9186ab6099c835a1c820572ba
4
+ data.tar.gz: 65aa39c04cc5d537a2ed6abe5e9adfb7e43f5e42af125a6d8f884fea512fcaa9
5
+ SHA512:
6
+ metadata.gz: c74f1b178f63da4ba91a2bcbaa6a739bde17e118e04efbf22398aa1c63464f9b545addba4b29fb03568f0972678e1d5434d1765a92fc163e2379727e1ccf38ce
7
+ data.tar.gz: 9d8d3e6bdf55e874ab300240e27a4253129d388d6c6d126a966165238fdf8e39fe22d210b348874bd678f7bf461b28ff57d58f911c2bd012218a072f390ed2a4
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 KindTap
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ ## KindTap Platform Library for Ruby
2
+
3
+ #### This library currently supports generating a signed authorization header which is required to make requests to KindTap Platform APIs.
4
+
5
+ ### Installation
6
+
7
+ * Add the following to your Gemfile
8
+
9
+ `gem 'kindtap-platform-ruby', git: 'https://github.com/KindTap/kindtap-platform-ruby.git', tag: '0.1.0'`
10
+
11
+ ### Example using Faraday
12
+
13
+ #### Important Notes
14
+
15
+ * the `host` and `x-kt-date` headers are required
16
+ * request body must be a string that matches exactly the body of the HTTP request
17
+
18
+ ```rb
19
+ require 'date'
20
+ require 'faraday'
21
+ require 'json'
22
+ require 'kindtap_platform'
23
+
24
+ host = 'kindtap-platform-host'
25
+ date = Time.now.utc
26
+
27
+ path = '/path/to/api/endpoint/'
28
+ service = 'kindtap-platform-service-name'
29
+ key = 'kindtap-client-key'
30
+ secret = 'kindtap-client-secret'
31
+ method = '<http-method>'
32
+ headers = {
33
+ 'Content-Type' => 'application/json',
34
+ 'Host' => host,
35
+ 'X-KT-Date' => KindTapPlatform.stringify_date(date),
36
+ }
37
+ body = JSON.generate({})
38
+ query = {}
39
+ querystring = URI.encode_www_form(query)
40
+
41
+ headers['Authorization'] = KindTapPlatform.generate_signed_auth_header(
42
+ service,
43
+ key,
44
+ secret,
45
+ method,
46
+ path,
47
+ date,
48
+ headers,
49
+ body,
50
+ query,
51
+ )
52
+
53
+ session = Faraday.new
54
+
55
+ response = session.post do |request|
56
+ request.url "https://#{host}#{path}?#{querystring}"
57
+ request.headers = headers
58
+ request.body = body
59
+ end
60
+
61
+ puts response.body
62
+ ```
@@ -0,0 +1,24 @@
1
+ require File.expand_path('lib/kindtap_platform/version', __dir__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'kindtap-platform-ruby'
5
+ spec.version = KindTapPlatform::VERSION
6
+ spec.authors = ['Jeff Trudeau']
7
+ spec.email = ['jeff@kindtap.com']
8
+ spec.summary = 'Facilitates integration with KindTap Platform'
9
+ spec.description = 'This library currently supports generating a signed authorization header which is required to make requests to KindTap Platform APIs.'
10
+ spec.homepage = 'https://github.com/KindTap/kindtap-platform-ruby'
11
+ spec.license = 'MIT'
12
+ spec.platform = Gem::Platform::RUBY
13
+ #spec.required_ruby_version = '~> 2.7'
14
+ spec.files = Dir[
15
+ 'Gemfile',
16
+ 'kindtap_platform.gemspec',
17
+ 'lib/**/*.rb',
18
+ 'LICENSE',
19
+ 'README.md',
20
+ ]
21
+ spec.extra_rdoc_files = [
22
+ 'README.md',
23
+ ]
24
+ end
@@ -0,0 +1,3 @@
1
+ module KindTapPlatform
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,118 @@
1
+ module KindTapPlatform
2
+ DEBUG = false
3
+
4
+ class << self
5
+ ALGO_PRE = "KT1"
6
+ ALGO = "#{ALGO_PRE}-HMAC-SHA256"
7
+ AUTH_TYPE = "#{ALGO_PRE.downcase}_request"
8
+ REGION = "us"
9
+
10
+ EQUALS_ENC = URI.encode_www_form_component("=")
11
+
12
+ EQUALS_EXPR = /=/
13
+ MULTI_WS_EXPR = /[ ][ ]+/
14
+
15
+ def HMACSHA256(key, data)
16
+ OpenSSL::HMAC.digest("SHA256", key, data)
17
+ end
18
+
19
+ def SHA256(data)
20
+ OpenSSL::Digest.hexdigest("SHA256", data)
21
+ end
22
+
23
+ def build_canon_headers(headers)
24
+ parsed = headers.reduce({}) { |acc, (k, v)|
25
+ acc.merge({ k.to_s.downcase => v.to_s.gsub(MULTI_WS_EXPR, " ") })
26
+ }
27
+ sorted = parsed.sort_by { |k, v| k.to_s.ord }
28
+ sorted.reduce("") { |acc, cur| acc += "#{cur[0]}:#{cur[1]}\n" }
29
+ end
30
+
31
+ def build_canon_query(params)
32
+ parsed = params.reduce({}) { |acc, (k, v)|
33
+ acc.merge({ k.to_s => v.to_s.gsub(EQUALS_EXPR, EQUALS_ENC) })
34
+ }
35
+ sorted = parsed.sort_by { |k, v| k.to_s.ord }
36
+ encoded = sorted.reduce([]) { |acc, cur|
37
+ acc.push("#{URI.encode_www_form_component(cur[0])}=#{URI.encode_www_form_component(cur[1])}")
38
+ }
39
+ encoded.join("&")
40
+ end
41
+
42
+ def build_canon_uri(uri)
43
+ parts = uri.split("/").filter { |p| p.length > 0 }
44
+ if parts.length == 0
45
+ return "/"
46
+ end
47
+ encoded = parts.reduce([]) { |acc, cur|
48
+ acc.push(URI.encode_www_form_component(URI.encode_www_form_component(cur)))
49
+ }
50
+ "/#{encoded.join("/")}/"
51
+ end
52
+
53
+ def build_signed_headers(headers)
54
+ parsed = headers.reduce([]) { |acc, cur| acc.push([cur[0].downcase]) }
55
+ parsed.join(";")
56
+ end
57
+
58
+ def debug(*args)
59
+ if KindTapPlatform::DEBUG
60
+ puts args
61
+ end
62
+ end
63
+
64
+ def generate_signature_v1(service, secret, method, uri, date, headers, body, params)
65
+ canon_headers = build_canon_headers(headers)
66
+ debug({ "canon_headers" => canon_headers })
67
+ canon_query = build_canon_query(params)
68
+ debug({ "canon_query" => canon_query })
69
+ canon_uri = build_canon_uri(uri)
70
+ debug({ "canon_uri" => canon_uri })
71
+ signed_headers = build_signed_headers(headers)
72
+ debug({ "signed_headers" => signed_headers })
73
+
74
+ canon_request = [
75
+ method.upcase,
76
+ canon_uri,
77
+ canon_query,
78
+ canon_headers,
79
+ signed_headers,
80
+ SHA256(body),
81
+ ].join("\n")
82
+ debug({ "canon_request" => canon_request })
83
+ canon_request_hash = SHA256(canon_request)
84
+ debug({ "canon_request_hash" => canon_request_hash })
85
+
86
+ cred_date = stringify_date(date, false)
87
+ cred_scope = "#{cred_date}/#{REGION}/#{service}/#{AUTH_TYPE}"
88
+
89
+ msg_to_sign = [
90
+ ALGO,
91
+ stringify_date(date),
92
+ cred_scope,
93
+ canon_request_hash,
94
+ ].join("\n")
95
+ debug({ "msg_to_sign" => msg_to_sign })
96
+
97
+ k0 = HMACSHA256("#{ALGO_PRE}#{secret}", cred_date)
98
+ k1 = HMACSHA256(k0, REGION)
99
+ k2 = HMACSHA256(k1, service)
100
+ k3 = HMACSHA256(k2, AUTH_TYPE)
101
+
102
+ HMACSHA256(k3, msg_to_sign).unpack("H*")[0]
103
+ end
104
+
105
+ def generate_signed_auth_header(service, key, secret, method, uri, date, headers, body, params)
106
+ cred_date = stringify_date(date, false)
107
+ cred_scope = "#{cred_date}/#{REGION}/#{service}/#{AUTH_TYPE}"
108
+ signed_headers = build_signed_headers(headers)
109
+ signature = generate_signature_v1(service, secret, method, uri, date, headers, body, params)
110
+ debug({ "signature" => signature })
111
+ "#{ALGO} Credential=#{key}/#{cred_scope}, SignedHeaders=#{signed_headers}, Signature=#{signature}"
112
+ end
113
+
114
+ def stringify_date(date, full = true)
115
+ date.strftime(full ? "%Y%m%dT%H%M%SZ" : "%Y%m%d")
116
+ end
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kindtap-platform-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Trudeau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This library currently supports generating a signed authorization header
14
+ which is required to make requests to KindTap Platform APIs.
15
+ email:
16
+ - jeff@kindtap.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.md
21
+ files:
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - kindtap_platform.gemspec
26
+ - lib/kindtap_platform.rb
27
+ - lib/kindtap_platform/version.rb
28
+ homepage: https://github.com/KindTap/kindtap-platform-ruby
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.1.4
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Facilitates integration with KindTap Platform
51
+ test_files: []