Sha256: 86bd1fbf023e933847e7cfaa753995c082517d968756d413c949cc9298da5eae

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# This class provides the rack middleware that builds on top of warden to provide the necessary endpoints for a rack 
# application to function as an AFID protected resource.
class Hadley::Middleware < Sinatra::Base

  include Hadley::Authz

  # Initializes the middleware with the provided application and options
  #
  # @param [Rack::Application] app The rack application that this middleware is participating in
  # @param [Hash] options The Hash of keyword arguments
  # @param [Hadley::TokenStore] options.store The token store to be used for persisting tokens provisioned by the AFID
  #  authorization server
  def initialize(app=nil, options={})
    super(app)
    @config ||= Hadley::Config.new(options)
    yield @config if block_given?
    @tokens = @config.token_store
    self
  end

  # The required endpoint for provisioning AFID access tokens.
  put '/access/tokens/:token' do |token|
    warden.authenticate!(:afid_server)
    begin
      @tokens.put(token, Integer(params.fetch('expires_in')), 
        identity: params.fetch('identity'), 
        client: params.fetch('client')
      )
      body 'Token Accepted'
    rescue => e
      status 400
      body e.to_s
    end
  end

  # The required endpoint for invalidating AFID access tokens.
  delete '/access/tokens/:token' do |token|
    warden.authenticate!(:afid_server)
    begin
      @tokens.delete(token)
      body 'Token Deleted'
    rescue => e
      status 400
      body e.to_s
    end
  end

  # ------------------------------------------
  # Config
  # ------------------------------------------

  disable :show_exceptions
  enable :raise_errors
  enable :logging

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hadley-0.0.3 lib/hadley/middleware.rb