Sha256: ccaedf0167ef9b6dbdb58b52a1a509b716a02a375614405bcb9bc3135137dc69

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'sinatra/base'
require 'sinatra/g_auth/version'
require 'openid/store/filesystem'
require 'omniauth/strategies/google_apps'

module Sinatra
  module GAuth 
    module Helpers
      def protect_with_gauth!
        redirect '/auth/g' unless session[:_gauth]
      end
    end

    def self.registered(app)
      app.helpers GAuth::Helpers
      app.enable :sessions
      app.set :gauth_domain, 'gmail.com'
      app.set :gauth_redirect, '/'
      app.set :gauth_tmp_dir, '/tmp'

      app.use OmniAuth::Builder do
        provider :google_apps, store: OpenID::Store::Filesystem.new(app.settings.gauth_tmp_dir), name: 'g', domain: app.settings.gauth_domain
      end

      app.post '/auth/g/callback' do
        if auth = request.env['omniauth.auth'] 
          session[:_gauth] = { id: auth['uid'], name: auth['info']['name'], email: auth['info']['email'] }
          redirect app.settings.gauth_redirect
        else
          halt 401, 'Authorization Failed.'
        end
      end
    end
  end
  register GAuth
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-g_auth-0.0.2 lib/sinatra/g_auth.rb