Enable Wordpress Oauth login and registration

This commit is contained in:
Julian Herrero
2020-01-18 18:51:06 +07:00
parent 9bbed55d20
commit 193e51d42f
12 changed files with 181 additions and 0 deletions

View File

@@ -1539,6 +1539,7 @@ table {
.button.button-twitter, .button.button-twitter,
.button.button-facebook, .button.button-facebook,
.button.button-google, .button.button-google,
.button.button-wordpress,
.button.button-telegram { .button.button-telegram {
background: #fff; background: #fff;
color: $text; color: $text;
@@ -1678,6 +1679,23 @@ table {
} }
} }
.button.button-wordpress {
background: #dcdde3;
border-left: 3px solid #2f2f33;
&::before {
color: #2f2f33;
content: "J";
font-family: "icons" !important;
font-size: rem-calc(24);
left: 0;
line-height: $line-height * 2;
padding: 0 rem-calc(20);
position: absolute;
top: 0;
}
}
.button.button-telegram { .button.button-telegram {
background: #ecf7fc; background: #ecf7fc;
border-left: 3px solid #08c; border-left: 3px solid #08c;

View File

@@ -11,6 +11,10 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
sign_in_with :google_login, :google_oauth2 sign_in_with :google_login, :google_oauth2
end end
def wordpress_oauth2
sign_in_with :wordpress_login, :wordpress_oauth2
end
def after_sign_in_path_for(resource) def after_sign_in_path_for(resource)
if resource.registering_with_oauth if resource.registering_with_oauth
finish_signup_path finish_signup_path

View File

@@ -84,6 +84,7 @@ class Setting < ApplicationRecord
"feature.facebook_login": true, "feature.facebook_login": true,
"feature.google_login": true, "feature.google_login": true,
"feature.twitter_login": true, "feature.twitter_login": true,
"feature.wordpress_login": false,
"feature.public_stats": true, "feature.public_stats": true,
"feature.signature_sheets": true, "feature.signature_sheets": true,
"feature.user.recommendations": true, "feature.user.recommendations": true,

View File

@@ -35,6 +35,15 @@
</div> </div>
<% end %> <% end %>
<% if feature? :wordpress_login %>
<div class="small-12 medium-6 large-4 column end">
<%= link_to t("omniauth.wordpress_oauth2.name"), user_wordpress_oauth2_omniauth_authorize_path,
title: t("omniauth.wordpress_oauth2.sign_in"),
class: "button-wordpress button expanded",
method: :post %>
</div>
<% end %>
<div class="small-12 column auth-divider"> <div class="small-12 column auth-divider">
<span><%= t("omniauth.or_fill") %></span> <span><%= t("omniauth.or_fill") %></span>
</div> </div>
@@ -72,6 +81,16 @@
title: t("omniauth.google_oauth2.sign_up"), title: t("omniauth.google_oauth2.sign_up"),
class: "button-google button expanded", class: "button-google button expanded",
method: :post %> method: :post %>
</div>
<% end %>
<% if feature? :wordpress_login %>
<div class="small-12 medium-6 large-4 column end">
<%= link_to t("omniauth.wordpress_oauth2.name"), user_wordpress_oauth2_omniauth_authorize_path,
title: t("omniauth.wordpress_oauth2.sign_up"),
class: "button-wordpress button expanded",
method: :post %>
</div> </div>
<% end %> <% end %>

View File

@@ -1,3 +1,5 @@
require Rails.root.join("lib", "omniauth_wordpress")
# Use this hook to configure devise mailer, warden hooks and so forth. # Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model. # Many of these configuration options can be set straight in your model.
Devise.setup do |config| Devise.setup do |config|
@@ -242,6 +244,11 @@ Devise.setup do |config|
config.omniauth :twitter, Rails.application.secrets.twitter_key, Rails.application.secrets.twitter_secret config.omniauth :twitter, Rails.application.secrets.twitter_key, Rails.application.secrets.twitter_secret
config.omniauth :facebook, Rails.application.secrets.facebook_key, Rails.application.secrets.facebook_secret, scope: "email", info_fields: "email,name,verified" config.omniauth :facebook, Rails.application.secrets.facebook_key, Rails.application.secrets.facebook_secret, scope: "email", info_fields: "email,name,verified"
config.omniauth :google_oauth2, Rails.application.secrets.google_oauth2_key, Rails.application.secrets.google_oauth2_secret config.omniauth :google_oauth2, Rails.application.secrets.google_oauth2_key, Rails.application.secrets.google_oauth2_secret
config.omniauth :wordpress_oauth2,
Rails.application.secrets.wordpress_oauth2_key,
Rails.application.secrets.wordpress_oauth2_secret,
strategy_class: OmniAuth::Strategies::Wordpress,
client_options: { site: Rails.application.secrets.wordpress_oauth2_site }
# ==> Warden configuration # ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or # If you want to use other strategies, that are not supported by Devise, or

View File

@@ -294,6 +294,10 @@ en:
sign_in: Sign in with Google sign_in: Sign in with Google
sign_up: Sign up with Google sign_up: Sign up with Google
name: Google name: Google
wordpress_oauth2:
sign_in: Sign in with Wordpress
sign_up: Sign up with Wordpress
name: Wordpress
twitter: twitter:
sign_in: Sign in with Twitter sign_in: Sign in with Twitter
sign_up: Sign up with Twitter sign_up: Sign up with Twitter

View File

@@ -89,6 +89,8 @@ en:
facebook_login_description: "Allow users to sign up with their Facebook account" facebook_login_description: "Allow users to sign up with their Facebook account"
google_login: "Google login" google_login: "Google login"
google_login_description: "Allow users to sign up with their Google Account" google_login_description: "Allow users to sign up with their Google Account"
wordpress_login: "Wordpress login"
wordpress_login_description: "Allow users to sign up with their Wordpress Account"
featured_proposals: "Featured proposals" featured_proposals: "Featured proposals"
featured_proposals_description: "Shows featured proposals on index proposals page" featured_proposals_description: "Shows featured proposals on index proposals page"
signature_sheets: "Signature sheets" signature_sheets: "Signature sheets"

View File

@@ -294,6 +294,10 @@ es:
sign_in: Entra con Google sign_in: Entra con Google
sign_up: Regístrate con Google sign_up: Regístrate con Google
name: Google name: Google
wordpress_oauth2:
sign_in: Entra con Wordpress
sign_up: Regístrate con Wordpress
name: Wordpress
twitter: twitter:
sign_in: Entra con Twitter sign_in: Entra con Twitter
sign_up: Regístrate con Twitter sign_up: Regístrate con Twitter

View File

@@ -89,6 +89,8 @@ es:
facebook_login_description: "Permitir que los usuarios se registren con su cuenta de Facebook" facebook_login_description: "Permitir que los usuarios se registren con su cuenta de Facebook"
google_login: "Registro con Google" google_login: "Registro con Google"
google_login_description: "Permitir que los usuarios se registren con su cuenta de Google" google_login_description: "Permitir que los usuarios se registren con su cuenta de Google"
wordpress_login: "Registro con Wordpress"
wordpress_login_description: "Permitir que los usuarios se registren con su cuenta de Wordpress"
featured_proposals: "Propuestas destacadas" featured_proposals: "Propuestas destacadas"
featured_proposals_description: "Muestra propuestas destacadas en la página principal de propuestas" featured_proposals_description: "Muestra propuestas destacadas en la página principal de propuestas"
signature_sheets: "Hojas de firmas" signature_sheets: "Hojas de firmas"

View File

@@ -105,5 +105,8 @@ production:
facebook_secret: "" facebook_secret: ""
google_oauth2_key: "" google_oauth2_key: ""
google_oauth2_secret: "" google_oauth2_secret: ""
wordpress_oauth2_key: ""
wordpress_oauth2_secret: ""
wordpress_oauth2_site: ""
<<: *maps <<: *maps
<<: *apis <<: *apis

40
lib/omniauth_wordpress.rb Normal file
View File

@@ -0,0 +1,40 @@
# This code is based on this gem https://github.com/jwickard/omniauth-wordpress-oauth2-plugin
require "omniauth-oauth2"
module OmniAuth
module Strategies
class Wordpress < OmniAuth::Strategies::OAuth2
option :name, "wordpress_oauth2"
option :client_options, {}
uid { raw_info["ID"] }
info do
{
name: raw_info["display_name"],
email: raw_info["user_email"],
nickname: raw_info["user_nicename"],
urls: { "Website" => raw_info["user_url"] }
}
end
extra do
{ raw_info: raw_info }
end
def callback_url
full_host + script_name + callback_path
end
def raw_info
@raw_info ||= obtain_raw_info
end
def obtain_raw_info
access_token.get("/oauth/me", params: { "Authorization" => "Bearer #{access_token.token}" }).parsed
end
end
end
end

View File

@@ -312,6 +312,83 @@ describe "Users" do
expect(page).to have_field("user_email", with: "somethingelse@example.com") expect(page).to have_field("user_email", with: "somethingelse@example.com")
end end
end end
context "Wordpress" do
let(:wordpress_hash) do
{ provider: "wordpress",
uid: "12345",
info: {
name: "manuela",
email: "manuelacarmena@example.com" }}
end
before { Setting["feature.wordpress_login"] = true }
scenario "Sign up" do
OmniAuth.config.add_mock(:wordpress_oauth2, wordpress_hash)
visit "/"
click_link "Register"
click_link "Sign up with Wordpress"
expect(page).to have_current_path(new_user_session_path)
expect(page).to have_content "To continue, please click on the confirmation link that we have sent you via email"
confirm_email
expect(page).to have_content "Your account has been confirmed"
visit "/"
click_link "Sign in"
click_link "Sign in with Wordpress"
expect_to_be_signed_in
click_link "My account"
expect(page).to have_field("account_username", with: "manuela")
visit edit_user_registration_path
expect(page).to have_field("user_email", with: "manuelacarmena@example.com")
end
scenario "Try to register with username and email of an already existing user" do
create(:user, username: "manuela", email: "manuelacarmena@example.com", password: "judgementday")
OmniAuth.config.add_mock(:wordpress_oauth2, wordpress_hash)
visit "/"
click_link "Register"
click_link "Sign up with Wordpress"
expect(page).to have_current_path(finish_signup_path)
expect(page).to have_field("user_username", with: "manuela")
click_button "Register"
expect(page).to have_current_path(do_finish_signup_path)
fill_in "Username", with: "manuela2"
fill_in "Email", with: "manuela@consul.dev"
click_button "Register"
expect(page).to have_current_path(new_user_session_path)
expect(page).to have_content "To continue, please click on the confirmation link that we have sent you via email"
confirm_email
expect(page).to have_content "Your account has been confirmed"
visit "/"
click_link "Sign in"
click_link "Sign in with Wordpress"
expect_to_be_signed_in
click_link "My account"
expect(page).to have_field("account_username", with: "manuela2")
visit edit_user_registration_path
expect(page).to have_field("user_email", with: "manuela@consul.dev")
end
end
end end
scenario "Sign out" do scenario "Sign out" do