Add preferred provider social signup

- Add preferred provider for authorization to login and signup pages.
  To use, the 3rd party application would have to add `preferred_provider=...`
  parameter to OAuth2 authorization request.
- Resize 3rd party provider icons
- Add "login to authorize" heading to login and signup screens
This commit is contained in:
Milan Cvetkovic 2023-12-06 10:31:52 +00:00
parent d0e8f72311
commit 9649b192c0
11 changed files with 135 additions and 59 deletions

View file

@ -12,7 +12,7 @@ $(document).ready(function () {
$("#openid_open_url").click(function (e) {
e.preventDefault();
$("#openid_url").val("http://");
$("#login_auth_buttons").hide();
$("#login_auth_buttons").hide().removeClass("d-flex");
$("#login_openid_url").show();
$("#openid_login_button").show();
});

View file

@ -3,6 +3,18 @@ module SessionMethods
private
##
# Read @preferred_auth_provider and @client_app_name from oauth2 authorization request's referer
def parse_oauth_referer(referer)
referer_query = URI(referer).query if referer
return unless referer_query
ref_params = CGI.parse referer_query
preferred = ref_params["preferred_auth_provider"].first
@preferred_auth_provider = preferred if preferred && Settings.key?(:"#{preferred}_auth_id")
@client_app_name = Oauth2Application.where(:uid => ref_params["client_id"].first).pick(:name)
end
##
# return the URL to use for authentication
def auth_url(provider, uid, referer = nil)

View file

@ -15,6 +15,8 @@ class SessionsController < ApplicationController
override_content_security_policy_directives(:form_action => []) if Settings.csp_enforce || Settings.key?(:csp_report_url)
session[:referer] = safe_referer(params[:referer]) if params[:referer]
parse_oauth_referer session[:referer]
end
def create

View file

@ -60,6 +60,8 @@ class UsersController < ApplicationController
session[:referer]
end
parse_oauth_referer @referer
append_content_security_policy_directives(
:form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
)

View file

@ -60,11 +60,25 @@ module UserHelper
link_to(
image_tag("#{name}.svg",
:alt => t("application.auth_providers.#{name}.alt"),
:class => "rounded-3",
:size => "36"),
:class => "rounded-1",
:size => "24"),
auth_path(options.merge(:provider => provider)),
:method => :post,
:class => "auth_button",
:class => "auth_button p-2 d-block",
:title => t("application.auth_providers.#{name}.title")
)
end
def auth_button_preferred(name, provider, options = {})
link_to(
image_tag("#{name}.svg",
:alt => t("application.auth_providers.#{name}.alt"),
:class => "rounded-1 me-3",
:width => "24px",
:height => "24px") + t("application.auth_providers.#{name}.title"),
auth_path(options.merge(:provider => provider)),
:method => :post,
:class => "auth_button fs-6 border rounded text-muted text-decoration-none py-2 px-4 d-flex justify-content-center align-items-center",
:title => t("application.auth_providers.#{name}.title")
)
end

View file

@ -1,33 +1,44 @@
<div>
<div class="mb-3">
<label class="form-label"><%= t ".with external" %></label>
<div class="list-inline justify-content-center d-flex align-items-center flex-wrap mb-3 gap-3" id="login_auth_buttons">
<ul class='list-inline' id="login_auth_buttons">
<li class="list-inline-item me-3">
<% %w[google facebook microsoft github wikipedia].each do |provider| %>
<% if Settings.key?("#{provider}_auth_id".to_sym) -%>
<% if @preferred_auth_provider == provider %>
<div class="mx-2"><%= auth_button_preferred provider, provider %></div>
<% end %>
<% end -%>
<% end -%>
<div class="justify-content-center d-flex gap-1">
<div>
<%= link_to image_tag("openid.png",
:alt => t("application.auth_providers.openid.title"),
:size => "36"),
:size => "24"),
"#",
:id => "openid_open_url",
:title => t("application.auth_providers.openid.title") %>
</li>
<% %w[google facebook microsoft github wikipedia].each do |provider| %>
<% if Settings.key?("#{provider}_auth_id".to_sym) -%>
<li class="list-inline-item me-3"><%= auth_button provider, provider %></li>
<% end -%>
<% end -%>
</ul>
<%= form_tag(auth_path(:provider => "openid"), :id => "openid_login_form") do %>
<div id='login_openid_url' class="mb-3">
<label for='openid_url' class="form-label"><%= t ".openid_html", :logo => openid_logo %></label>
<%= hidden_field_tag("referer", params[:referer], :autocomplete => "off") %>
<%= text_field_tag("openid_url", "", :tabindex => 5, :autocomplete => "on", :class => "openid_url form-control") %>
<span class="form-text text-muted">(<a href="<%= t "accounts.edit.openid.link" %>" target="_new"><%= t "accounts.edit.openid.link text" %></a>)</span>
:title => t("application.auth_providers.openid.title"),
:class => "p-2 d-block" %>
</div>
<%= submit_tag t(".openid_login_button"), :tabindex => 6, :id => "openid_login_button", :class => "btn btn-primary" %>
<% end %>
<% %w[google facebook microsoft github wikipedia].each do |provider| %>
<% unless @preferred_auth_provider == provider %>
<% if Settings.key?("#{provider}_auth_id".to_sym) -%>
<div><%= auth_button provider, provider %></div>
<% end -%>
<% end %>
<% end -%>
</div>
</div>
<%# :tabindex starts high to allow rendering at the bottom of the template %>
<%= form_tag(auth_path(:provider => "openid"), :id => "openid_login_form") do %>
<div id="login_openid_url" class="mb-3">
<label for="openid_url" class="form-label"><%= t ".openid_html", :logo => openid_logo %></label>
<%= hidden_field_tag("referer", params[:referer], :autocomplete => "off") %>
<%= text_field_tag("openid_url", "", :tabindex => 20, :autocomplete => "on", :class => "openid_url form-control") %>
<span class="form-text text-muted">(<a href="<%= t "accounts.edit.openid.link" %>" target="_new"><%= t "accounts.edit.openid.link text" %></a>)</span>
</div>
<%= submit_tag t(".openid_login_button"), :tabindex => 21, :id => "openid_login_button", :class => "btn btn-primary" %>
<% end %>
</div>

