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 Method Summary collapse

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


64
65
66
# File 'lib/magic-admin/resource/token.rb', line 64

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


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/magic-admin/resource/token.rb', line 42

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


76
77
78
# File 'lib/magic-admin/resource/token.rb', line 76

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.


89
90
91
# File 'lib/magic-admin/resource/token.rb', line 89

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


20
21
22
23
24
25
26
27
28
29
# File 'lib/magic-admin/resource/token.rb', line 20

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

  validate_public_address!(rec_address, did_token)
  validate_claim_fields!(claim)
  validate_claim_ext!(time, claim["ext"])
  validate_claim_nbf!(time, claim["nbf"])
end