Commit cf8ddd52c4125e9113bea0459ff0c1a10b9febe8

Authored by Victor Costa
1 parent f323c51d

oauth_client: oauth login with google and facebook

plugins/oauth_client/Gemfile 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +gem 'omniauth'
  2 +gem 'omniauth-facebook'
  3 +gem "omniauth-google-oauth2"
... ...
plugins/oauth_client/README 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +
  2 +
  3 +https://github.com/mkdynamic/omniauth-facebook
  4 +https://github.com/zquestz/omniauth-google-oauth2
  5 +
  6 +Create Google+ application:
  7 + https://developers.google.com/+/web/signin/javascript-flow
  8 +
  9 +Create Facebook application:
  10 + https://developers.facebook.com/docs/facebook-login/v2.1
  11 + https://developers.facebook.com/docs/reference/dialogs/oauth
... ...
plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class OauthClientPluginAdminController < AdminController
  2 +
  3 + def index
  4 + settings = params[:settings] || {}
  5 +
  6 + @settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin, settings)
  7 + @providers = @settings.get_setting(:providers) || {}
  8 + if request.post?
  9 + @settings.save!
  10 + session[:notice] = 'Settings succefully saved.'
  11 + redirect_to :action => 'index'
  12 + end
  13 + end
  14 +
  15 +end
... ...
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +class OauthClientPluginPublicController < PublicController
  2 +
  3 + def callback
  4 + auth = request.env["omniauth.auth"]
  5 + login = auth.info.email.split('@').first
  6 + user = environment.users.find_with_omniauth(auth)
  7 +
  8 + if user
  9 + session[:user] = user
  10 + redirect_to :controller => :account, :action => :login
  11 + else
  12 + name = auth.info.name
  13 + name ||= auth.extra && auth.extra.raw_info ? auth.extra.raw_info.name : ''
  14 + redirect_to :controller => :account, :action => :signup, :user => {:login => login, :email => auth.info.email, :oauth_providers => [{:provider => auth.provider, :uid => auth.uid}]}, :profile_data => {:name => name}
  15 + end
  16 + end
  17 +
  18 + def failure
  19 + redirect_to root_url
  20 + end
  21 +
  22 + def destroy
  23 + session[:user] = nil
  24 + redirect_to root_url
  25 + end
  26 +
  27 +end
... ...
plugins/oauth_client/db/migrate/20140828184930_add_settings_to_users.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddSettingsToUsers < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :users, :settings, :string
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :users, :settings
  8 + end
  9 +end
... ...
plugins/oauth_client/lib/ext/user.rb 0 → 100644
... ... @@ -0,0 +1,40 @@
  1 +require_dependency 'user'
  2 +
  3 +class User
  4 +
  5 + acts_as_having_settings :field => :settings
  6 +
  7 + settings_items :oauth_providers, :type => Array, :default => []
  8 +
  9 + def self.find_with_omniauth(auth)
  10 + user = self.find_by_email(auth.info.email)
  11 + if user && !user.oauth_providers.empty? #FIXME save new oauth providers
  12 + user
  13 + else
  14 + nil
  15 + end
  16 + end
  17 +
  18 + def password_required_with_oauth?
  19 + password_required_without_oauth? && oauth_providers.blank?
  20 + end
  21 +
  22 + alias_method_chain :password_required?, :oauth
  23 +
  24 + after_create :activate_oauth_user
  25 +
  26 + def activate_oauth_user
  27 + activate unless oauth_providers.empty?
  28 + end
  29 +
  30 + def make_activation_code_with_oauth
  31 + if oauth_providers.blank?
  32 + nil
  33 + else
  34 + make_activation_code_without_oauth
  35 + end
  36 + end
  37 +
  38 + alias_method_chain :make_activation_code, :oauth
  39 +
  40 +end
... ...
plugins/oauth_client/lib/oauth_client_plugin.rb 0 → 100644
... ... @@ -0,0 +1,70 @@
  1 +class OauthClientPlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + "Oauth Client Plugin"
  5 + end
  6 +
  7 + def self.plugin_description
  8 + _("Login with Oauth.")
  9 + end
  10 +
  11 + def login_extra_contents
  12 + plugin = self
  13 + proc do
  14 + render :partial => 'auth/oauth_login', :locals => {:providers => plugin.enabled_providers}
  15 + end
  16 + end
  17 +
  18 + def signup_extra_contents
  19 + plugin = self
  20 +
  21 + proc do
  22 + unless (plugin.context.params[:user]||{})[:oauth_providers].blank?
  23 + render :partial => 'oauth_signup'
  24 + else
  25 + ''
  26 + end
  27 + end
  28 + end
  29 +
  30 + def enabled_providers
  31 + settings = Noosfero::Plugin::Settings.new(context.environment, OauthClientPlugin)
  32 + providers = settings.get_setting(:providers)
  33 + providers.select {|provider, options| options[:enabled]}
  34 + end
  35 +
  36 + PROVIDERS = {
  37 + :facebook => {
  38 + :name => 'Facebook'
  39 + },
  40 + :google_oauth2 => {
  41 + :name => 'Google'
  42 + }
  43 + }
  44 +
  45 + def stylesheet?
  46 + true
  47 + end
  48 +
  49 + Rails.application.config.middleware.use OmniAuth::Builder do
  50 + PROVIDERS.each do |provider, options|
  51 + provider provider, :setup => lambda { |env|
  52 + request = Rack::Request.new env
  53 + strategy = env['omniauth.strategy']
  54 +
  55 + domain = Domain.find_by_name(request.host)
  56 + environment = domain.environment rescue Environment.default
  57 + settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin)
  58 + providers = settings.get_setting(:providers)
  59 +
  60 + strategy.options.client_id = providers[provider][:client_id]
  61 + strategy.options.client_secret = providers[provider][:client_secret]
  62 + }, :path_prefix => '/plugin/oauth_client', :callback_path => "/plugin/oauth_client/public/callback/#{provider}"
  63 + end
  64 +
  65 + unless Rails.env.production?
  66 + provider :developer, :path_prefix => "/plugin/oauth_client", :callback_path => "/plugin/oauth_client/public/callback/developer"
  67 + end
  68 + end
  69 +
  70 +end
