Module: CWT

Defined in:
lib/cwt.rb,
lib/cwt/version.rb,
lib/cwt/claims_set.rb

Defined Under Namespace

Classes: ClaimsSet, Error

Constant Summary collapse

CBOR_TAG =
61
VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.decode(token, cose_key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cwt.rb', line 15

def self.decode(token, cose_key)
  decoded = CBOR.decode(token)

  case decoded.tag
  when CBOR_TAG
    decode(CBOR.encode(decoded.value), cose_key)
  when COSE::Sign1.tag
    key = COSE::Key.deserialize(cose_key)
    sign1 = COSE::Sign1.deserialize(token)

    if sign1.verify(key)
      CWT::ClaimsSet.from_cbor(sign1.payload)
    else
      raise(CWT::Error, "Verification failed")
    end
  when COSE::Mac0.tag
    key = COSE::Key.deserialize(cose_key)
    mac0 = COSE::Mac0.deserialize(token)

    if mac0.verify(key)
      CWT::ClaimsSet.from_cbor(mac0.payload)
    else
      raise(CWT::Error, "Verification failed")
    end
  else
    raise(CWT::Error, "Unsupported tag #{decoded.tag}")
  end
end