Sha256: d9b0d3095f160baa0c87a620960c3ccb89346ceb685cba16d968506f8576340e

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 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 "#{request.script_name}/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

2 entries across 2 versions & 1 rubygems

Version Path
sinatra-g_auth-0.0.4 lib/sinatra/g_auth.rb
sinatra-g_auth-0.0.3 lib/sinatra/g_auth.rb