Module: Himari::TokenString
Defined Under Namespace
Modules: ClassMethods
Classes: Error, Format, InvalidFormat, SecretIncorrect, SecretMissing, TokenExpired
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(k) ⇒ Object
37
38
39
|
# File 'lib/himari/token_string.rb', line 37
def self.included(k)
k.extend(ClassMethods)
end
|
Instance Method Details
#expiry ⇒ Object
45
46
47
|
# File 'lib/himari/token_string.rb', line 45
def expiry
@expiry
end
|
92
93
94
|
# File 'lib/himari/token_string.rb', line 92
def format
Format.new(header: , handle: handle, secret: secret)
end
|
#handle ⇒ Object
41
42
43
|
# File 'lib/himari/token_string.rb', line 41
def handle
@handle
end
|
88
89
90
|
# File 'lib/himari/token_string.rb', line 88
def
self.class.
end
|
#secret ⇒ Object
49
50
51
52
|
# File 'lib/himari/token_string.rb', line 49
def secret
raise SecretMissing unless @secret
@secret
end
|
#secret_hash ⇒ Object
54
55
56
|
# File 'lib/himari/token_string.rb', line 54
def secret_hash
@secret_hash ||= Base64.urlsafe_encode64(Digest::SHA384.digest(secret), padding: false)
end
|
#verify!(secret:, now: Time.now) ⇒ Object
58
59
60
61
|
# File 'lib/himari/token_string.rb', line 58
def verify!(secret:, now: Time.now)
verify_expiry!(now)
verify_secret!(secret)
end
|
#verify_expiry!(now = Time.now) ⇒ Object
71
72
73
|
# File 'lib/himari/token_string.rb', line 71
def verify_expiry!(now = Time.now)
raise TokenExpired if @expiry <= now.to_i
end
|
#verify_secret!(given_secret) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/himari/token_string.rb', line 63
def verify_secret!(given_secret)
dgst = Base64.urlsafe_decode64(secret_hash) given_dgst = Digest::SHA384.digest(given_secret)
raise SecretIncorrect unless Rack::Utils.secure_compare(dgst, given_dgst)
@secret = given_secret
true
end
|