View file

@ -6,6 +6,10 @@
<% content_for :heading_class, "p-0 mw-100" %>
<% content_for :heading do %>
<% if @client_app_name %>
<p class="text-center text-muted fs-6 py-2 mb-0 bg-white"><%= t(".login_to_authorize_html", :client_app_name => @client_app_name) %></p>
<% end %>
<div class="header-illustration new-user-main auth-container mx-auto">
<ul class="nav nav-tabs position-absolute bottom-0 px-3 fs-6 w-100">
<li class="nav-item">
@ -19,7 +23,14 @@
<% end %>
<div id="login_login" class="auth-container mx-auto my-0">
<p class='text-muted'><%= t ".no account" %> <%= link_to t(".register now"), user_new_path(:referer => params[:referer]) %></p>
<% if @preferred_auth_provider %>
<%= render :partial => "auth_providers" %>
<div class="d-flex justify-content-center align-items-center">
<div class="border-bottom border-1 flex-grow-1"></div>
<div class="text-secondary mx-3"><%= t ".or" %></div>
<div class="border-bottom border-1 flex-grow-1"></div>
</div>
<% end %>
<%= bootstrap_form_tag(:action => "login", :html => { :id => "login_form" }) do |f| %>
<%= hidden_field_tag("referer", h(params[:referer]), :autocomplete => "off") %>
@ -40,10 +51,17 @@
<%= f.check_box :remember_me, { :label => t(".remember"), :tabindex => 3, :checked => (params[:remember_me] == "yes") }, "yes" %>
<% end %>
<%= f.primary t(".login_button"), :tabindex => 4 %>
<div class="mb-3">
<%= f.primary t(".login_button"), :tabindex => 4 %>
</div>
<% end %>
<hr>
<%= render :partial => "auth_providers" %>
<% unless @preferred_auth_provider %>
<div class="d-flex justify-content-center align-items-center">
<div class="border-bottom border-1 flex-grow-1"></div>
<div class="text-secondary mx-3"><%= t ".with external" %></div>
<div class="border-bottom border-1 flex-grow-1"></div>
</div>
<%= render :partial => "auth_providers" %>
<% end %>
</div>

