class ICFS::Demo::Auth

Test authentication - Rack middleware

@deprecated This is a horrible security implementation, DO NOT USE

for anything other than testing.

Public Class Methods

new(app, api) click to toggle source

New instance

@param app [Object] The rack app @param api [Object] the ICFS API

# File lib/icfs/demo/auth.rb, line 33
def initialize(app, api)
  @app = app
  @api = api
end

Public Instance Methods

call(env) click to toggle source

Handle the calls

# File lib/icfs/demo/auth.rb, line 42
def call(env)

  # login
  if env['PATH_INFO'] == '/login'
    user = env['QUERY_STRING']
    body = 'User set'

    # set the cookie
    rsp = Rack::Response.new( body, 200, {} )
    rsp.set_cookie( 'icfs-user', {
        value: user,
        expires: Time.now + 24*60*60
      })
    return rsp.finish
  end

  # pull the username from the cookie
  cookies = Rack::Request.new(env).cookies
  user = cookies['icfs-user']
  if !user
    return [400, {'Content-Type' => 'text/plain'}, ['Login first']]
  end

  # set up for the call
  @api.user = user
  env['icfs'] = @api
  return @app.call(env)

rescue ICFS::Error::NotFound, ICFS::Error::Value => err
  return [400, {'Content-Type' => 'text/plain'}, [err.message]]
end