Class: Virgil::SDK::HighLevel::VirgilCard

Inherits:
Object
  • Object
show all
Defined in:
lib/virgil/sdk/high_level/virgil_card.rb

Overview

A Virgil Card is the main entity of the Virgil Security services, it includes an information about the user and his public key. The Virgil Card identifies the user by one of his available types, such as an email, a phone number, etc.

Defined Under Namespace

Classes: AppCredentialsException

Instance Method Summary collapse

Constructor Details

#initialize(context:, card:) ⇒ VirgilCard

Initializes a new instance of the Virgil::SDK::HighLevel::VirgilCard class.



54
55
56
57
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 54

def initialize(context:, card:)
  @context = context
  @card = card
end

Instance Method Details

#check_identity(identity_options = nil) ⇒ VirgilIdentity::VerificationAttempt

Initiates an identity verification process for current Card identity type. It is only working for Global identity types like Email.

Parameters:

Returns:



202
203
204
205
206
207
208
209
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 202

def check_identity(identity_options = nil)

  extra_fields = identity_options ? identity_options.extra_fields : {}
  action_id = context.client.verify_identity(identity, identity_type, extra_fields)
  VirgilIdentity::VerificationAttempt.new(context: context, action_id: action_id,
                                          identity: identity, identity_type: identity_type,
                                          additional_options: identity_options)
end

#dataObject



90
91
92
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 90

def data
  card.data
end

#deviceObject



105
106
107
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 105

def device
  card.device
end

#device_nameObject



110
111
112
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 110

def device_name
  card.device_name
end

#encrypt(buffer) ⇒ VirgilBuffer

Encrypts the specified data for current Virgil card recipient

Examples:

virgil = VirgilApi.new( access_token: "[YOUR_ACCESS_TOKEN_HERE]")
alice_card = virgil.cards.get("[USER_CARD_ID_HERE]")

file_buf = VirgilBuffer.from_file("[FILE_NAME_HERE]")

# encrypt the buffer using found Virgil Cards
cipher_file_buf = alice_card.encrypt(file_buf)

Parameters:

  • buffer (VirgilBuffer, Crypto::Bytes, String)

    The data to be encrypted. It can be VirgilBuffer, utf8-String or Array of bytes

Returns:

  • (VirgilBuffer)

    Encrypted data for current Virgil card recipient

Raises:

  • (ArgumentError)

    if buffer doesn't have type VirgilBuffer, String or Array of bytes

See Also:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 179

def encrypt(buffer)

  buffer_to_encrypt = case buffer.class.name.split("::").last
                        when 'VirgilBuffer'
                          buffer
                        when 'String'
                          VirgilBuffer.from_string(buffer)
                        when 'Array'
                          VirgilBuffer.from_bytes(buffer)
                        else
                          raise ArgumentError.new("Buffer has unsupported type")
                      end


  VirgilBuffer.new(context.crypto.encrypt(buffer_to_encrypt.bytes, public_key))
end

#exportString

Exports card's snapshot.



123
124
125
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 123

def export
  card.export
end

#idString

unique identifier for the Virgil Card.

Returns:

  • (String)


71
72
73
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 71

def id
  card.id
end

#identityString

the value of current Virgil Card identity.

Returns:

  • (String)


78
79
80
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 78

def identity
  card.identity
end

#identity_typeString

the identityType of current Virgil Card identity.

Returns:

  • (String)


85
86
87
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 85

def identity_type
  card.identity_type
end

#public_keyObject



100
101
102
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 100

def public_key
  context.crypto.import_public_key(card.public_key)
end

#publishObject

Publish synchronously the card into application Virgil Services scope

Examples:

alice_card.publish

Raises:

See Also:



136
137
138
139
140
141
142
143
144
145
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 136

def publish

  raise NotImplementedError.new("Current card isn't local!") unless @card.scope == Client::Card::APPLICATION
  validate_app_credentials

  @card = context.client.sign_and_publish_card(
      card,
      context.credentials.app_id,
      context.credentials.app_key(context.crypto))
end

#publish_as_global(validation_token) ⇒ Object

Publish synchronously the global card into application Virgil Services scope

Parameters:

Raises:



154
155
156
157
158
159
160
161
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 154

def publish_as_global(validation_token)

  raise NotImplementedError.new("Current card isn't global!") unless @card.scope == Client::Card::GLOBAL

  @card.validation_token = validation_token
  @card = context.client.publish_as_global_card(card)
  @card.validation_token = validation_token
end

#scopeObject



95
96
97
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 95

def scope
  card.scope
end

#verify(buffer, signature) ⇒ Boolean

Verifies the specified buffer and signature with current VirgilCard recipient

Examples:

virgil = VirgilApi.new(access_token: "[YOUR_ACCESS_TOKEN_HERE]")
# search for Alice's Card
alice_card = virgil.cards.get("[ALICE_CARD_ID]")

unless alice_card.verify(message, signature)
    raise "Alice it's not you."
end

Parameters:

  • buffer (VirgilBuffer, Crypto::Bytes, String)

    The data to be verified. It can be VirgilBuffer, utf8-encoded String or Array of bytes

  • signature (VirgilBuffer, Crypto::Bytes, String)

    The signature used to verify the data integrity. It can be VirgilBuffer, base64-encoded String or Array of bytes

Returns:

  • (Boolean)

    true if signature is valid, false otherwise.

Raises:

  • (ArgumentError)

    if buffer doesn't have type VirgilBuffer, Array of bytes or utf8-encoded String

  • (ArgumentError)

    if signature doesn't have type VirgilBuffer, base64-encoded String or Array of bytes

See Also:



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/virgil/sdk/high_level/virgil_card.rb', line 230

def verify(buffer, signature)

  buffer_to_verify = case buffer.class.name.split("::").last
                       when 'VirgilBuffer'
                         buffer
                       when 'String'
                         VirgilBuffer.from_string(buffer)
                       when 'Array'
                         VirgilBuffer.from_bytes(buffer)
                       else
                         raise ArgumentError.new("Buffer has unsupported type")
                     end

  signature_to_verify = case signature.class.name.split("::").last
                          when 'VirgilBuffer'
                            signature
                          when 'String'
                            VirgilBuffer.from_base64(signature)
                          when 'Array'
                            VirgilBuffer.from_bytes(signature)
                          else
                            raise ArgumentError.new("Signature has unsupported type")
                        end
  context.crypto.verify(buffer_to_verify.bytes, signature_to_verify.bytes, public_key)
end