Sha256: 34e67e6070487c7fd09d2ee8e57713913a3c2876af06af7d7c90ca3b53f5e331

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'sinatra/base'
require 'warden-github'

module Sinatra
  module Auth
    module Github
      class BadAuthentication < Sinatra::Base
        get '/unauthenticated' do
          status 403
          "Unable to authenticate, sorry bud."
        end
      end

      module Helpers
        def warden
          env['warden']
        end

        def authenticate!(*args)
          warden.authenticate!(*args)
        end

        def authenticated?(*args)
          warden.authenticated?(*args)
        end

        def logout!
          warden.logout
        end

        def github_user
          warden.user
        end

        def github_request(path)
          response = RestClient.get("https://github.com/api/v2/json/#{path}", {:accept => :json, :params => {:token => github_user.token}})
          JSON.parse(response.body)
        end
      end

      def self.registered(app)
        app.use Warden::Manager do |manager|
          manager.default_strategies :github

          manager.failure_app           = app.github_options[:failure_app] || BadAuthentication

          manager[:github_secret]       = app.github_options[:secret]
          manager[:github_client_id]    = app.github_options[:client_id]
          manager[:github_callback_url] = app.github_options[:callback_url] || '/auth/github/callback'
        end

        app.helpers Helpers

        app.get '/auth/github/callback' do
          authenticate!
          redirect url_for '/'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra_auth_github-0.0.6 lib/sinatra/auth/github.rb