From 898dd81c6b55ca2eb80a979e3fa76cfd08496d5d Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Sat, 15 Apr 2017 23:08:25 +0200 Subject: [PATCH] Playing with OmniAuth However OmniAuth is mainly used for authorization. So let's try a more lightweight approach with just OpenID. --- Gemfile | 8 ++-- Gemfile.lock | 19 ++++---- .../users/omniauth_callbacks_controller.rb | 11 +++-- .../mailer/confirmation_instructions.html.erb | 5 +++ .../devise/mailer/password_change.html.erb | 3 ++ .../reset_password_instructions.html.erb | 8 ++++ .../mailer/unlock_instructions.html.erb | 7 +++ app/views/devise/passwords/edit.html.erb | 25 +++++++++++ app/views/devise/passwords/new.html.erb | 16 +++++++ app/views/devise/registrations/edit.html.erb | 43 +++++++++++++++++++ app/views/devise/registrations/new.html.erb | 29 +++++++++++++ app/views/devise/sessions/new.slim | 12 +++++- app/views/devise/shared/_links.html.erb | 25 +++++++++++ app/views/devise/unlocks/new.html.erb | 16 +++++++ config/initializers/devise.rb | 13 +++++- 15 files changed, 219 insertions(+), 21 deletions(-) create mode 100644 app/views/devise/mailer/confirmation_instructions.html.erb create mode 100644 app/views/devise/mailer/password_change.html.erb create mode 100644 app/views/devise/mailer/reset_password_instructions.html.erb create mode 100644 app/views/devise/mailer/unlock_instructions.html.erb create mode 100644 app/views/devise/passwords/edit.html.erb create mode 100644 app/views/devise/passwords/new.html.erb create mode 100644 app/views/devise/registrations/edit.html.erb create mode 100644 app/views/devise/registrations/new.html.erb create mode 100644 app/views/devise/shared/_links.html.erb create mode 100644 app/views/devise/unlocks/new.html.erb diff --git a/Gemfile b/Gemfile index 89bc3c9..9f59149 100644 --- a/Gemfile +++ b/Gemfile @@ -111,7 +111,7 @@ gem "font-awesome-rails" # gem "recaptcha", require: "recaptcha/rails" -gem "sprockets", '3.6.3' +gem "sprockets" #gem "activemodel-serializers-xml" @@ -120,7 +120,9 @@ gem 'omniauth' gem 'omniauth-amazon' gem 'omniauth-google-oauth2' gem 'omniauth-github' -gem 'omniauth-twitter' -gem 'omniauth-facebook' +# gem 'omniauth-twitter' +gem 'omniauth-openid' +# gem 'omniauth-facebook' # gem 'omniauth-launchpad' # is broken gem 'omniauth-launchpad', :git => 'https://github.com/joaopapereira/omniauth-launchpad' +# gem 'omniauth-openid-connect' diff --git a/Gemfile.lock b/Gemfile.lock index 69a9ed7..126d7ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,8 +183,6 @@ GEM omniauth-amazon (1.0.1) omniauth (~> 1.0) omniauth-oauth2 (~> 1.1) - omniauth-facebook (4.0.0) - omniauth-oauth2 (~> 1.2) omniauth-github (1.2.3) omniauth (~> 1.5) omniauth-oauth2 (>= 1.4.0, < 2.0) @@ -199,9 +197,9 @@ GEM omniauth-oauth2 (1.4.0) oauth2 (~> 1.0) omniauth (~> 1.2) - omniauth-twitter (1.4.0) - omniauth-oauth (~> 1.1) - rack + omniauth-openid (1.0.1) + omniauth (~> 1.0) + rack-openid (~> 1.3.1) orm_adapter (0.5.0) paperclip (5.1.0) activemodel (>= 4.2.0) @@ -220,6 +218,9 @@ GEM slop (~> 3.4) puma (3.8.2) rack (2.0.1) + rack-openid (1.3.1) + rack (>= 1.1.0) + ruby-openid (>= 2.1.8) rack-test (0.6.3) rack (>= 1.0) rails (5.0.2) @@ -259,6 +260,7 @@ GEM responders (2.3.0) railties (>= 4.2.0, < 5.1) ruby-graphviz (1.2.3) + ruby-openid (2.7.0) ruby-progressbar (1.8.1) sass (3.4.23) sass-rails (5.0.6) @@ -284,7 +286,7 @@ GEM spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) - sprockets (3.6.3) + sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-es6 (0.9.2) @@ -344,11 +346,10 @@ DEPENDENCIES minitest-reporters omniauth omniauth-amazon - omniauth-facebook omniauth-github omniauth-google-oauth2 omniauth-launchpad! - omniauth-twitter + omniauth-openid paperclip pg pg_search @@ -360,7 +361,7 @@ DEPENDENCIES slim-rails spring spring-watcher-listen (~> 2.0.0) - sprockets (= 3.6.3) + sprockets therubyracer turbolinks (~> 5) uglifier (>= 1.3.0) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 5c664f1..32ca1f4 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -16,7 +16,6 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController # session["devise.google_data"] = request.env["omniauth.auth"].except(:extra) #Removing extra as it can overflow some session stores redirect_to new_user_session_url, alert: @user.errors.full_messages.join("\n") end - end def google_oauth2 @@ -45,12 +44,12 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController end end - # def failure - # redirect_to root_path - # end + def failure + redirect_to root_path + end + + private - # private - # # def providers # ["twitter", "google_oauth2", "launchpad", "amazon"] # end diff --git a/app/views/devise/mailer/confirmation_instructions.html.erb b/app/views/devise/mailer/confirmation_instructions.html.erb new file mode 100644 index 0000000..dc55f64 --- /dev/null +++ b/app/views/devise/mailer/confirmation_instructions.html.erb @@ -0,0 +1,5 @@ +

