Sha256: 5c9a7dd70f8d833cac27330a4a5df3880efa34993989ba89f9bb8f24f60341ca

Contents?: true

Size: 946 Bytes

Versions: 2

Compression:

Stored size: 946 Bytes

Contents

# encoding: UTF-8

module Spontaneous
  module Rack
    class CookieAuthentication
      def initialize(app)
        @app = app
      end

      def access_key(env)
        if login = Site.config.auto_login
          user = Spontaneous::Permissions::User[:login => login]
          if user.access_keys.empty?
            user.generate_access_key
          else
            user.access_keys.first
          end
        else
          request = ::Rack::Request.new(env)
          api_key = request.cookies[Spontaneous::Rack::AUTH_COOKIE]
          if api_key && key = Spontaneous::Permissions::AccessKey.authenticate(api_key)
            key
          else
            nil
          end
        end
      end

      def call(env)
        response = nil
        key = env[S::Rack::ACTIVE_KEY] = access_key(env)
        user = env[S::Rack::ACTIVE_USER] = key.user if key
        response = @app.call(env)
        response
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha2 lib/spontaneous/rack/cookie_authentication.rb
spontaneous-0.2.0.alpha1 lib/spontaneous/rack/cookie_authentication.rb