Class: TrivialSso::Login

Inherits:
Object
  • Object
show all
Defined in:
lib/trivial_sso/login.rb

Class Method Summary collapse

Class Method Details

create an encrypted and signed cookie containing userdata and an expiry date. userdata should be an array, and at minimum include a ‘username’ key. using json serializer to hopefully allow future cross version compatibliity (Marshall, the default serializer, is not compatble between versions)



28
29
30
31
32
33
34
35
36
# File 'lib/trivial_sso/login.rb', line 28

def self.cookie(userdata, expire_date = default_expire_date)
  begin
    raise TrivialSso::Error::MissingConfig    if sso_secret
    raise TrivialSso::Error::NoUsernameCookie if check_username(userdata)
    enc.encrypt_and_sign([userdata, expire_date])
  rescue NoMethodError
    raise TrivialSso::Error::MissingConfig
  end
end

Decodes and verifies an encrypted cookie throw a proper exception if a bad or invalid cookie. otherwise, return the username and userdata stored in the cookie



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/trivial_sso/login.rb', line 10

def self.decode_cookie(cookie = nil)
  begin
    raise TrivialSso::Error::MissingCookie if cookie.nil? || cookie.empty?
    userdata, timestamp = encrypted_message.decrypt_and_verify(cookie)
    raise TrivialSso::Error::LoginExpired if check_timestamp(timestamp)
    userdata
  rescue NoMethodError
    raise TrivialSso::Error::MissingConfig
  rescue ActiveSupport::MessageVerifier::InvalidSignature ||
         ActiveSupport::MessageEncryptor::InvalidMessage
    raise TrivialSso::Error::BadCookie
  end
end