Class: MagicAdmin::Resource::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/magic-admin/resource/token.rb

Overview

The token resource and its methods are accessible on the Magic instance by the Token attribute. It provides methods to interact with the DID Token.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(magic) ⇒ Token

The constructor allows you to create a token object when your application interacting with the Magic API

Arguments:

magic: A Magic object.

Returns:

A token object that provides access to all the supported resources.

Examples:

Token.new(<magic>)


26
27
28
# File 'lib/magic-admin/resource/token.rb', line 26

def initialize(magic)
  @magic = magic
end

Instance Attribute Details

#magicObject (readonly)

attribute reader for magic client object



13
14
15
# File 'lib/magic-admin/resource/token.rb', line 13

def magic
  @magic
end

Instance Method Details

#construct_issuer_with_public_address(public_address) ⇒ Object

Description:

Method parse public_address and extract issuer

Arguments:

public_address: Cryptographic public address of the Magic User.

Returns:

issuer info


83
84
85
# File 'lib/magic-admin/resource/token.rb', line 83

def construct_issuer_with_public_address(public_address)
  "did:ethr:#{public_address}"
end

#decode(did_token) ⇒ Object

Description:

Method Decodes a DID Token from a Base64 string into
a tuple of its individual components: proof and claim.
This method allows you decode the DID Token
and inspect the token

Arguments:

did_token: A DID Token generated by a Magic user on the client-side.

Returns:

An array containing proof and claim or raise an error


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/magic-admin/resource/token.rb', line 61

def decode(did_token)
  proof = nil
  claim = nil
  begin
    token_array = JSON.parse(base64_decode(did_token))
    proof = token_array[0]
    claim = JSON.parse(token_array[1])
    validate_claim_fields!(claim)
  rescue JSON::ParserError, ArgumentError
    raise DIDTokenError, "DID Token is malformed"
  end
  [proof, claim]
end

#get_issuer(did_token) ⇒ Object

Description:

Method parse did_token and extract issuer

Arguments:

did_token: A DID Token generated by a Magic user on the client-side.

Returns:

issuer info


95
96
97
# File 'lib/magic-admin/resource/token.rb', line 95

def get_issuer(did_token)
  decode(did_token).last["iss"]
end

#get_public_address(did_token) ⇒ Object

Description:

Method parse did_token and extract  cryptographic public_address

Arguments:

did_token: A DID Token generated by a Magic user on the client-side.

Returns:

cryptographic public address of the Magic User
who generated the supplied DID Token.


108
109
110
# File 'lib/magic-admin/resource/token.rb', line 108

def get_public_address(did_token)
  get_issuer(did_token).split(":").last
end

#validate(did_token) ⇒ Object

Description:

Method validate did_token

Arguments:

did_token: A DID Token generated by a Magic user on the client-side.

Returns:

true or raise an error


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/magic-admin/resource/token.rb', line 38

def validate(did_token)
  time = Time.now.to_i
  proof, claim = decode(did_token)
  rec_address = rec_pub_address(claim, proof).to_s

  validate_public_address!(rec_address, did_token)
  validate_claim_fields!(claim)
  validate_claim_ext!(time, claim["ext"])
  validate_claim_nbf!(time, claim["nbf"])
  validate_claim_aud!(magic.client_id, claim["aud"])
end