Class: Silkey::Models::JwtPayload
- Inherits:
-
Object
- Object
- Silkey::Models::JwtPayload
- Defined in:
- lib/silkey/models/jwt_payload.rb
Constant Summary collapse
- SCOPE_DIVIDER =
','
Instance Method Summary collapse
- #import(hash) ⇒ Object
- #message_to_sign_by_silkey ⇒ Object
-
#message_to_sign_by_user ⇒ Object
Creates message that’s need to be sign by user.
-
#scope ⇒ Object
rubocop:enable Style/HashSyntax.
- #scope_divider ⇒ Object
- #set_address(addr) ⇒ Object
- #set_email(email) ⇒ Object
- #set_ref_id(ref_id) ⇒ Object
-
#set_scope(scope) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
- #set_silkey_signature(sig, timestamp) ⇒ Object
- #set_user_signature(sig, timestamp) ⇒ Object
- #validate ⇒ Object
Instance Method Details
#import(hash) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/silkey/models/jwt_payload.rb', line 126 def import(hash) hash.each do |k, v| var = k.to_s.underscore if k == 'scope' set_scope(v) else self.instance_variable_set("@#{var}", v) end end self end |
#message_to_sign_by_silkey ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/silkey/models/jwt_payload.rb', line 97 def return '' if Silkey::Utils.empty?(email) if Silkey::Utils.empty?() self. = Silkey::Utils. end str_hex = [ 'email', email, 'silkeySignatureTimestamp' ].map { |str| str.to_s.unpack('H*') }.join('') "#{str_hex}#{Silkey::Utils.int_to_hex(.to_s)}" end |
#message_to_sign_by_user ⇒ Object
Creates message that’s need to be sign by user
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/silkey/models/jwt_payload.rb', line 76 def if !Silkey::Utils.empty?(address) && Silkey::Utils.empty?() self. = Silkey::Utils. end str1_hex = 'address'.unpack('H*')[0] adr_hex = Silkey::Utils.remove0x(address).downcase str2_hex = [ 'refId', ref_id.to_s, 'scope', scope, 'userSignatureTimestamp' ].map { |str| str.to_s.unpack('H*') }.join('') str_hex = "#{str1_hex}#{adr_hex}#{str2_hex}" return str_hex if Silkey::Utils.empty?() "#{str_hex}#{Silkey::Utils.int_to_hex(.to_s)}" end |
#scope ⇒ Object
rubocop:enable Style/HashSyntax
21 22 23 |
# File 'lib/silkey/models/jwt_payload.rb', line 21 def scope _scope.keys.sort.join(SCOPE_DIVIDER) end |
#scope_divider ⇒ Object
25 26 27 |
# File 'lib/silkey/models/jwt_payload.rb', line 25 def scope_divider SCOPE_DIVIDER end |
#set_address(addr) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/silkey/models/jwt_payload.rb', line 37 def set_address(addr) raise "`#{addr}` is not ethereum address" unless Silkey::Utils.ethereum_address?(addr) self.address = addr self end |
#set_email(email) ⇒ Object
44 45 46 47 |
# File 'lib/silkey/models/jwt_payload.rb', line 44 def set_email(email) self.email = email self end |
#set_ref_id(ref_id) ⇒ Object
49 50 51 52 |
# File 'lib/silkey/models/jwt_payload.rb', line 49 def set_ref_id(ref_id) self.ref_id = ref_id self end |
#set_scope(scope) ⇒ Object
rubocop:disable Naming/AccessorMethodName
30 31 32 33 34 35 |
# File 'lib/silkey/models/jwt_payload.rb', line 30 def set_scope(scope) return self if Silkey::Utils.empty?(scope) _scope[scope] = true self end |
#set_silkey_signature(sig, timestamp) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/silkey/models/jwt_payload.rb', line 63 def set_silkey_signature(sig, ) raise "silkey signature invalid: `#{sig}`" unless Silkey::Utils.signature?(sig) raise 'empty silkey signature timestamp' if Silkey::Utils.empty?() self.silkey_signature = sig self. = self end |
#set_user_signature(sig, timestamp) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/silkey/models/jwt_payload.rb', line 54 def set_user_signature(sig, ) raise "user signature invalid: `#{sig}`" unless Silkey::Utils.signature?(sig) raise 'empty user signature timestamp' if Silkey::Utils.empty?() self.user_signature = sig self. = self end |
#validate ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/silkey/models/jwt_payload.rb', line 112 def validate raise "address is invalid: #{address}" unless Silkey::Utils.ethereum_address?(address) unless Silkey::Utils.signature?(user_signature) raise "user_signature is invalid: #{user_signature}" end raise 'user_signature_timestamp is empty' if Silkey::Utils.empty?() return self if Silkey::Utils.empty?(scope) || scope == 'id' validate_scope_email end |