lib/micropub/server/rails/middleware.rb in micropub-server-rails-0.1.2 vs lib/micropub/server/rails/middleware.rb in micropub-server-rails-0.1.3

- old
+ new

@@ -1,16 +1,57 @@ +require 'micropub' + +Micropub.configure do |c| + c.token_endpoint = "https://tokens.indieauth.com/token" + c.me = "http://bookisworthy.com" + c.allowed_scopes = [:post] +end + module Micropub::Server::Rails class Middleware def initialize(app) @app = app end def call(env) + @env = env if env["PATH_INFO"] == "/micropub" - [200, self.headers, ["I am micropub."]] + token = Micropub::Token.new(auth_token) + if token.valid? + response + else + error_response + end else @app.call env end + end + + def env + @env + end + + def auth_token + @auth_token ||= begin + input = env["HTTP_AUTHORIZATION"] || params[:access_token] || "" + input.split("Bearer ").last + end + end + + def response + [201, headers, ["I am micropub."]] + end + + def error_response + [401, {}, [""]] + end + + def request + @request ||= Rack::Request.new(env) + end + + def params + request.params end def headers { "Location" => "http://bookisworthy.com/posts/1",