Class: Virgil::SDK::Client::VirgilClient
- Inherits:
-
Object
- Object
- Virgil::SDK::Client::VirgilClient
- Defined in:
- lib/virgil/sdk/client/virgil_client.rb
Overview
This class represents a Virgil Security services client and contains all methods to interaction with server.
Defined Under Namespace
Classes: InvalidCardException
Instance Attribute Summary collapse
-
#access_token ⇒ String
Provides an authenticated secure access to the Virgil Security services.
-
#card_validator ⇒ CardValidator
The current value of card_validator.
-
#cards_read_only_service_url ⇒ String
Virgil Cards RO service url.
-
#cards_service_url ⇒ String
Virgil Cards service url.
-
#identity_service_url ⇒ String
Virgil Identity service url.
-
#ra_service_url ⇒ String
Virgil RA service url.
Instance Method Summary collapse
-
#add_relation(request) ⇒ Card
Adds a relation for the Virgil Card to Virgil cards service.
-
#cards_connection ⇒ HTTP::CardsServiceConnection
Cards service connection used for add and delete relations.
-
#confirm_identity(action_id, confirmation_code, time_to_live, count_to_live) ⇒ String
Confirms the identity using confirmation code, that has been generated to confirm an identity.
-
#create_card(identity, identity_type, key_pair, app_id, app_key) ⇒ Card
Create published new card from given attributes.
-
#create_card_from_signed_request(create_request) ⇒ Card
Create new published card from signed creation request.
-
#crypto ⇒ Cryptography::VirgilCrypto
Crypto library wrapper.
-
#delete_relation(request) ⇒ Card
Deletes a relation for the Virgil Card to Virgil cards service.
-
#get_card(card_id) ⇒ Card
Get card by id.
-
#identity_service_connection ⇒ HTTP::CardsServiceConnection
Virgil Identity service connection used for validation of user's identities like email, application, etc.
-
#initialize(access_token = nil, cards_service_url = Card::SERVICE_URL, cards_read_only_service_url = Card::READ_ONLY_SERVICE_URL, identity_service_url = HighLevel::VirgilIdentity::IDENTITY_SERVICE_URL, ra_service_url = Card::RA_SERVICE_URL) ⇒ VirgilClient
constructor
Initializes a new instance of the VirgilClient class.
-
#new_card(identity, identity_type, private_key, custom_data = {}) ⇒ Card
Create unpublished local card from given attributes.
-
#new_global_card(identity, identity_type, private_key, custom_data = {}) ⇒ Card
Create unpublished global card from given attributes.
-
#publish_as_global_card(card) ⇒ Card
Publishes Global card in Virgil cards service.
-
#ra_connection ⇒ HTTP::CardsServiceConnection
The Virgil Registration Authority service connection used for creating and revoking cards.
-
#read_cards_connection ⇒ HTTP::CardsServiceConnection
Cards service connection used for getting and searching cards.
-
#request_signer ⇒ RequestSigner
Request signer for signing constructed requests.
-
#revoke_card(card_id, app_id, app_key, reason = Requests::RevokeCardRequest::Reasons::Unspecified) ⇒ void
Revoke card by id.
-
#revoke_card_from_signed_request(revocation_request) ⇒ void
Revoke card using signed revocation request.
-
#revoke_global_card(card_id, key_pair, validation_token, reason = Requests::RevokeCardRequest::Reasons::Unspecified) ⇒ void
Revoke Global card by id.
-
#search_cards_by_app_bundle(bundle) ⇒ Array<Card>
Search cards by specified app bundle.
-
#search_cards_by_criteria(search_criteria) ⇒ Array<Card>
Search cards by specified search criteria.
-
#search_cards_by_identities(*identities) ⇒ Array<Card>
Search cards by specified identities.
-
#sign_and_publish_card(card, app_id, app_key) ⇒ Card
Signs and publishes card in Virgil cards service.
-
#validate_cards(cards) ⇒ Object
Validate cards signatures.
-
#verify_identity(identity, identity_type, extra_fields = {}) ⇒ String
Sends the request for identity verification, that's will be processed depending of specified type.
Constructor Details
#initialize(access_token = nil, cards_service_url = Card::SERVICE_URL, cards_read_only_service_url = Card::READ_ONLY_SERVICE_URL, identity_service_url = HighLevel::VirgilIdentity::IDENTITY_SERVICE_URL, ra_service_url = Card::RA_SERVICE_URL) ⇒ VirgilClient
Initializes a new instance of the Virgil::SDK::Client::VirgilClient class.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 69 def initialize( access_token=nil, cards_service_url=Card::SERVICE_URL, cards_read_only_service_url=Card::READ_ONLY_SERVICE_URL, identity_service_url=HighLevel::VirgilIdentity::IDENTITY_SERVICE_URL, ra_service_url=Card::RA_SERVICE_URL ) self.access_token = access_token self.cards_service_url = cards_service_url self.cards_read_only_service_url = cards_read_only_service_url self.identity_service_url = identity_service_url self.ra_service_url = ra_service_url end |
Instance Attribute Details
#access_token ⇒ String
Provides an authenticated secure access to the Virgil Security services.
48 49 50 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 48 def access_token @access_token end |
#card_validator ⇒ CardValidator
Returns the current value of card_validator
48 49 50 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 48 def card_validator @card_validator end |
#cards_read_only_service_url ⇒ String
Virgil Cards RO service url
48 49 50 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 48 def cards_read_only_service_url @cards_read_only_service_url end |
#cards_service_url ⇒ String
Virgil Cards service url
48 49 50 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 48 def cards_service_url @cards_service_url end |
#identity_service_url ⇒ String
Virgil Identity service url
48 49 50 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 48 def identity_service_url @identity_service_url end |
#ra_service_url ⇒ String
Virgil RA service url
48 49 50 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 48 def ra_service_url @ra_service_url end |
Instance Method Details
#add_relation(request) ⇒ Card
Adds a relation for the Virgil Card to Virgil cards service.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 283 def add_relation(request) unless (request.is_a?(Requests::AddRelationRequest) && !request.snapshot.nil? && request.signatures.count == 1) raise ArgumentError.new("Request is not valid. Request must have snapshot and exactly 1 relation signature.") end http_request = Client::HTTP::Request.new( method: Client::HTTP::Request::POST, endpoint: "/#{Card::VC_VERSION}/card/#{request.signatures.keys.first}/collections/relations", body: request.request_model ) raw_response = self.cards_connection.send_request(http_request) card = Card.from_response(raw_response) self.validate_cards([card]) if self.card_validator card end |
#cards_connection ⇒ HTTP::CardsServiceConnection
Cards service connection used for add and delete relations.
568 569 570 571 572 573 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 568 def cards_connection @_cards_connection ||= HTTP::CardsServiceConnection.new( self.access_token, self.cards_service_url ) end |
#confirm_identity(action_id, confirmation_code, time_to_live, count_to_live) ⇒ String
Confirms the identity using confirmation code, that has been generated to confirm an identity.
443 444 445 446 447 448 449 450 451 452 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 443 def confirm_identity(action_id, confirmation_code, time_to_live, count_to_live) confirm_request = Requests::ConfirmIdentityRequest.new(confirmation_code, action_id, time_to_live, count_to_live) http_request = Client::HTTP::Request.new( method: HTTP::Request::POST, endpoint: "/#{Card::VRA_VERSION}/confirm", body: confirm_request.request_model ) raw_response = self.identity_service_connection.send_request(http_request) raw_response['validation_token'] end |
#create_card(identity, identity_type, key_pair, app_id, app_key) ⇒ Card
Create published new card from given attributes.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 109 def create_card(identity, identity_type, key_pair, app_id, app_key) request = Client::Requests::CreateCardRequest.new( identity: identity, identity_type: identity_type, scope: Client::Card::APPLICATION, raw_public_key: self.crypto.export_public_key(key_pair.public_key) ) self.request_signer.self_sign(request, key_pair.private_key) self.request_signer.(request, app_id, app_key) return self.create_card_from_signed_request(request) end |
#create_card_from_signed_request(create_request) ⇒ Card
Create new published card from signed creation request.
240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 240 def create_card_from_signed_request(create_request) http_request = Client::HTTP::Request.new( method: Client::HTTP::Request::POST, endpoint: "/#{Card::VRA_VERSION}/card", body: create_request.request_model ) raw_response = self.ra_connection.send_request(http_request) card = Card.from_response(raw_response) self.validate_cards([card]) if self.card_validator card end |
#crypto ⇒ Cryptography::VirgilCrypto
Crypto library wrapper.
616 617 618 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 616 def crypto @_crypto ||= Cryptography::VirgilCrypto.new end |
#delete_relation(request) ⇒ Card
Deletes a relation for the Virgil Card to Virgil cards service.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 313 def delete_relation(request) unless (request.is_a?(Requests::DeleteRelationRequest) && !request.snapshot.nil? && request.signatures.count == 1) raise ArgumentError.new("Request is not valid. Request must have snapshot and exactly 1 relation signature.") end http_request = Client::HTTP::Request.new( method: Client::HTTP::Request::DELETE, endpoint: "/#{Card::VC_VERSION}/card/#{request.signatures.keys.first}/collections/relations", body: request.request_model ) raw_response = self.cards_connection.send_request(http_request) card = Card.from_response(raw_response) self.validate_cards([card]) if self.card_validator card end |
#get_card(card_id) ⇒ Card
Get card by id.
463 464 465 466 467 468 469 470 471 472 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 463 def get_card(card_id) http_request = Client::HTTP::Request.new( method: HTTP::Request::GET, endpoint: "/#{Card::VC_VERSION}/card/#{card_id}", ) raw_response = self.read_cards_connection.send_request(http_request) card = Card.from_response(raw_response) self.validate_cards([card]) if self.card_validator card end |
#identity_service_connection ⇒ HTTP::CardsServiceConnection
Virgil Identity service connection used for validation of user's identities like email, application, etc.
598 599 600 601 602 603 604 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 598 def identity_service_connection @identity_service_connection = HTTP::CardsServiceConnection.new( nil, self.identity_service_url ) end |
#new_card(identity, identity_type, private_key, custom_data = {}) ⇒ Card
Create unpublished local card from given attributes.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 144 def new_card(identity, identity_type, private_key, custom_data={}) data = custom_data[:data] custom_data.delete(:data) request = Client::Requests::CreateCardRequest.new( identity: identity, identity_type: identity_type, scope: Client::Card::APPLICATION, raw_public_key: self.crypto.extract_public_key(private_key).value, info: custom_data, data: data ) self.request_signer.self_sign(request, private_key) return Client::Card.from_request_model(request.request_model) end |
#new_global_card(identity, identity_type, private_key, custom_data = {}) ⇒ Card
Create unpublished global card from given attributes.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 170 def new_global_card(identity, identity_type, private_key, custom_data={}) data = custom_data[:data] custom_data.delete(:data) request = Client::Requests::CreateCardRequest.new( identity: identity, identity_type: identity_type, scope: Client::Card::GLOBAL, raw_public_key: self.crypto.extract_public_key(private_key).value, info: custom_data, data: data ) self.request_signer.self_sign(request, private_key) return Client::Card.from_request_model(request.request_model) end |
#publish_as_global_card(card) ⇒ Card
Publishes Global card in Virgil cards service.
209 210 211 212 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 209 def publish_as_global_card(card) request = card.to_request create_card_from_signed_request(request) end |
#ra_connection ⇒ HTTP::CardsServiceConnection
The Virgil Registration Authority service connection used for creating and revoking cards.
578 579 580 581 582 583 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 578 def ra_connection @_ra_connection ||= HTTP::CardsServiceConnection.new( self.access_token, self.ra_service_url ) end |
#read_cards_connection ⇒ HTTP::CardsServiceConnection
Cards service connection used for getting and searching cards.
588 589 590 591 592 593 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 588 def read_cards_connection @_read_cards_connection = HTTP::CardsServiceConnection.new( self.access_token, self.cards_read_only_service_url ) end |
#request_signer ⇒ RequestSigner
Request signer for signing constructed requests.
609 610 611 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 609 def request_signer @_request_signer ||= RequestSigner.new(self.crypto) end |
#revoke_card(card_id, app_id, app_key, reason = Requests::RevokeCardRequest::Reasons::Unspecified) ⇒ void
This method returns an undefined value.
Revoke card by id.
346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 346 def revoke_card( card_id, app_id, app_key, reason=Requests::RevokeCardRequest::Reasons::Unspecified ) request = Requests::RevokeCardRequest.new( card_id: card_id, reason: reason ) self.request_signer.(request, app_id, app_key) self.revoke_card_from_signed_request(request) end |
#revoke_card_from_signed_request(revocation_request) ⇒ void
This method returns an undefined value.
Revoke card using signed revocation request.
409 410 411 412 413 414 415 416 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 409 def revoke_card_from_signed_request(revocation_request) http_request = Client::HTTP::Request.new( method: HTTP::Request::DELETE, endpoint: "/#{Card::VRA_VERSION}/card/#{revocation_request.card_id}", body: revocation_request.request_model ) self.ra_connection.send_request(http_request) end |
#revoke_global_card(card_id, key_pair, validation_token, reason = Requests::RevokeCardRequest::Reasons::Unspecified) ⇒ void
This method returns an undefined value.
Revoke Global card by id.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 369 def revoke_global_card( card_id, key_pair, validation_token, reason=Requests::RevokeCardRequest::Reasons::Unspecified ) request = Requests::RevokeCardRequest.new( card_id: card_id, reason: reason ) request.restore(validation_token) self.request_signer.(request, card_id, key_pair.private_key) self.revoke_card_from_signed_request(request) end |
#search_cards_by_app_bundle(bundle) ⇒ Array<Card>
Search cards by specified app bundle.
500 501 502 503 504 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 500 def search_cards_by_app_bundle(bundle) return self.search_cards_by_criteria( SearchCriteria.by_app_bundle(bundle) ) end |
#search_cards_by_criteria(search_criteria) ⇒ Array<Card>
Search cards by specified search criteria.
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 518 def search_cards_by_criteria(search_criteria) body = {identities: search_criteria.identities} if search_criteria.identity_type body[:identity_type] = search_criteria.identity_type end if search_criteria.scope == Card::GLOBAL body[:scope] = Card::GLOBAL end http_request = Client::HTTP::Request.new( method: HTTP::Request::POST, endpoint: "/#{Card::VC_VERSION}/card/actions/search", body: body, ) response = nil begin response = self.read_cards_connection.send_request(http_request) rescue end cards = [] if response cards = response.map { |card| Card.from_response(card) } self.validate_cards(cards) if self.card_validator end return cards end |
#search_cards_by_identities(*identities) ⇒ Array<Card>
Search cards by specified identities.
483 484 485 486 487 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 483 def search_cards_by_identities(*identities) return self.search_cards_by_criteria( SearchCriteria.by_identities(identities) ) end |
#sign_and_publish_card(card, app_id, app_key) ⇒ Card
Signs and publishes card in Virgil cards service.
195 196 197 198 199 200 201 202 203 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 195 def sign_and_publish_card(card, app_id, app_key) request = card.to_request request_signer.( request, app_id, app_key ) create_card_from_signed_request(request) end |
#validate_cards(cards) ⇒ Object
Validate cards signatures.
558 559 560 561 562 563 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 558 def validate_cards(cards) invalid_cards = cards.select { |card| !card_validator.is_valid?(card) } if invalid_cards.any? raise InvalidCardException.new(invalid_cards) end end |
#verify_identity(identity, identity_type, extra_fields = {}) ⇒ String
use method confirm_identity to confirm and get the identity token.
Sends the request for identity verification, that's will be processed depending of specified type.
425 426 427 428 429 430 431 432 433 434 |
# File 'lib/virgil/sdk/client/virgil_client.rb', line 425 def verify_identity(identity, identity_type, extra_fields = {}) verify_identity_request = Requests::VerifyIdentityRequest.new(identity, identity_type, extra_fields) http_request = Client::HTTP::Request.new( method: HTTP::Request::POST, endpoint: "/#{Card::VRA_VERSION}/verify", body: verify_identity_request.request_model ) raw_response = self.identity_service_connection.send_request(http_request) raw_response['action_id'] end |