Merge pull request #29 from ZeusWPI/omniauth

Omniauth
This commit is contained in:
benji 2015-03-19 22:37:38 +01:00
commit ee1c7891aa
14 changed files with 100 additions and 7 deletions

View file

@ -75,4 +75,4 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
gem 'coveralls', require: false
gem 'omniauth'
gem 'omniauth-oauth2'

View file

@ -108,6 +108,8 @@ GEM
execjs (2.3.0)
faker (1.4.2)
i18n (~> 0.5)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.6-x64-mingw32)
globalid (0.3.2)
activesupport (>= 4.1.0)
@ -122,6 +124,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.2)
jwt (1.4.1)
launchy (2.4.3)
addressable (~> 2.3)
loofah (2.0.1)
@ -132,6 +135,8 @@ GEM
mini_portile (0.6.2)
minitest (5.5.1)
multi_json (1.10.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
mysql2 (0.3.17)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
@ -141,9 +146,20 @@ GEM
mini_portile (~> 0.6.0)
nokogiri (1.6.6.2-x64-mingw32)
mini_portile (~> 0.6.0)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
omniauth (1.2.2)
hashie (>= 1.2, < 4)
rack (~> 1.0)
omniauth-oauth2 (1.2.0)
faraday (>= 0.8, < 0.10)
multi_json (~> 1.3)
oauth2 (~> 1.0)
omniauth (~> 1.2)
orm_adapter (0.5.0)
paper_trail (4.0.0.beta2)
activerecord (>= 3.0, < 6.0)
@ -267,7 +283,7 @@ DEPENDENCIES
jquery-rails
launchy
mysql2
omniauth
omniauth-oauth2
paper_trail (~> 4.0.0.beta)
paperclip
rails (= 4.2)

View file

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

View file

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

View file

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

View file

@ -0,0 +1,2 @@
module CallbacksHelper
end

View file

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

View file

@ -14,5 +14,4 @@
<% end %>
</div>
<%= render "devise/shared/links" %>

View file

@ -1,5 +1,4 @@
<%= render partial: 'flash' %>
<%= text_field_tag :search %>
<div class="row">
<% @users.each do |user| %>

View file

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

View file

@ -0,0 +1,44 @@
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: "http://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'] }
info do
{
# :nickname => raw_info['username'],
}
end
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,5 @@
Rails.application.routes.draw do
devise_for :users
devise_for :users, controllers: { omniauth_callbacks: "callbacks" }
devise_scope :user do
unauthenticated :user do

View file

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

View file

@ -0,0 +1,7 @@
require 'test_helper'
class CallbacksControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end