Class: ICFS::Demo::Auth Deprecated

Inherits:
Object
  • Object
show all
Defined in:
lib/icfs/demo/auth.rb

Overview

Deprecated.

This is a horrible security implementation, DO NOT USE for anything other than testing.

Test authentication - Rack middleware

Instance Method Summary collapse

Constructor Details

#initialize(app, api) ⇒ Auth

New instance

Parameters:

  • app (Object)

    The rack app

  • api (Object)

    the ICFS API



33
34
35
36
# File 'lib/icfs/demo/auth.rb', line 33

def initialize(app, api)
  @app = app
  @api = api
end

Instance Method Details

#call(env) ⇒ Object

Handle the calls



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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