Class: JOSE::JWT

Inherits:
Struct
  • Object
show all
Defined in:
lib/jose/jwt.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields

Returns:

  • (Object)

    the current value of fields


3
4
5
# File 'lib/jose/jwt.rb', line 3

def fields
  @fields
end

Class Method Details

.decrypt(jwk, encrypted) ⇒ Object

API


70
71
72
73
# File 'lib/jose/jwt.rb', line 70

def self.decrypt(jwk, encrypted)
  decrypted, jwe = JOSE::JWK.block_decrypt(jwk, encrypted)
  return from_binary(decrypted), jwe
end

.encrypt(jwt, jwk, jwe = nil) ⇒ Object


75
76
77
# File 'lib/jose/jwt.rb', line 75

def self.encrypt(jwt, jwk, jwe = nil)
  return from(jwt).encrypt(jwk, jwe)
end

.from(object, modules = {}) ⇒ Object

Decode API


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jose/jwt.rb', line 7

def self.from(object, modules = {})
  case object
  when JOSE::Map, Hash
    return from_map(object, modules)
  when String
    return from_binary(object, modules)
  when JOSE::JWT
    return object
  else
    raise ArgumentError, "'object' must be a Hash, String, or JOSE::JWT"
  end
end

.from_binary(object, modules = {}) ⇒ Object


20
21
22
23
24
25
26
27
# File 'lib/jose/jwt.rb', line 20

def self.from_binary(object, modules = {})
  case object
  when String
    return from_map(JOSE.decode(object), modules)
  else
    raise ArgumentError, "'object' must be a String"
  end
end

.from_file(file, modules = {}) ⇒ Object


29
30
31
# File 'lib/jose/jwt.rb', line 29

def self.from_file(file, modules = {})
  return from_binary(File.binread(file), modules)
end

.from_map(object, modules = {}) ⇒ Object


33
34
35
36
37
38
39
40
# File 'lib/jose/jwt.rb', line 33

def self.from_map(object, modules = {})
  case object
  when JOSE::Map, Hash
    return from_fields(JOSE::JWT.new(JOSE::Map.new(object)), modules)
  else
    raise ArgumentError, "'object' must be a Hash"
  end
end

.merge(left, right) ⇒ Object


94
95
96
# File 'lib/jose/jwt.rb', line 94

def self.merge(left, right)
  return from(left).merge(right)
end

.peek_payload(signed) ⇒ Object


112
113
114
# File 'lib/jose/jwt.rb', line 112

def self.peek_payload(signed)
  return JOSE::Map.new(JOSE.decode(JOSE::JWS.peek_payload(signed)))
end

.peek_protected(signed) ⇒ Object


116
117
118
# File 'lib/jose/jwt.rb', line 116

def self.peek_protected(signed)
  return JOSE::JWS.peek_protected(signed)
end

.sign(jwt, jwk, jws = nil, header = nil) ⇒ Object


120
121
122
# File 'lib/jose/jwt.rb', line 120

def self.sign(jwt, jwk, jws = nil, header = nil)
  return from(jwt).sign(jwk, jws)
end

.to_binary(jwt) ⇒ Object

Encode API


44
45
46
# File 'lib/jose/jwt.rb', line 44

def self.to_binary(jwt)
  return from(jwt).to_binary
end

.to_file(jwt, file) ⇒ Object


52
53
54
# File 'lib/jose/jwt.rb', line 52

def self.to_file(jwt, file)
  return from(jwt).to_file(file)
end

.to_map(jwt) ⇒ Object


60
61
62
# File 'lib/jose/jwt.rb', line 60

def self.to_map(jwt)
  return from(jwt).to_map
end

.verify(jwk, signed) ⇒ Object


139
140
141
142
143
# File 'lib/jose/jwt.rb', line 139

def self.verify(jwk, signed)
  verified, payload, jws = JOSE::JWK.verify(signed, jwk)
  jwt = from_binary(payload)
  return verified, jwt, jws
end

.verify_strict(jwk, allow, signed) ⇒ Object


145
146
147
148
149
150
151
152
# File 'lib/jose/jwt.rb', line 145

def self.verify_strict(jwk, allow, signed)
  verified, payload, jws = JOSE::JWK.verify(signed, allow, jwk)
  jwt = payload
  if verified
    jwt = from_binary(payload)
  end
  return verified, jwt, jws
end

Instance Method Details

#encrypt(jwk, jwe = nil) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jose/jwt.rb', line 79

def encrypt(jwk, jwe = nil)
  plain_text = to_binary
  if jwe.nil?
    jwk = JOSE::JWK.from(jwk)
    jwe = jwk.block_encryptor
  end
  if jwe.is_a?(Hash)
    jwe = JOSE::Map.new(jwe)
  end
  if jwe.is_a?(JOSE::Map) and not jwe.has_key?('typ')
    jwe = jwe.put('typ', 'JWT')
  end
  return JOSE::JWK.block_encrypt(jwk, plain_text, jwe)
end

#merge(object) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jose/jwt.rb', line 98

def merge(object)
  object = case object
  when JOSE::Map, Hash
    object
  when String
    JOSE.decode(object)
  when JOSE::JWT
    object.to_map
  else
    raise ArgumentError, "'object' must be a Hash, String, or JOSE::JWT"
  end
  return JOSE::JWT.from_map(self.to_map.merge(object))
end

#sign(jwk, jws = nil, header = nil) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jose/jwt.rb', line 124

def sign(jwk, jws = nil, header = nil)
  plain_text = to_binary
  if jws.nil?
    jwk = JOSE::JWK.from(jwk)
    jws = jwk.signer
  end
  if jws.is_a?(Hash)
    jws = JOSE::Map.new(jws)
  end
  if jws.is_a?(JOSE::Map) and not jws.has_key?('typ')
    jws = jws.put('typ', 'JWT')
  end
  return JOSE::JWK.sign(jwk, plain_text, jws, header)
end

#to_binaryObject


48
49
50
# File 'lib/jose/jwt.rb', line 48

def to_binary
  return JOSE.encode(to_map)
end

#to_file(file) ⇒ Object


56
57
58
# File 'lib/jose/jwt.rb', line 56

def to_file(file)
  return File.binwrite(file, to_binary)
end

#to_mapObject


64
65
66
# File 'lib/jose/jwt.rb', line 64

def to_map
  return fields
end