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
# File 'lib/maestrano/sso/session.rb', line 23

def initialize(session)
  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

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



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

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)


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

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

#saveObject



79
80
81
82
83
84
85
86
# File 'lib/maestrano/sso/session.rb', line 79

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?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/maestrano/sso/session.rb', line 67

def valid?
  if self.remote_check_required?
    if perform_remote_check
      self.save
      return true
    else
      return false
    end
  end
  return true
end