diff --git a/app/controllers/callbacks_controller.rb b/app/controllers/callbacks_controller.rb new file mode 100644 index 0000000..dcd8663 --- /dev/null +++ b/app/controllers/callbacks_controller.rb @@ -0,0 +1,10 @@ +class CallbacksController < Devise::OmniauthCallbacksController + def zeuswpi + @user = User.from_omniauth(request.env["omniauth.auth"]) + sign_in_and_redirect @user + end + + def after_omniauth_failure_path_for(scope) + root_path + end +end diff --git a/app/models/user.rb b/app/models/user.rb index cbc6d09..85881b5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,4 +20,10 @@ class User < ActiveRecord::Base def transactions Transaction.where("creditor_id = ? OR debtor_id = ?", id, id) end + + def self.from_omniauth(auth) + where(name: auth.uid).first_or_create do |user| + user.name = auth.uid + end + end end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 25e238d..15aa283 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -235,7 +235,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/zeuswpi.rb b/config/initializers/zeuswpi.rb new file mode 100644 index 0000000..857e150 --- /dev/null +++ b/config/initializers/zeuswpi.rb @@ -0,0 +1,38 @@ +require 'omniauth-oauth2' + +module OmniAuth + module Strategies + class Zeuswpi < OmniAuth::Strategies::OAuth2 + + option :provider_ignores_state, true + + # Give your strategy a name. + 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: 'https://kelder.zeus.ugent.be', + authorize_url: '/oauth/oauth2/authorize/', + token_url: '/oauth/oauth2/token/', + } + + # These are called after authentication has succeeded. If + # possible, you should try to set the UID without making + # 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['username'] } + + extra do + { + 'raw_info' => raw_info + } + end + + def raw_info + @raw_info ||= access_token.get('/oauth/api/current_user/').parsed + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index fc2791d..6547aea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ Rails.application.routes.draw do - devise_for :users + devise_for :users, controllers: { + omniauth_callbacks: 'callbacks' + } + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/config/secrets.yml b/config/secrets.yml index c195577..c9b7fd3 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -12,6 +12,8 @@ development: secret_key_base: f196861eed46b817be7402ec85f917fa19c7a2f7a4f17e1ee4c9e8f657eea809be7a56951bd0af1f339237d1bcef739f4f5206ba06d68bcbe24866a8cb1e6b5f + omniauth_client_id: tomtest + omniauth_client_secret: blargh test: secret_key_base: 74b63cafd732dfbdddede69122e2735485c9b30392b068d6260495fafd3c2cd71209fc396acdd3f06ab27f63b605fd31f9e76a5e4d5d3426878cb27dccba4db1 @@ -20,3 +22,5 @@ test: # instead read values from the environment. production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> + omniauth_client_id: "" + omniauth_client_secret: ""