Welcome <%= @email %>!

+ +

You can confirm your account email through the link below:

+ +

<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %>

diff --git a/app/views/devise/mailer/password_change.html.erb b/app/views/devise/mailer/password_change.html.erb new file mode 100644 index 0000000..b41daf4 --- /dev/null +++ b/app/views/devise/mailer/password_change.html.erb @@ -0,0 +1,3 @@ +

Hello <%= @resource.email %>!

+ +

We're contacting you to notify you that your password has been changed.

diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/devise/mailer/reset_password_instructions.html.erb new file mode 100644 index 0000000..f667dc1 --- /dev/null +++ b/app/views/devise/mailer/reset_password_instructions.html.erb @@ -0,0 +1,8 @@ +

Hello <%= @resource.email %>!

+ +

Someone has requested a link to change your password. You can do this through the link below.

+ +

<%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %>

+ +

If you didn't request this, please ignore this email.

+

Your password won't change until you access the link above and create a new one.

diff --git a/app/views/devise/mailer/unlock_instructions.html.erb b/app/views/devise/mailer/unlock_instructions.html.erb new file mode 100644 index 0000000..41e148b --- /dev/null +++ b/app/views/devise/mailer/unlock_instructions.html.erb @@ -0,0 +1,7 @@ +

Hello <%= @resource.email %>!

+ +

Your account has been locked due to an excessive number of unsuccessful sign in attempts.

+ +

Click the link below to unlock your account:

+ +

<%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %>

diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb new file mode 100644 index 0000000..6a796b0 --- /dev/null +++ b/app/views/devise/passwords/edit.html.erb @@ -0,0 +1,25 @@ +

Change your password

+ +<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %> + <%= devise_error_messages! %> + <%= f.hidden_field :reset_password_token %> + +
+ <%= f.label :password, "New password" %>
+ <% if @minimum_password_length %> + (<%= @minimum_password_length %> characters minimum)
+ <% end %> + <%= f.password_field :password, autofocus: true, autocomplete: "off" %> +
+ +
+ <%= f.label :password_confirmation, "Confirm new password" %>
+ <%= f.password_field :password_confirmation, autocomplete: "off" %> +
+ +
+ <%= f.submit "Change my password" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/passwords/new.html.erb b/app/views/devise/passwords/new.html.erb new file mode 100644 index 0000000..3d6d11a --- /dev/null +++ b/app/views/devise/passwords/new.html.erb @@ -0,0 +1,16 @@ +

Forgot your password?

+ +<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %> + <%= devise_error_messages! %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true %> +
+ +
+ <%= f.submit "Send me reset password instructions" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb new file mode 100644 index 0000000..1e66f3d --- /dev/null +++ b/app/views/devise/registrations/edit.html.erb @@ -0,0 +1,43 @@ +

Edit <%= resource_name.to_s.humanize %>