... ...
plugins/oauth_client/public/images/facebook-icon.png 0 → 100644

831 Bytes

plugins/oauth_client/public/images/google-icon.png 0 → 100644

1.58 KB

plugins/oauth_client/public/style.css 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +.oauth-login .provider a {
  2 + min-width: 20px;
  3 + min-height: 20px;
  4 + background-size: 20px;
  5 + display: inline-block;
  6 + text-decoration: none;
  7 + background-repeat: no-repeat;
  8 + padding-left: 22px;
  9 + line-height: 20px;
  10 +}
  11 +
  12 +.oauth-login .provider .facebook {
  13 + background-image: url(images/facebook-icon.png);
  14 +}
  15 +
  16 +.oauth-login .provider .google_oauth2 {
  17 + background-image: url(images/google-icon.png);
  18 +}
  19 +
  20 +.oauth-login .provider .developer {
  21 + display: none;
  22 +}
... ...
plugins/oauth_client/views/account/_oauth_signup.html.erb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<%= hidden_field_tag 'user[oauth_providers][][provider]', @user.oauth_providers.first[:provider] %>
  2 +<%= hidden_field_tag 'user[oauth_providers][][uid]', @user.oauth_providers.first[:uid] %>
  3 +
  4 +<style>
  5 + #signup-password {
  6 + display: none;
  7 + }
  8 + #signup-password-confirmation {
  9 + display: none;
  10 + }
  11 +</style>
... ...
plugins/oauth_client/views/auth/_facebook.html.erb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +<a class="facebook" href="/plugin/oauth_client/facebook"><%= _('Login with Facebook') %></a>
... ...
plugins/oauth_client/views/auth/_google_oauth2.html.erb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +<a class="google_oauth2" href="/plugin/oauth_client/google_oauth2"><%= _('Login with Google') %></a>
... ...
plugins/oauth_client/views/auth/_oauth_login.html.erb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +<div class="oauth-login">
  2 + <% providers.each do |provider, options| %>
  3 + <span class="provider">
  4 + <%= render :partial => "auth/#{provider}", :locals => {:app_id => options['client_id'] } %>
  5 + </span>
  6 + <% end %>
  7 +
  8 + <span class="provider">
  9 + <% unless Rails.env.production? %>
  10 + <%= link_to _('Developer Login'), "/plugin/oauth/developer", :class => 'developer' %>
  11 + <% end %>
  12 + </span>
  13 +</div>
... ...
plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +<h1><%= _('Oauth Client Settings') %></h1>
  2 +
  3 +<%= form_for(:settings) do |f| %>
  4 + <div class="providers">
  5 + <h3><%= _('Providers') %></h3>
  6 + <%= f.fields_for :providers, OpenStruct.new(@providers) do |p| %>
  7 +
  8 + <% OauthClientPlugin::PROVIDERS.each do |available_provider, options| %>
  9 + <% provider = OpenStruct.new(@providers[available_provider]) %>
  10 +
  11 + <%= p.fields_for available_provider, provider do |o| %>
  12 + <div class="provider">
  13 + <div class="name">
  14 + <h4><%= o.check_box :enabled, {:class => 'enable', :checked => provider.enabled=='true'}, true, false %>
  15 + <%= options[:name] %></h4>
  16 + </div>
  17 + <div class="options" style="<%= provider.enabled=='true' ? '':'display:none' %>">
  18 + <div class="client-id">
  19 + <span class="label"><%= _('Client ID') %></span>
  20 + <span class="value"><%= o.text_field :client_id %></span>
  21 + </div>
  22 + <div class="client-secret">
  23 + <span class="label"><%= _('Client Secret') %></span>
  24 + <span class="value"><%= o.text_field :client_secret %></span>
  25 + </div>
  26 + </div>
  27 + </div>
  28 + <% end %>
  29 + <% end %>
  30 + <% end %>
  31 +
  32 + <% button_bar do %>
  33 + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
  34 + <% end %>
  35 + </div>
  36 +<% end %>
  37 +
  38 +<script>
  39 + jQuery(document).ready(function($) {
  40 + $('.providers .provider .enable').on('click', function() {
  41 + $(this).parents('.provider').find('.options').toggle('fast');
  42 + });
  43 + });
  44 +</script>
... ...