Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Slotos committed Jun 6, 2012
0 parents commit d637dad
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in rubycas-facebook-matcher.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Dmitriy Soltys

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# CASServer::Matchers::Facebook

Provides mechanism to authenticate user with Facebook and then match them against your central SQL user database

## Installation

Ensure this gem is reachable by rubycase server, which depends on how you run it.

If you run rubycase-server as sinatra, be it alone or mounted to another app - add this line to Gemfile:

gem 'rubycas-facebook-matcher', :git => git://github.com/Slotos/rubycas-facebook-matcher.git

And then execute:

bundle

If you run is as centralized system service - install gem by running:

gem install rubycas-facebook-matcher

Of course I lied, there's no way to install it that way unless I release it as a gem =P

## Usage

For now you'll have to use `generic_matchers` branch from my fork of rubycas-server if you want to use this matcher. All you need to do is add this definition to your config.yml:

````yaml
matcher:
facebook:
database:
adapter: mysql2
database: candi_development
username: root
user_table: users
username_column: email
token_table: access_tokens
foreign_key_column: user_id
provider_column: provider
consumerkey: '' # facebook App ID
consumersecret: '' # facebook App secret````

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
64 changes: 64 additions & 0 deletions lib/rubycas-facebook-matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require "rubycas-facebook-matcher/version"
require "sequel"
require "omniauth-facebook"

module CASServer
module Matchers
module Facebook
class Worker
def initialize(config)
raise "Expecting config" unless config.is_a?(::Hash)
@token_table = config['token_table']

@connection = Sequel.connect(config['database'])
@dataset = @connection.from(config['token_table']).join(config['user_table'].to_sym, :id => config['foreign_key_column'].to_sym).where(config['provider_column'].to_sym => 'facebook')
end

def match(uid)
matcher = @dataset.where("#{@token_table}__uid".to_sym => uid)
raise "Multiple matches, database tainted" if matcher.count > 1
matcher.first
end

def link(path)
%[<a href="#{path}"><img alt="Log in with Facebook" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAMAAAAM7l6QAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAV9QTFRFVHCncIi2cYm2bYW0XXisPFyZ4ebvMlOUOFiXKkyQLlCSna3MMVKULE+Rnq7NyNHjQF+cusXbOVmYPV2aNleW8PL3OFmXM1SVVnKotMDYeI65b4e1rbvUL1GTlafJjqHEwszfNVWWJkmO6+/0U2+mnKzLbISy5urxZ4CxzNTlzdXmPFyal6jJT2ykVXGnX3qtuMTaPl2aOluZIESLx9Diu8bbTGmjt8PaKEuPSGagKUuQwcvfvsndKUyPOlqYXnisW3aqPVyZNleXNlaW6u30/f3+RWOeYHutXHar2d/s3eLt193qMVOUY32vUm6ldIu3coq3tsHZ3OLsc4m1dYy5YHqtG0CIOFiYw83gVnGoMFKTucTbnKzMwszgboa1c4u4oK/Oz9fmxs/ir7zWdo25ZoCxTGmidIy4X3itTWqihJi/HEGIp7bRb4a0N1iXa4OzeY+6N1eXQWCc////O1uZx/K/HgAAABB0Uk5T////////////////////AOAjXRkAAAEvSURBVHjarNNXb8IwFIZhQ5uELGhTICmjQIHuvTfde++9907C/1e/gFSrwslV36sjPbJ0LMskZHkUIkXPiOWlFov7AgLSXdic2ePRgM7i9vVFfsFGzQKL51527VI8k0fey2pHFBaP5UFrTbHYR5TBgakd8HV/MqlwLO4dBMsS896c2qjWgxveRFGtPG2m8sFV8E2HLKfMClYi9m+MzQWeMuPeSpZytvJ0JjF+PO+cnEinExn3zQ+Zm+PedbXgoOTy3v/N0bBJ07Ue8PkkxrAGruE2D6pp09234LscxqWhI4cLVz7a5XYb+OwB49OJ6fCyr4r2FW8FD29g9JMSP+/7afGtFfBFC8Z7A2xxjwah5bpeZVn+7sRojGoWPtGfzc1ZSRTF08/y5kXi/QV/BBgA/go2OS2QHDcAAAAASUVORK5CYII=" /></a> ]
end
end

def self.registered(app)
# Faraday won't work with facebook ssl certificate on some machines when using net/http. Using another adapter.
Faraday.default_adapter = :typhoeus

settings = app.config[:matcher][:facebook]

app.set :facebook_matcher, Worker.new(settings)

# Register omniauth interface
key = settings["consumerkey"]
secret = settings["consumersecret"]
app.use ::OmniAuth::Builder do
provider :facebook, key, secret
end

app.get "#{app.uri_path}/auth/facebook/callback" do
auth = request.env['omniauth.auth']

if match = app.settings.facebook_matcher.match(auth[:uid])
@username = match[:email]
@service = session[:service]

confirm_authentication!
end

# Redirect to login page if we're still here. Preserve service and renew data
redirect_params = []
redirect_params << "service=#{CGI.escape session[:service]}" if session[:service]
redirect_params << "renew=#{session[:renew]}" if session[:renew]
redirect to("#{app.uri_path}/login?#{redirect_params.join('&')}"), 303
end

app.add_oauth_link app.settings.facebook_matcher.link("/auth/facebook")
end
end
end
end
7 changes: 7 additions & 0 deletions lib/rubycas-facebook-matcher/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module CASServer
module Matchers
module Facebook
VERSION = "0.0.1"
end
end
end
22 changes: 22 additions & 0 deletions rubycas-facebook-matcher.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/rubycas-facebook-matcher/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Dmitriy Soltys"]
gem.email = ["slotos@gmail.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "rubycas-facebook-matcher"
gem.require_paths = ["lib"]
gem.version = CASServer::Matchers::Facebook::VERSION

gem.add_dependency "sequel"
gem.add_dependency "rubycas-server"
gem.add_dependency "omniauth-facebook"
gem.add_dependency "typhoeus"
end

0 comments on commit d637dad

Please sign in to comment.