Class: OMF::SFA::AM::Rest::SessionAuthenticator

Inherits:
Base::LObject
  • Object
show all
Defined in:
lib/omf-sfa/am/am-rest/session_authenticator.rb

Constant Summary collapse

@@store =
{}
@@active =
false
@@expire_after =

Expire authenticated session after being idle for that many seconds

2592000
@@def_authenticator =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ SessionAuthenticator

opts -

:no_session - Array of regexp to ignore


44
45
46
47
48
49
50
51
52
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 44

def initialize(app, opts = {})
  @app = app
  @opts = opts
  @opts[:no_session] = (@opts[:no_session] || []).map { |s| Regexp.new(s) }
  if @opts[:expire_after]
    @@expire_after = @opts[:expire_after]
  end
  @@active = true
end

Class Method Details

.[](key) ⇒ Object



28
29
30
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 28

def self.[](key)
  (@@store[key] || {})[:value]
end

.[]=(key, value) ⇒ Object



32
33
34
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 32

def self.[]=(key, value)
  @@store[key] = {:value => value, :time => Time.now } # add time for GC
end

.active?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 9

def self.active?
  @@active
end

.authenticateObject



17
18
19
20
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 17

def self.authenticate
  self[:authenticated] = true
  self[:valid_until] = Time.now + @@expire_after
end

.authenticated?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 13

def self.authenticated?
  self[:authenticated]
end

.logoutObject



22
23
24
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 22

def self.logout
  self[:authenticated] = false
end

Instance Method Details

#call(env) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 55

def call(env)
  #puts env.keys.inspect
  req = ::Rack::Request.new(env)
  sid = nil
  path_info = req.path_info
  #puts "REQUEST(#{self.object_id}): #{path_info}"

  unless @opts[:no_session].find {|rx| rx.match(path_info) }
    unless sid = req.cookies['sid']
      sid = "s#{(rand * 10000000).to_i}_#{(rand * 10000000).to_i}"
      debug "Setting session for '#{req.path_info}' to '#{sid}'"
    end
    Thread.current["sessionID"] = sid
    # If 'login_url' is defined, check if this session is authenticated
     = @opts[:login_url]
    if 
      unless  == req.path_info
        puts ">>>>>> CHECKING FOR LOGIN #{.class}"
        if authenticated = self.class[:authenticated]
          # Check if it hasn't imed out
          if self.class[:valid_until] < Time.now
            debug "Session '#{sid}' expired"
            authenticated = false
          end
        end
        unless authenticated
          return [301, {'Location' => , "Content-Type" => ""}, ['Login first']]
        end
      end
    else
      init_fake_root
    end
    self.class[:valid_until] = Time.now + @@expire_after
  end

  status, headers, body = @app.call(env)
  if sid
    headers['Set-Cookie'] = "sid=#{sid}"  ##: name2=value2; Expires=Wed, 09-Jun-2021 ]
  end
  [status, headers, body]
end

#init_fake_rootObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/omf-sfa/am/am-rest/session_authenticator.rb', line 99

def init_fake_root
  unless @@def_authenticator
    auth = {}
    [
      # ACCOUNT
      :can_create_account?, # ()
      :can_view_account?, # (account)
      :can_renew_account?, # (account, until)
      :can_close_account?, # (account)
      # RESOURCE
      :can_create_resource?, # (resource_descr, type)
      :can_view_resource?, # (resource)
      :can_release_resource?, # (resource)
      # LEASE
      :can_create_lease?, # (lease)
      :can_view_lease?, # (lease)
      :can_modify_lease?, # (lease)
      :can_release_lease?, # (lease)
    ].each do |m| auth[m] = true end
    require 'omf-sfa/am/default_authorizer'
    @@def_authenticator = OMF::SFA::AM::DefaultAuthorizer.new(auth)
  end
  Thread.current["authenticator"] = @@def_authenticator
end