Module: Himari::TokenString

Included in:
AccessToken, SessionData
Defined in:
lib/himari/token_string.rb

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

#expiryObject



45
46
47
# File 'lib/himari/token_string.rb', line 45

def expiry
  @expiry
end

#formatObject



92
93
94
# File 'lib/himari/token_string.rb', line 92

def format
  Format.new(header: magic_header, handle: handle, secret: secret)
end

#handleObject



41
42
43
# File 'lib/himari/token_string.rb', line 41

def handle
  @handle
end

#magic_headerObject



88
89
90
# File 'lib/himari/token_string.rb', line 88

def magic_header
  self.class.magic_header
end

#secretObject

Raises:



49
50
51
52
# File 'lib/himari/token_string.rb', line 49

def secret
  raise SecretMissing unless @secret
  @secret
end

#secret_hashObject



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

Raises:



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

Raises:



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) # TODO: rescue errors
  given_dgst = Digest::SHA384.digest(given_secret)
  raise SecretIncorrect unless Rack::Utils.secure_compare(dgst, given_dgst)
  @secret = given_secret
  true
end