Class: GLogin::Cookie::Closed

Inherits:
Object
  • Object
show all
Defined in:
lib/glogin/cookie.rb

Overview

Closed cookie.

An instance of this class is created when a cookie arrives to the application. The cookie text is provided to the class as the first parameter. Then, when an instance of the class is created, the value encypted inside the cookie text may be retrieved through the to_user method.

Instance Method Summary collapse

Constructor Details

#initialize(text, secret, context = '') ⇒ Closed

Returns a new instance of Closed.



49
50
51
52
53
54
55
56
# File 'lib/glogin/cookie.rb', line 49

def initialize(text, secret, context = '')
  raise 'Text can\'t be nil' if text.nil?
  @text = text
  raise 'Secret can\'t be nil' if secret.nil?
  @secret = secret
  raise 'Context can\'t be nil' if context.nil?
  @context = context.to_s
end

Instance Method Details

#to_userObject

Returns a hash with four elements: ‘id`, `login`, `avatar`, and `bearer`.

If the ‘secret` is empty, the text will not be decrypted, but used “as is”. This may be helpful during testing.

If the data is not valid, an exception ‘GLogin::Codec::DecodingError` will be raised, which you have to catch in your applicaiton and ignore the login attempt.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/glogin/cookie.rb', line 66

def to_user
  plain = Codec.new(@secret).decrypt(@text)
  id, , avatar, bearer, ctx = plain.split(GLogin::SPLIT, 5)
  if !@secret.empty? && ctx.to_s != @context
    raise(
      GLogin::Codec::DecodingError,
      "Context '#{@context}' expected, but '#{ctx}' found"
    )
  end
  { id: id, login: , avatar: avatar, bearer: bearer }
end