virgil-sdk 4.2.3
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 +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/README.md +134 -0
- data/Rakefile +9 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/dockefiles/Dockerfile-200 +25 -0
- data/dockefiles/Dockerfile-2110 +36 -0
- data/dockefiles/Dockerfile-220 +26 -0
- data/dockefiles/Dockerfile-226 +25 -0
- data/dockefiles/Dockerfile-233 +25 -0
- data/dockefiles/Dockerfile-240 +26 -0
- data/docker-compose.yml +107 -0
- data/lib/virgil/sdk.rb +10 -0
- data/lib/virgil/sdk/client.rb +47 -0
- data/lib/virgil/sdk/client/card.rb +142 -0
- data/lib/virgil/sdk/client/card_validator.rb +104 -0
- data/lib/virgil/sdk/client/http.rb +45 -0
- data/lib/virgil/sdk/client/http/base_connection.rb +112 -0
- data/lib/virgil/sdk/client/http/cards_service_connection.rb +113 -0
- data/lib/virgil/sdk/client/http/request.rb +63 -0
- data/lib/virgil/sdk/client/request_signer.rb +90 -0
- data/lib/virgil/sdk/client/requests.rb +50 -0
- data/lib/virgil/sdk/client/requests/confirm_identity_request.rb +67 -0
- data/lib/virgil/sdk/client/requests/create_card_request.rb +105 -0
- data/lib/virgil/sdk/client/requests/revoke_card_request.rb +85 -0
- data/lib/virgil/sdk/client/requests/signable_request.rb +142 -0
- data/lib/virgil/sdk/client/requests/verify_identity_request.rb +60 -0
- data/lib/virgil/sdk/client/search_criteria.rb +79 -0
- data/lib/virgil/sdk/client/signatures_base64.rb +25 -0
- data/lib/virgil/sdk/client/virgil_client.rb +425 -0
- data/lib/virgil/sdk/cryptography.rb +42 -0
- data/lib/virgil/sdk/cryptography/hashes.rb +44 -0
- data/lib/virgil/sdk/cryptography/hashes/fingerprint.rb +79 -0
- data/lib/virgil/sdk/cryptography/hashes/hash_algorithm.rb +91 -0
- data/lib/virgil/sdk/cryptography/keys.rb +48 -0
- data/lib/virgil/sdk/cryptography/keys/key_pair.rb +46 -0
- data/lib/virgil/sdk/cryptography/keys/key_pair_type.rb +108 -0
- data/lib/virgil/sdk/cryptography/keys/key_storage.rb +177 -0
- data/lib/virgil/sdk/cryptography/keys/private_key.rb +44 -0
- data/lib/virgil/sdk/cryptography/keys/public_key.rb +44 -0
- data/lib/virgil/sdk/cryptography/keys/storage_item.rb +63 -0
- data/lib/virgil/sdk/cryptography/virgil_crypto.rb +411 -0
- data/lib/virgil/sdk/high_level.rb +21 -0
- data/lib/virgil/sdk/high_level/virgil_api.rb +71 -0
- data/lib/virgil/sdk/high_level/virgil_app_credentials.rb +54 -0
- data/lib/virgil/sdk/high_level/virgil_buffer.rb +161 -0
- data/lib/virgil/sdk/high_level/virgil_card.rb +204 -0
- data/lib/virgil/sdk/high_level/virgil_card_manager.rb +294 -0
- data/lib/virgil/sdk/high_level/virgil_card_verifier_info.rb +49 -0
- data/lib/virgil/sdk/high_level/virgil_context.rb +69 -0
- data/lib/virgil/sdk/high_level/virgil_identity.rb +17 -0
- data/lib/virgil/sdk/high_level/virgil_identity/email_confirmation.rb +60 -0
- data/lib/virgil/sdk/high_level/virgil_identity/validation_token.rb +49 -0
- data/lib/virgil/sdk/high_level/virgil_identity/verification_attempt.rb +69 -0
- data/lib/virgil/sdk/high_level/virgil_identity/verification_options.rb +56 -0
- data/lib/virgil/sdk/high_level/virgil_key.rb +168 -0
- data/lib/virgil/sdk/high_level/virgil_key_manager.rb +97 -0
- data/lib/virgil/sdk/version.rb +5 -0
- data/virgil-sdk.gemspec +31 -0
- metadata +203 -0
data/lib/virgil/sdk.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Copyright (C) 2016 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, bytes, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
module Virgil
|
35
|
+
module SDK
|
36
|
+
module Client
|
37
|
+
autoload :SignaturesBase64, 'virgil/sdk/client/signatures_base64'
|
38
|
+
autoload :Card, 'virgil/sdk/client/card'
|
39
|
+
autoload :CardValidator, 'virgil/sdk/client/card_validator'
|
40
|
+
autoload :SearchCriteria, 'virgil/sdk/client/search_criteria'
|
41
|
+
autoload :RequestSigner, 'virgil/sdk/client/request_signer'
|
42
|
+
autoload :VirgilClient, 'virgil/sdk/client/virgil_client'
|
43
|
+
autoload :HTTP, 'virgil/sdk/client/http'
|
44
|
+
autoload :Requests, 'virgil/sdk/client/requests'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# Copyright (C) 2016 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
require 'base64'
|
35
|
+
require 'json'
|
36
|
+
module Virgil
|
37
|
+
module SDK
|
38
|
+
module Client
|
39
|
+
# Model representing cards information.
|
40
|
+
Card = Struct.new(:id, :snapshot, :identity,
|
41
|
+
:identity_type, :public_key, :scope,
|
42
|
+
:data, :device, :device_name, :version,
|
43
|
+
:signatures, :validation_token) do
|
44
|
+
|
45
|
+
extend SignaturesBase64
|
46
|
+
|
47
|
+
def initialize(options)
|
48
|
+
self.id = options[:id]
|
49
|
+
self.snapshot = options[:snapshot]
|
50
|
+
self.identity = options[:identity]
|
51
|
+
self.identity_type = options[:identity_type]
|
52
|
+
self.public_key = options[:public_key]
|
53
|
+
self.scope = options[:scope]
|
54
|
+
self.data = options[:data] || {}
|
55
|
+
self.device = options[:device]
|
56
|
+
self.device_name = options[:device_name]
|
57
|
+
self.version = options[:version]
|
58
|
+
self.signatures = options[:signatures] || {}
|
59
|
+
end
|
60
|
+
|
61
|
+
# Create new Card from response containing json-encoded snapshot.
|
62
|
+
# Args:
|
63
|
+
# response: Cards service response containing base64 encoded content_snapshot.
|
64
|
+
# Returns:
|
65
|
+
# Card model restored from snapshot.
|
66
|
+
def self.from_response(response)
|
67
|
+
snapshot = Base64.decode64(response["content_snapshot"])
|
68
|
+
snapshot_model = JSON.parse(snapshot)
|
69
|
+
info = snapshot_model.fetch("info", {}) || {}
|
70
|
+
|
71
|
+
return new(
|
72
|
+
id: response["id"],
|
73
|
+
snapshot: snapshot,
|
74
|
+
identity: snapshot_model["identity"],
|
75
|
+
identity_type: snapshot_model["identity_type"],
|
76
|
+
public_key: Virgil::Crypto::Bytes.from_base64(
|
77
|
+
snapshot_model["public_key"]
|
78
|
+
),
|
79
|
+
device: info["device"],
|
80
|
+
device_name: info["device_name"],
|
81
|
+
data: snapshot_model.fetch("data", {}),
|
82
|
+
scope: snapshot_model["scope"],
|
83
|
+
version: response["meta"]["card_version"],
|
84
|
+
signatures: response["meta"]["signs"]
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def to_request
|
92
|
+
request = Virgil::SDK::Client::Requests::CreateCardRequest.new({})
|
93
|
+
request.restore(Crypto::Bytes.from_string(self.snapshot), self.signatures, validation_token)
|
94
|
+
request
|
95
|
+
end
|
96
|
+
|
97
|
+
def export
|
98
|
+
self.to_request.export
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def self.from_request_model(request_model)
|
103
|
+
snapshot = Base64.decode64(request_model[:content_snapshot])
|
104
|
+
# if request_model[:content_snapshot].is_a?(Array)
|
105
|
+
# snapshot = Virgil::Crypto::Bytes.new(request_model[:content_snapshot]).to_s
|
106
|
+
# end
|
107
|
+
|
108
|
+
snapshot_model = JSON.parse(snapshot)
|
109
|
+
meta = request_model[:meta]
|
110
|
+
info = snapshot_model.fetch("info", {}) || {}
|
111
|
+
return new(
|
112
|
+
snapshot: snapshot,
|
113
|
+
identity: snapshot_model["identity"],
|
114
|
+
identity_type: snapshot_model["identity_type"],
|
115
|
+
public_key: Virgil::Crypto::Bytes.from_base64(
|
116
|
+
snapshot_model["public_key"]
|
117
|
+
),
|
118
|
+
device: info["device"],
|
119
|
+
device_name: info["device_name"],
|
120
|
+
data: snapshot_model.fetch("data", {}),
|
121
|
+
scope: snapshot_model["scope"],
|
122
|
+
signatures: meta[:signs]
|
123
|
+
)
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
Card::APPLICATION = "application"
|
130
|
+
Card::GLOBAL = "global"
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
Card::SERVICE_URL = ENV["VIRGIL_SERVICE_URL"] || "https://ra.virgilsecurity.com"
|
136
|
+
Card::READ_ONLY_SERVICE_URL = ENV["VIRGIL_READ_ONLY_SERVICE_URL"] || "https://cards-ro.virgilsecurity.com"
|
137
|
+
|
138
|
+
Card::VRA_VERSION = "v1" # version of service, which creates and deletes local and global cards
|
139
|
+
Card::VC_VERSION = "v4" # version of service, which gets, searchs card
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# Copyright (C) 2016 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
module Virgil
|
38
|
+
module SDK
|
39
|
+
module Client
|
40
|
+
# Class used for cards signatures validation.
|
41
|
+
class CardValidator
|
42
|
+
SERVICE_CARD_ID =
|
43
|
+
'3e29d43373348cfb373b7eae189214dc01d7237765e572db685839b64adca853'
|
44
|
+
SERVICE_PUBLIC_KEY =
|
45
|
+
'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUNvd0JRWURLMlZ3QXlFQVlSNTAx'\
|
46
|
+
'a1YxdFVuZTJ1T2RrdzRrRXJSUmJKcmMyU3lhejVWMWZ1RytyVnM9Ci0tLS0tRU5E'\
|
47
|
+
'IFBVQkxJQyBLRVktLS0tLQo='
|
48
|
+
|
49
|
+
attr_reader :crypto, :verifiers
|
50
|
+
|
51
|
+
def initialize(crypto)
|
52
|
+
@crypto = crypto
|
53
|
+
@public_key_bytes = Crypto::Bytes.from_base64(SERVICE_PUBLIC_KEY)
|
54
|
+
@public_key = crypto.import_public_key(@public_key_bytes)
|
55
|
+
@verifiers = {
|
56
|
+
SERVICE_CARD_ID => @public_key
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
# Add signature verifier.
|
61
|
+
#
|
62
|
+
# Args:
|
63
|
+
# card_id: Card identifier
|
64
|
+
# public_key: Public key used for signature verification.
|
65
|
+
def add_verifier(card_id, public_key)
|
66
|
+
@verifiers[card_id] = public_key
|
67
|
+
end
|
68
|
+
|
69
|
+
# Validates Card using verifiers.
|
70
|
+
#
|
71
|
+
# Args:
|
72
|
+
# card: Card for validation.
|
73
|
+
# Returns:
|
74
|
+
# True if card signatures are valid, false otherwise.
|
75
|
+
def is_valid?(card)
|
76
|
+
|
77
|
+
return true if (card.version == '4.0' || card.version == '3.0')
|
78
|
+
|
79
|
+
fingerprint = self.crypto.calculate_fingerprint(
|
80
|
+
Crypto::Bytes.from_string(card.snapshot)
|
81
|
+
)
|
82
|
+
fingerprint_hex = fingerprint.to_hex
|
83
|
+
return false if fingerprint_hex != card.id
|
84
|
+
|
85
|
+
verifiers = self.verifiers.clone
|
86
|
+
card_public_key = self.crypto.import_public_key(card.public_key)
|
87
|
+
verifiers[fingerprint_hex] = card_public_key
|
88
|
+
verifiers.each do |id, key|
|
89
|
+
unless card.signatures.has_key?(id)
|
90
|
+
return false
|
91
|
+
end
|
92
|
+
is_valid = self.crypto.verify(
|
93
|
+
fingerprint.value,
|
94
|
+
Crypto::Bytes.from_base64(card.signatures[id]),
|
95
|
+
key
|
96
|
+
)
|
97
|
+
return false unless is_valid
|
98
|
+
end
|
99
|
+
true
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Copyright (C) 2016 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, bytes, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
module Virgil
|
35
|
+
module SDK
|
36
|
+
module Client
|
37
|
+
module HTTP
|
38
|
+
autoload :Request, 'virgil/sdk/client/http/request'
|
39
|
+
autoload :BaseConnection, 'virgil/sdk/client/http/base_connection'
|
40
|
+
autoload :CardsServiceConnection,
|
41
|
+
'virgil/sdk/client/http/cards_service_connection'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# Copyright (C) 2016 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
require 'faraday'
|
35
|
+
require 'faraday_middleware'
|
36
|
+
require 'json'
|
37
|
+
|
38
|
+
module Virgil
|
39
|
+
module SDK
|
40
|
+
module Client
|
41
|
+
module HTTP
|
42
|
+
# Base API service connection class.
|
43
|
+
class BaseConnection
|
44
|
+
class ApiError < StandardError
|
45
|
+
end
|
46
|
+
|
47
|
+
ERRORS = {}
|
48
|
+
|
49
|
+
attr_accessor :access_token, :base_url
|
50
|
+
|
51
|
+
# Constructs new BaseConnection object.
|
52
|
+
def initialize(access_token, base_url)
|
53
|
+
self.access_token = access_token
|
54
|
+
self.base_url = base_url
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sends http request to the endpoint.
|
58
|
+
#
|
59
|
+
# Args:
|
60
|
+
# request: HTTP::Request object containing sending request data.
|
61
|
+
#
|
62
|
+
# Returns:
|
63
|
+
# Deserialized ruby object from the json response.
|
64
|
+
#
|
65
|
+
# Raises:
|
66
|
+
# HTTPError with error message decoded from errors dictionary.
|
67
|
+
def send_request(request)
|
68
|
+
response = faraday_connection.run_request(
|
69
|
+
request.method,
|
70
|
+
request.endpoint,
|
71
|
+
request.body,
|
72
|
+
request.headers
|
73
|
+
)
|
74
|
+
return response.body if response.success?
|
75
|
+
|
76
|
+
raise ApiError.new(error_message(response))
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def faraday_connection
|
83
|
+
@faraday_connection ||= Faraday.new(url: base_url) do |connection|
|
84
|
+
if access_token
|
85
|
+
connection.authorization :VIRGIL, access_token
|
86
|
+
end
|
87
|
+
connection.request :json
|
88
|
+
connection.response :json, :content_type => /\bjson$/
|
89
|
+
connection.response :follow_redirects
|
90
|
+
connection.adapter Faraday.default_adapter
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def error_message(response)
|
96
|
+
error_message = nil
|
97
|
+
error_body = response.body
|
98
|
+
if error_body
|
99
|
+
error_body = JSON.parse(error_body) unless error_body.is_a? Hash
|
100
|
+
error_code = error_body['code'] ||
|
101
|
+
(error_body['error'] && error_body['error']['code'])
|
102
|
+
error_message = self.class::ERRORS[error_code] || error_code
|
103
|
+
end
|
104
|
+
# token = attempt.confirm(emailConfirmation)
|
105
|
+
error_message = "Error code is #{response.status}" unless error_message
|
106
|
+
error_message
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|