diff --git a/app/assets/javascripts/callbacks.js.coffee b/app/assets/javascripts/callbacks.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/callbacks.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/callbacks.css.scss b/app/assets/stylesheets/callbacks.css.scss new file mode 100644 index 0000000..e4c4d53 --- /dev/null +++ b/app/assets/stylesheets/callbacks.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the callbacks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/callbacks_controller.rb b/app/controllers/callbacks_controller.rb new file mode 100644 index 0000000..bbab5e8 --- /dev/null +++ b/app/controllers/callbacks_controller.rb @@ -0,0 +1,7 @@ +class CallbacksController < Devise::OmniauthCallbacksController + def zeuswpi + @user = User.from_omniauth(request.env["omniauth.auth"]) + @user.save + sign_in_and_redirect @user + end +end diff --git a/app/helpers/callbacks_helper.rb b/app/helpers/callbacks_helper.rb new file mode 100644 index 0000000..6c9550c --- /dev/null +++ b/app/helpers/callbacks_helper.rb @@ -0,0 +1,2 @@ +module CallbacksHelper +end diff --git a/app/models/user.rb b/app/models/user.rb index bd7ace0..6a4a158 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,7 +27,7 @@ # class User < ActiveRecord::Base - devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable + devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:zeuswpi] has_paper_trail only: [:debt_cents, :admin, :orders_count, :koelkast] @@ -44,6 +44,13 @@ class User < ActiveRecord::Base scope :members, -> { where koelkast: false } + def self.from_omniauth(auth) + where(provider: auth.provider, uid: auth.uid).first_or_create do |user| + user.provider = auth.provider + user.uid = auth.uid + end + end + def debt self.debt_cents / 100.0 end diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 7f01101..222f8a5 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -14,5 +14,4 @@ <% end %> - <%= render "devise/shared/links" %> diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 1f4e17f..447d856 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -1,5 +1,6 @@ # Use this hook to configure devise mailer, warden hooks and so forth. # Many of these configuration options can be set straight in your model. +# require 'yaml' Devise.setup do |config| # The secret key used by Devise. Devise uses this key to generate # random tokens. Changing this key will render invalid all existing @@ -232,7 +233,7 @@ Devise.setup do |config| # ==> OmniAuth # 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' + config.omniauth :zeuswpi, Rails.application.secrets.omniauth_client_id, Rails.application.secrets.omniauth_client_secret # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/config/initializers/tab.rb b/config/initializers/zeuswpi.rb similarity index 62% rename from config/initializers/tab.rb rename to config/initializers/zeuswpi.rb index 1c73d89..8b16a5b 100644 --- a/config/initializers/tab.rb +++ b/config/initializers/zeuswpi.rb @@ -2,14 +2,19 @@ require 'omniauth-oauth2' module OmniAuth module Strategies - class Tab < OmniAuth::Strategies::OAuth2 + class Zeuswpi < OmniAuth::Strategies::OAuth2 + + option :provider_ignores_state, true + # Give your strategy a name. - option :name, "tab" + option :name, "zeuswpi" # This is where you pass the options you would pass when # initializing your consumer from the OAuth gem. option :client_options, { - :site => "http://kelder.zeus.ugent.be/oauth/oauth2/authorize/" + site: "http://kelder.zeus.ugent.be", + authorize_url: "/oauth/oauth2/authorize/", + token_url: "/oauth/oauth2/token/", } # These are called after authentication has succeeded. If @@ -17,11 +22,11 @@ module OmniAuth # additional calls (if the user id is returned with the token # or as a URI parameter). This may not be possible with all # providers. - uid{ raw_info['id'] } + uid{ raw_info['username'] } info do { - :name => raw_info['name'], + # :nickname => raw_info['username'], } end @@ -32,7 +37,7 @@ module OmniAuth end def raw_info - @raw_info ||= access_token.get('/me').parsed + @raw_info ||= access_token.get('/oauth/api/current_user/').parsed end end end diff --git a/config/routes.rb b/config/routes.rb index 2b91e32..7e68b34 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - devise_for :users + devise_for :users, controllers: { omniauth_callbacks: "callbacks" } devise_scope :user do unauthenticated :user do diff --git a/config/secrets.yml b/config/secrets.yml index 90d7301..226185a 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -12,6 +12,8 @@ development: secret_key_base: 5d40610321e19e4f71ee2ba8af4f426fe15096c405da3800c6b33bed6779f2d11f55a0edc455974b19a01fd71f6cd508dba980305dbc55ff82521a2d12f891d8 + omniauth_client_id: "client_id" + omniauth_client_secret: "client_secret" test: secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81 @@ -20,3 +22,6 @@ test: # instead read values from the environment. production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> + omniauth_client_id: "" + omniauth_client_secret: "" + diff --git a/test/controllers/callbacks_controller_test.rb b/test/controllers/callbacks_controller_test.rb new file mode 100644 index 0000000..6d160fc --- /dev/null +++ b/test/controllers/callbacks_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CallbacksControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end