wattsense-api-client 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/watt_sense_api/v1/authentication.rb +27 -2
- data/lib/watt_sense_api/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d0e958d259d4dfd6a296368e9c49867d396cb789e9a529952d05edb0cfcbe78
|
4
|
+
data.tar.gz: 2059813ffd4b5e059f0707256bfc33e86d9cb42d927cccab5338854d06934d48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12a85c70878ebf6de8a047e2500fbe36825e886f1c1b513111a75c1fc56d8ac320fe016c690419638a13925a8cc55bd0b7b79df887ac3937dcb93422072a814f
|
7
|
+
data.tar.gz: 424cf11c91269389ca4e73c7f0e26507f55bfd30ff23ae0ffd043aa2f3262eeb0bd6ecd4c0bd543516fdfa8dc5c8d36cfd2f78bc6d5602577487604c3474845c
|
@@ -5,6 +5,9 @@
|
|
5
5
|
#++
|
6
6
|
# frozen_string_literal: true
|
7
7
|
|
8
|
+
require 'openssl'
|
9
|
+
require 'base64'
|
10
|
+
|
8
11
|
module WattSenseApi
|
9
12
|
module V1
|
10
13
|
# This module gather all the necessary elements for ClientV1 ability to authenticate to the
|
@@ -19,8 +22,15 @@ module WattSenseApi
|
|
19
22
|
#
|
20
23
|
# @param api_key [String] The api's credential key
|
21
24
|
# @param api_secret [String] The api's credential secret
|
22
|
-
def api_key_authentication(
|
23
|
-
|
25
|
+
def api_key_authentication(api_key, api_secret)
|
26
|
+
@auth_block =
|
27
|
+
lambda do |request|
|
28
|
+
time = now_in_ms
|
29
|
+
to_sign = to_sign_from_request(request, time)
|
30
|
+
signed = ::OpenSSL::HMAC.digest('SHA512', api_secret, to_sign.join("\n"))
|
31
|
+
request['X-API-Auth'] = "#{api_key}:#{::Base64.strict_encode64(signed)}"
|
32
|
+
request['X-API-Timestamp'] = time.to_s
|
33
|
+
end
|
24
34
|
end
|
25
35
|
|
26
36
|
# This method allows to use username/password credentials with HTTP Basic user authentication
|
@@ -31,6 +41,21 @@ module WattSenseApi
|
|
31
41
|
def basic_user_authentication(username, password)
|
32
42
|
@auth_block = ->(request) { request.basic_auth(username, password) }
|
33
43
|
end
|
44
|
+
|
45
|
+
# @return [Integer] Time now in ms since Epoch
|
46
|
+
def now_in_ms
|
47
|
+
now = ::Time.now
|
48
|
+
now.tv_sec * 1000 + now.tv_usec / 1_000
|
49
|
+
end
|
50
|
+
|
51
|
+
# @return [String] Message to sign
|
52
|
+
def to_sign_from_request(request, time)
|
53
|
+
to_sign = [request.method, request.uri.path]
|
54
|
+
to_sign.push(request.uri.query) if request.uri.query
|
55
|
+
to_sign.push(request.body) if request.body
|
56
|
+
to_sign << time.to_s
|
57
|
+
to_sign
|
58
|
+
end
|
34
59
|
end
|
35
60
|
end
|
36
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wattsense-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roland Laurès
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -119,7 +119,7 @@ licenses:
|
|
119
119
|
metadata:
|
120
120
|
bug_tracker_uri: https://gitlab.com/octopuslab-public/wattsense-api-client/-/issues
|
121
121
|
changelog_uri: https://gitlab.com/octopuslab-public/wattsense-api-client/-/blob/main/CHANGELOG.md
|
122
|
-
documentation_uri: https://octopuslab-public.gitlab.io/wattsense-api-client/0.
|
122
|
+
documentation_uri: https://octopuslab-public.gitlab.io/wattsense-api-client/0.3.0
|
123
123
|
homepage_uri: https://gitlab.com/octopuslab-public/wattsense-api-client
|
124
124
|
post_install_message:
|
125
125
|
rdoc_options: []
|