Add oauth

This commit is contained in:
benji 2015-03-19 22:37:16 +01:00
parent c9a24e81e2
commit eaa076b508
11 changed files with 49 additions and 10 deletions

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 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] has_paper_trail only: [:debt_cents, :admin, :orders_count, :koelkast]
@ -44,6 +44,13 @@ class User < ActiveRecord::Base
scope :members, -> { where koelkast: false } 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 def debt
self.debt_cents / 100.0 self.debt_cents / 100.0
end end

View file

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

View file

@ -1,5 +1,6 @@
# Use this hook to configure devise mailer, warden hooks and so forth. # Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model. # Many of these configuration options can be set straight in your model.
# require 'yaml'
Devise.setup do |config| Devise.setup do |config|
# The secret key used by Devise. Devise uses this key to generate # The secret key used by Devise. Devise uses this key to generate
# random tokens. Changing this key will render invalid all existing # random tokens. Changing this key will render invalid all existing
@ -232,7 +233,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

@ -2,14 +2,19 @@ require 'omniauth-oauth2'
module OmniAuth module OmniAuth
module Strategies module Strategies
class Tab < OmniAuth::Strategies::OAuth2 class Zeuswpi < OmniAuth::Strategies::OAuth2
option :provider_ignores_state, true
# Give your strategy a name. # Give your strategy a name.
option :name, "tab" option :name, "zeuswpi"
# This is where you pass the options you would pass when # This is where you pass the options you would pass when
# initializing your consumer from the OAuth gem. # initializing your consumer from the OAuth gem.
option :client_options, { 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 # 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 # additional calls (if the user id is returned with the token
# or as a URI parameter). This may not be possible with all # or as a URI parameter). This may not be possible with all
# providers. # providers.
uid{ raw_info['id'] } uid{ raw_info['username'] }
info do info do
{ {
:name => raw_info['name'], # :nickname => raw_info['username'],
} }
end end
@ -32,7 +37,7 @@ module OmniAuth
end end
def raw_info def raw_info
@raw_info ||= access_token.get('/me').parsed @raw_info ||= access_token.get('/oauth/api/current_user/').parsed
end end
end end
end end

View file

@ -1,5 +1,5 @@
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :users devise_for :users, controllers: { omniauth_callbacks: "callbacks" }
devise_scope :user do devise_scope :user do
unauthenticated :user do unauthenticated :user do

View file

@ -12,6 +12,8 @@
development: development:
secret_key_base: 5d40610321e19e4f71ee2ba8af4f426fe15096c405da3800c6b33bed6779f2d11f55a0edc455974b19a01fd71f6cd508dba980305dbc55ff82521a2d12f891d8 secret_key_base: 5d40610321e19e4f71ee2ba8af4f426fe15096c405da3800c6b33bed6779f2d11f55a0edc455974b19a01fd71f6cd508dba980305dbc55ff82521a2d12f891d8
omniauth_client_id: "client_id"
omniauth_client_secret: "client_secret"
test: test:
secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81 secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81
@ -20,3 +22,6 @@ 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: ""

View file

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