Class: Maestrano::SSO::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/maestrano/sso/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Session

Returns a new instance of Session.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/maestrano/sso/session.rb', line 23

def initialize(session)
  self.session = session
  if (self.session = session)
    begin
      if mno_session = (self.session[:maestrano] || self.session['maestrano'])
        decrypted_session = JSON.parse(Base64.decode64(mno_session))
        self.uid = decrypted_session['uid']
        self.session_token = decrypted_session['session']
        self.recheck = Time.iso8601(decrypted_session['session_recheck'])
        self.group_uid = decrypted_session['group_uid']
      end
    rescue
    end
  end
end

Instance Attribute Details

#group_uidObject

Returns the value of attribute group_uid.



4
5
6
# File 'lib/maestrano/sso/session.rb', line 4

def group_uid
  @group_uid
end

#recheckObject

Returns the value of attribute recheck.



4
5
6
# File 'lib/maestrano/sso/session.rb', line 4

def recheck
  @recheck
end

#sessionObject

Returns the value of attribute session.



4
5
6
# File 'lib/maestrano/sso/session.rb', line 4

def session
  @session
end

#session_tokenObject

Returns the value of attribute session_token.



4
5
6
# File 'lib/maestrano/sso/session.rb', line 4

def session_token
  @session_token
end

#uidObject

Returns the value of attribute uid.



4
5
6
# File 'lib/maestrano/sso/session.rb', line 4

def uid
  @uid
end

Class Method Details

.from_user_auth_hash(session, auth) ⇒ Object

Load a Maestrano::SSO::Session object from a hash generated by Maestrano::SSO::BaseUser#to_hash



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/maestrano/sso/session.rb', line 8

def self.from_user_auth_hash(session, auth)
  instance = self.new({})
  instance.session = session
  
  if (extra = (auth[:extra] || auth['extra'])) && (sso_session = (extra[:session] || extra['session']))
    instance.uid = (sso_session[:uid] || sso_session['uid'])
    instance.session_token = (sso_session[:token] || sso_session['token'])
    instance.group_uid = (sso_session[:group_uid] || sso_session['group_uid'])
    if recheck = (sso_session[:recheck] || sso_session['recheck'])
      instance.recheck = recheck
    end
  end
  return instance
end

Instance Method Details

#perform_remote_checkObject

Check remote maestrano session and update the recheck attribute if the session is still valid Return true if the session is still valid and false otherwise



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/maestrano/sso/session.rb', line 50

def perform_remote_check
  # Get remote session info
  url = Maestrano::SSO.session_check_url(self.uid, self.session_token)
  begin
    response = RestClient.get(url)
    response = JSON.parse(response)
  rescue Exception => e
    response = {}
  end
  
  # Process response
  if response['valid'] && response['recheck']
    self.recheck = Time.iso8601(response['recheck'])
    return true
  end
  
  return false
end

#remote_check_required?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/maestrano/sso/session.rb', line 39

def remote_check_required?
  if self.uid && self.session_token && self.recheck
    return (self.recheck <= Time.now)
  end
  return true
end

#saveObject



96
97
98
99
100
101
102
103
# File 'lib/maestrano/sso/session.rb', line 96

def save
  self.session[:maestrano] = Base64.encode64({
    uid: self.uid,
    session: self.session_token,
    session_recheck: self.recheck.utc.iso8601,
    group_uid: self.group_uid
  }.to_json)
end

#valid?(opts = {}) ⇒ Boolean

Check whether this mno session is valid or not Return true if SLO is disabled (via sso.slo_enabled config param) Return false if no session defined


opts: if_session: if true then the session will be considered valid if the http session is nil or does not have a maestrano key. Useful when the validity of a session should be restricted to maestrano users only within an application

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/maestrano/sso/session.rb', line 80

def valid?(opts = {})
  return true unless Maestrano.param('sso.slo_enabled')
  return true if opts[:if_session] && (!self.session || (!self.session[:maestrano] && !self.session['maestrano']))
  return false unless self.session 
  
  if self.remote_check_required?
    if perform_remote_check
      self.save
      return true
    else
      return false
    end
  end
  return true
end