+ +<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> + <%= devise_error_messages! %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true %> +
+ + <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> +
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
+ <% end %> + +
+ <%= f.label :password %> (leave blank if you don't want to change it)
+ <%= f.password_field :password, autocomplete: "off" %> + <% if @minimum_password_length %> +
+ <%= @minimum_password_length %> characters minimum + <% end %> +
+ +
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation, autocomplete: "off" %> +
+ +
+ <%= f.label :current_password %> (we need your current password to confirm your changes)
+ <%= f.password_field :current_password, autocomplete: "off" %> +
+ +
+ <%= f.submit "Update" %> +
+<% end %> + +

Cancel my account

+ +

Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>

+ +<%= link_to "Back", :back %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb new file mode 100644 index 0000000..5a238ce --- /dev/null +++ b/app/views/devise/registrations/new.html.erb @@ -0,0 +1,29 @@ +

Sign up

+ +<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> + <%= devise_error_messages! %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true %> +
+ +
+ <%= f.label :password %> + <% if @minimum_password_length %> + (<%= @minimum_password_length %> characters minimum) + <% end %>
+ <%= f.password_field :password, autocomplete: "off" %> +
+ +
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation, autocomplete: "off" %> +
+ +
+ <%= f.submit "Sign up" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/app/views/devise/sessions/new.slim b/app/views/devise/sessions/new.slim index 20ed454..c963195 100644 --- a/app/views/devise/sessions/new.slim +++ b/app/views/devise/sessions/new.slim @@ -35,8 +35,16 @@ / = link_to 'log in using your secure certificate', users_debian_sso_path / = link_to "Sign in with Launchpad", user_launchpad_omniauth_authorize_path -= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path +/ = link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path = link_to "Sign in with Launchpad", user_launchpad_omniauth_authorize_path -= link_to "Sign in with Amazon", user_amazon_omniauth_authorize_path +div + = link_to image_tag('/images/sso/google.png'), user_google_oauth2_omniauth_authorize_path + +div + = link_to image_tag('/images/sso/amazon.png'), user_amazon_omniauth_authorize_path + +div + stackexchange + diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb new file mode 100644 index 0000000..e6a3e41 --- /dev/null +++ b/app/views/devise/shared/_links.html.erb @@ -0,0 +1,25 @@ +<%- if controller_name != 'sessions' %> + <%= link_to "Log in", new_session_path(resource_name) %>
+<% end -%> + +<%- if devise_mapping.registerable? && controller_name != 'registrations' %> + <%= link_to "Sign up", new_registration_path(resource_name) %>
+<% end -%> + +<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> + <%= link_to "Forgot your password?", new_password_path(resource_name) %>
+<% end -%> + +<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %> + <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
+<% end -%> + +<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %> + <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %>
+<% end -%> + +<%- if devise_mapping.omniauthable? %> + <%- resource_class.omniauth_providers.each do |provider| %> + <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %>
+ <% end -%> +<% end -%> diff --git a/app/views/devise/unlocks/new.html.erb b/app/views/devise/unlocks/new.html.erb new file mode 100644 index 0000000..16586bc --- /dev/null +++ b/app/views/devise/unlocks/new.html.erb @@ -0,0 +1,16 @@ +

Resend unlock instructions

+ +<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %> + <%= devise_error_messages! %> + +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true %> +
+ +
+ <%= f.submit "Resend unlock instructions" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index f5a4423..ca0b382 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -276,10 +276,21 @@ Devise.setup do |config| # config.omniauth :launchpad, 'debshots' config.omniauth :google_oauth2, '398593918966-0fpmit0pb6ptio1ndsie5dfcnqhei63l.apps.googleusercontent.com', 'uRQq_dLs1kx7l5sYIyEyPVRc', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} - config.omniauth :launchpad, 'Debian Screenshots', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} + # config.omniauth :launchpad, 'Debian Screenshots', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} config.omniauth :amazon, 'amzn1.application-oa2-client.78d832919fc24456b0636762cf18efd9', '8c495d0940ca4100313c03e93b8b4240eef256d2fdbe672718506f29f5a20f61', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} + config.omniauth :openid, :client_options => { + name: :launchpad, + identifier: 'https://login.ubuntu.com' + # # scope: [:openid, :email], + # response_type: :code, + # client_options: { + # port: 443, + # scheme: "https", + # host: "login.ubuntu.com" } + } + end # OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE