Add omniauth strategie for zeuswpi

This commit is contained in:
benji 2015-09-08 13:34:29 +02:00
parent 334054142c
commit 4dc8c36eee
6 changed files with 63 additions and 2 deletions

View file

@ -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

View file

@ -20,4 +20,10 @@ class User < ActiveRecord::Base
def transactions def transactions
Transaction.where("creditor_id = ? OR debtor_id = ?", id, id) Transaction.where("creditor_id = ? OR debtor_id = ?", id, id)
end end
def self.from_omniauth(auth)
where(name: auth.uid).first_or_create do |user|
user.name = auth.uid
end
end
end end

View file

@ -235,7 +235,7 @@ Devise.setup do |config|
# ==> OmniAuth # ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting # Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks. # 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 # ==> 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

@ -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

View file

@ -1,5 +1,8 @@
Rails.application.routes.draw do 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. # The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes". # See how all your routes lay out with "rake routes".

View file

@ -12,6 +12,8 @@
development: development:
secret_key_base: f196861eed46b817be7402ec85f917fa19c7a2f7a4f17e1ee4c9e8f657eea809be7a56951bd0af1f339237d1bcef739f4f5206ba06d68bcbe24866a8cb1e6b5f secret_key_base: f196861eed46b817be7402ec85f917fa19c7a2f7a4f17e1ee4c9e8f657eea809be7a56951bd0af1f339237d1bcef739f4f5206ba06d68bcbe24866a8cb1e6b5f
omniauth_client_id: tomtest
omniauth_client_secret: blargh
test: test:
secret_key_base: 74b63cafd732dfbdddede69122e2735485c9b30392b068d6260495fafd3c2cd71209fc396acdd3f06ab27f63b605fd31f9e76a5e4d5d3426878cb27dccba4db1 secret_key_base: 74b63cafd732dfbdddede69122e2735485c9b30392b068d6260495fafd3c2cd71209fc396acdd3f06ab27f63b605fd31f9e76a5e4d5d3426878cb27dccba4db1
@ -20,3 +22,5 @@ test:
# instead read values from the environment. # instead read values from the environment.
production: production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
omniauth_client_id: ""
omniauth_client_secret: ""