View file

@ -6,6 +6,10 @@
<% content_for :heading_class, "p-0 mw-100" %>
<% content_for :heading do %>
<% if @client_app_name %>
<p class="text-center text-muted fs-6 py-2 mb-0 bg-white"><%= t(".signup_to_authorize_html", :client_app_name => @client_app_name) %></p>
<% end %>
<div class="header-illustration new-user-main auth-container mx-auto">
<ul class="nav nav-tabs position-absolute bottom-0 px-3 fs-6 w-100">
<li class="nav-item">
@ -24,6 +28,15 @@
<p><strong><%= t ".about.header" %></strong> <%= t ".about.paragraph_1" %></p>
<p><%= t ".about.paragraph_2" %></p>
</div>
<% unless @preferred_auth_provider.nil? %>
<%= render :partial => "auth_providers" %>
<div class="d-flex justify-content-center align-items-center">
<div class="border-bottom border-1 flex-grow-1"></div>
<div class="text-secondary mx-3"><%= t ".or" %></div>
<div class="border-bottom border-1 flex-grow-1"></div>
</div>
<% end %>
<% else %>
<h4><%= t ".about.welcome" %></h4>
<% end %>
@ -34,7 +47,7 @@
<%= f.hidden_field :auth_provider unless current_user.auth_provider.nil? %>
<%= f.hidden_field :auth_uid unless current_user.auth_uid.nil? %>
<% if current_user.auth_uid.nil? or not current_user.errors[:email].empty? %>
<% if current_user.auth_uid.nil? or @verified_email.nil? or not current_user.errors[:email].empty? %>
<%= f.email_field :email, :help => t(".email_help_html",
:privacy_policy_link => link_to(t(".privacy_policy"),
t(".privacy_policy_url"),
@ -58,18 +71,17 @@
</div>
<% end %>
<p class="mb-3 text-muted"><%= t(".by_signing_up_html",
:tou_link => link_to(t("layouts.tou"),
"https://wiki.osmfoundation.org/wiki/Terms_of_Use",
:target => :new),
:privacy_policy_link => link_to(t(".privacy_policy"),
t(".privacy_policy_url"),
:title => t(".privacy_policy_title"),
:target => :new),
:contributor_terms_link => link_to(t(".contributor_terms"),
t(".contributor_terms_url"),
:target => :new)) %></p>
<p class="mb-3 text-muted fs-6"><%= t(".by_signing_up_html",
:tou_link => link_to(t("layouts.tou"),
"https://wiki.osmfoundation.org/wiki/Terms_of_Use",
:target => :new),
:privacy_policy_link => link_to(t(".privacy_policy"),
t(".privacy_policy_url"),
:title => t(".privacy_policy_title"),
:target => :new),
:contributor_terms_link => link_to(t(".contributor_terms"),
t(".contributor_terms_url"),
:target => :new)) %></p>
<%= f.form_group do %>
<%= f.check_box :consider_pd,
:tabindex => 5,
@ -84,8 +96,12 @@
</div>
<% end %>
<% if current_user.auth_uid.nil? %>
<hr>
<% if current_user.auth_uid.nil? and @preferred_auth_provider.nil? %>
<div class="d-flex justify-content-center align-items-center">
<div class="border-bottom border-1 flex-grow-1"></div>
<div class="text-secondary mx-3"><%= t ".use external auth" %></div>
<div class="border-bottom border-1 flex-grow-1"></div>
</div>
<%= render :partial => "auth_providers" %>
<% end %>
</div>