Class: Saro::Dat::Dat

Inherits:
Object
  • Object
show all
Defined in:
lib/saro/dat/dat.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dat_str) ⇒ Dat

Returns a new instance of Dat.



13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/saro/dat/dat.rb', line 13

def initialize(dat_str)
  @dat = dat_str || ''
  @format = false
  @expire = 0
  @cid = 0
  @plain = "".b
  @secure = "".b
  @signature = "".b
  @error = nil

  if @dat.empty?
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::TOKEN_MALFORMED, "token is empty")
    return
  end

  parts = @dat.split('.', -1)
  if parts.length != 5
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::TOKEN_MALFORMED, "expected exactly 5 dot-separated fields")
    return
  end

  begin
    @expire = Saro::Dat::Util.parse_u64(parts[0])
  rescue Saro::Dat::Error => e
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::TOKEN_MALFORMED, "expire field is not a plain decimal u64", cause: e)
    return
  end

  begin
    @cid = Saro::Dat::Util.parse_u64_hex(parts[1])
  rescue Saro::Dat::Error => e
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::TOKEN_MALFORMED, "cid field is not a plain hex u64", cause: e)
    return
  end

  begin
    @plain = Saro::Dat::Util.decode_base64_url(parts[2])
  rescue Saro::Dat::Error => e
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::TOKEN_MALFORMED, "plain field is not base64url", cause: e)
    return
  end

  begin
    @secure = Saro::Dat::Util.decode_base64_url(parts[3])
  rescue Saro::Dat::Error => e
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::TOKEN_MALFORMED, "secure field is not base64url", cause: e)
    return
  end

  if parts[4].empty?
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::SIG_MALFORMED, "signature field is empty")
    return
  end

  begin
    @signature = Saro::Dat::Util.decode_base64_url(parts[4])
  rescue Saro::Dat::Error => e
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::SIG_MALFORMED, "signature field is not base64url", cause: e)
    return
  end

  if @signature.empty?
    @error = Saro::Dat::Error.new(Saro::Dat::ErrorCode::SIG_MALFORMED, "signature field is empty")
    return
  end

  @format = true
end

Instance Attribute Details

#cidObject (readonly)

Returns the value of attribute cid.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def cid
  @cid
end

#datObject (readonly)

Returns the value of attribute dat.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def dat
  @dat
end

#errorObject (readonly)

Returns the value of attribute error.



11
12
13
# File 'lib/saro/dat/dat.rb', line 11

def error
  @error
end

#expireObject (readonly)

Returns the value of attribute expire.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def expire
  @expire
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def format
  @format
end

#plainObject (readonly)

Returns the value of attribute plain.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def plain
  @plain
end

#secureObject (readonly)

Returns the value of attribute secure.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def secure
  @secure
end

#signatureObject (readonly)

Returns the value of attribute signature.



9
10
11
# File 'lib/saro/dat/dat.rb', line 9

def signature
  @signature
end

Class Method Details

.from_value(value) ⇒ Object



87
88
89
90
# File 'lib/saro/dat/dat.rb', line 87

def self.from_value(value)
  return value if value.is_a?(Dat)
  new(value)
end

Instance Method Details

#body_stringObject



99
100
101
102
103
# File 'lib/saro/dat/dat.rb', line 99

def body_string
  idx = @dat.rindex('.')
  return "" unless idx
  @dat[0, idx]
end

#expiredObject Also known as: expired?



92
93
94
95
# File 'lib/saro/dat/dat.rb', line 92

def expired
  return true unless @format
  Time.now.to_i >= @expire
end

#raise_if_invalid!Object

Raises:



82
83
84
85
# File 'lib/saro/dat/dat.rb', line 82

def raise_if_invalid!
  raise @error if @error
  nil
end