Refactor OmniAuth integration.

- Make use of Devise's OmniAuth integration.
- Merge config/initializers/omniauth.rb into config/initializers/devise.rb.
- Define button labels in config/locales/devise.en.yml.
- Fix devise's routes so we can use user_omniauth_authorize_path().
This commit is contained in:
Akinori MUSHA 2014-09-29 23:22:48 +09:00
parent 0aca3a074d
commit 87ad26301b
10 changed files with 46 additions and 57 deletions

View file

@ -5,9 +5,9 @@ module TwitterConcern
include Oauthable
validate :validate_twitter_options
valid_oauth_providers 'twitter'
valid_oauth_providers :twitter
gem_dependency_check { defined?(Twitter) && has_oauth_configuration_for?('twitter') }
gem_dependency_check { defined?(Twitter) && Devise.omniauth_providers.include?(:twitter) }
end
def validate_twitter_options

View file

@ -40,4 +40,13 @@ module ApplicationHelper
link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger'
end
end
def icon_for_service(service)
case service.to_sym
when :twitter, :github
"<i class='fa fa-#{service}'></i>".html_safe
else
"<i class='fa fa-lock'></i>".html_safe
end
end
end

View file

@ -3,7 +3,7 @@ module Agents
cannot_receive_events!
include Oauthable
valid_oauth_providers '37signals'
valid_oauth_providers :'37signals'
description <<-MD
The BasecampAgent checks a Basecamp project for new Events

View file

@ -64,10 +64,10 @@ class Service < ActiveRecord::Base
end
def self.provider_specific_options(omniauth)
case omniauth['provider']
when 'twitter', 'github'
case omniauth['provider'].to_sym
when :twitter, :github
{ name: omniauth['info']['nickname'] }
when '37signals'
when :'37signals'
{ user_id: omniauth['extra']['accounts'][0]['id'], name: omniauth['info']['name'] }
else
{ name: omniauth['info']['nickname'] }
@ -86,4 +86,4 @@ class Service < ActiveRecord::Base
options: options
end
end
end
end

View file

@ -1,10 +1,8 @@
# Huginn is designed to be a multi-User system. Users have many Agents (and Events created by those Agents).
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :lockable
:recoverable, :rememberable, :trackable, :validatable, :lockable,
:omniauthable
INVITATION_CODES = [ENV['INVITATION_CODE'] || 'try-huginn']

View file

@ -11,15 +11,9 @@
<%= link_to 'wiki', 'https://github.com/cantino/huginn/wiki/Configuring-OAuth-applications', target: :_blank %>
for guidance.
</p>
<% if has_oauth_configuration_for?('twitter') %>
<p><%= link_to "/auth/twitter", class: 'btn btn-default btn-auth btn-auth-twitter' do %><i class='fa fa-twitter'></i><span>Authenticate with Twitter</span><% end %></p>
<% end %>
<% if has_oauth_configuration_for?('37signals') %>
<p><%= link_to "/auth/37signals", class: 'btn btn-default btn-auth btn-auth-37signals' do %><i class='fa fa-lock'></i><span>Authenticate with 37Signals (Basecamp)</span><% end %></p>
<% end -%>
<% if has_oauth_configuration_for?('github') %>
<p><%= link_to "/auth/github", class: 'btn btn-default btn-auth btn-auth-github' do %><i class='fa fa-github'></i><span>Authenticate with Github</span><% end %></p>
<% end -%>
<%- Devise.omniauth_providers.each { |provider| -%>
<p><%= link_to user_omniauth_authorize_path(provider), class: "btn btn-default btn-auth btn-auth-#{provider}" do %><%= icon_for_service(provider) %><span>Authenticate with <%= t("devise.omniauth_providers.#{provider}") %></span><% end %></p>
<%- } -%>
<hr>
<div class='table-responsive'>

View file

@ -213,6 +213,23 @@ Devise.setup do |config|
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
if defined?(OmniAuth::Strategies::Twitter) &&
(key = ENV["TWITTER_OAUTH_KEY"]).present? &&
(secret = ENV["TWITTER_OAUTH_SECRET"]).present?
config.omniauth :twitter, key, secret, authorize_params: {force_login: 'true', use_authorize: 'true'}
end
if defined?(OmniAuth::Strategies::ThirtySevenSignals) &&
(key = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_KEY"]).present? &&
(secret = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_SECRET"]).present?
config.omniauth :'37signals', key, secret
end
if defined?(OmniAuth::Strategies::GitHub) &&
(key = ENV["GITHUB_OAUTH_KEY"]).present? &&
(secret = ENV["GITHUB_OAUTH_SECRET"]).present?
config.omniauth :github, key, secret
end
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
@ -236,4 +253,5 @@ Devise.setup do |config|
# When using omniauth, Devise cannot automatically set Omniauth path,
# so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = "/my_engine/users/auth"
end
config.omniauth_path_prefix = "/auth"
end

View file

@ -1,35 +0,0 @@
OMNIAUTH_PROVIDERS = {}.tap { |providers|
if defined?(OmniAuth::Strategies::Twitter) &&
(key = ENV["TWITTER_OAUTH_KEY"]).present? &&
(secret = ENV["TWITTER_OAUTH_SECRET"]).present?
providers['twitter'] = {
omniauth_params: [key, secret, authorize_params: {force_login: 'true', use_authorize: 'true'}]
}
end
if defined?(OmniAuth::Strategies::ThirtySevenSignals) &&
(key = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_KEY"]).present? &&
(secret = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_SECRET"]).present?
providers['37signals'] = {
omniauth_params: [key, secret]
}
end
if defined?(OmniAuth::Strategies::GitHub) &&
(key = ENV["GITHUB_OAUTH_KEY"]).present? &&
(secret = ENV["GITHUB_OAUTH_SECRET"]).present?
providers['github'] = {
omniauth_params: [key, secret]
}
end
}
def has_oauth_configuration_for?(provider)
OMNIAUTH_PROVIDERS.key?(provider.to_s)
end
Rails.application.config.middleware.use OmniAuth::Builder do
OMNIAUTH_PROVIDERS.each { |name, config|
provider name, *config[:omniauth_params]
}
end

View file

@ -49,6 +49,10 @@ en:
omniauth_callbacks:
success: 'Successfully authenticated from %{kind} account.'
failure: 'Could not authenticate you from %{kind} because "%{reason}".'
omniauth_providers:
twitter: 'Twitter'
github: 'GitHub'
37signals: '37Signals (Basecamp)'
mailer:
confirmation_instructions:
subject: 'Confirmation instructions'

View file

@ -66,8 +66,9 @@ Huginn::Application.routes.draw do
post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
post "/users/:user_id/update_location/:secret" => "web_requests#update_location" # legacy
match '/auth/:provider/callback', to: 'services#callback',
via: [:get, :post], constraints: { provider: Regexp.union(Devise.omniauth_providers.map(&:to_s)) }
devise_for :users, :sign_out_via => [ :post, :delete ]
get '/auth/:provider/callback', to: 'services#callback'
get "/about" => "home#about"
root :to => "home#index"