Skip to content

Commit

Permalink
Integrate OmniAuth with a Singly provider
Browse files Browse the repository at this point in the history
Using the new gem at Singly/omniauth-singly make starting up a breeze.
  • Loading branch information
kristjan committed May 16, 2012
1 parent b18a8a2 commit 8eec67a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 49 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -13,6 +13,7 @@ gem 'json'

gem 'haml', '3.1.4'
gem 'httparty'
gem 'omniauth-singly'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
19 changes: 19 additions & 0 deletions Gemfile.lock
Expand Up @@ -42,11 +42,15 @@ GEM
eventmachine (0.12.10)
execjs (1.3.2)
multi_json (~> 1.0)
faraday (0.8.0)
multipart-post (~> 1.1)
haml (3.1.4)
hashie (1.2.0)
hike (1.2.1)
httparty (0.8.1)
multi_json
multi_xml
httpauth (0.1)
i18n (0.6.0)
journey (1.0.3)
jquery-rails (2.0.2)
Expand All @@ -60,6 +64,20 @@ GEM
mime-types (1.18)
multi_json (1.3.4)
multi_xml (0.4.2)
multipart-post (1.1.5)
oauth2 (0.6.1)
faraday (~> 0.7)
httpauth (~> 0.1)
multi_json (~> 1.3)
omniauth (1.1.0)
hashie (~> 1.2)
rack
omniauth-oauth2 (1.0.2)
oauth2 (~> 0.6.0)
omniauth (~> 1.0)
omniauth-singly (0.0.1)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.0)
pg (0.13.2)
polyglot (0.3.3)
rack (1.4.1)
Expand Down Expand Up @@ -120,6 +138,7 @@ DEPENDENCIES
httparty
jquery-rails
json
omniauth-singly
pg
rails (= 3.2.3)
sass-rails (~> 3.2.3)
Expand Down
46 changes: 3 additions & 43 deletions app/controllers/auth_controller.rb
@@ -1,57 +1,17 @@
require 'httparty'
require "httparty"

class AuthController < ApplicationController

def service
redirect_to auth_url(params[:service])
end

SINGLY_API_BASE = "https://api.singly.com"

def callback
data = HTTParty.post(
token_url,
{:body => token_params(params[:code])}
).parsed_response
session[:access_token] = data['access_token']
auth = request.env["omniauth.auth"]
session[:access_token] = auth.credentials.token
redirect_to "/"
end

def logout
session.clear
redirect_to "/"
end

private

def token_params(code)
{
:client_id => ENV["SINGLY_ID"],
:client_secret => ENV["SINGLY_SECRET"],
:code => code
}
end

def token_url
"#{SINGLY_API_BASE}/oauth/access_token"
end

def auth_params(service)
{
:client_id => ENV["SINGLY_ID"],
:redirect_uri => callback_url,
:service => service
}.map {|key, value|
"#{key}=#{value}"
}.join("&")
end

def auth_url(servie)
"#{SINGLY_API_BASE}/oauth/authorize?#{auth_params(params[:service])}"
end

def callback_url
"#{request.protocol}#{request.host}:#{request.port}/auth_callback"
end

end
2 changes: 1 addition & 1 deletion app/controllers/default_controller.rb
@@ -1,4 +1,4 @@
require 'httparty'
require "httparty"

class DefaultController < ApplicationController
def home
Expand Down
2 changes: 1 addition & 1 deletion app/views/default/home.html.haml
Expand Up @@ -19,4 +19,4 @@
= @profiles[service]
- else
is not connected.
%a(href="/auth/#{service}") Connect
%a(href="/auth/singly?service=#{service}") Connect
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :singly, ENV['SINGLY_ID'], ENV['SINGLY_SECRET']
end
8 changes: 4 additions & 4 deletions config/routes.rb
@@ -1,6 +1,6 @@
SinglyRailsSkeleton::Application.routes.draw do
match 'auth/:service' => 'auth#service'
match 'auth_callback' => 'auth#callback'
match 'logout' => 'auth#logout'
root :to => 'default#home'
match "auth/:service" => "auth#service"
match "auth/:service/callback" => "auth#callback"
match "logout" => "auth#logout"
root :to => "default#home"
end

0 comments on commit 8eec67a

Please sign in to comment.