Skip to content

Commit

Permalink
implement OmniAuth strategy for Participa platform
Browse files Browse the repository at this point in the history
  • Loading branch information
zuzust committed Apr 4, 2017
1 parent a2deae0 commit ace99f2
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/omniauth-participa/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Omniauth
module Participa
VERSION = '0.1.0'
VERSION = '1.0.0.rc0'
end
end
46 changes: 46 additions & 0 deletions lib/omniauth/strategies/participa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@
module OmniAuth
module Strategies
class Participa < OmniAuth::Strategies::OAuth2

option :name, :participa
option :scope, 'public'
option :authorize_options, [:redirect_uri, :scope]

option :client_options, {
site: 'http://participa.dev',
authorize_url: '/oauth/authorize',
token_url: '/oauth/token'
}

def authorize_params
super.tap do |params|
options[:authorize_options].each do |k|
params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s])
end

params[:scope] = params[:scope].split(' ').map {|item| item.split(',')}.flatten.join(' ')
end
end

uid { raw_info['id'] }

# TODO: add user groups
info do
{
email: raw_info['email'],
name: raw_info['full_name'],
username: raw_info['username'],
admin: raw_info['admin']
}
end

extra do
skip_info? ? {} : { raw_info: raw_info }
end

def raw_info
@raw_info ||= acces_token.get('/api/v2/users/me').parsed
end

# https://github.com/intridea/omniauth-oauth2/issues/81
# https://github.com/doorkeeper-gem/doorkeeper/issues/732
def callback_url
options[:redirect_uri] || (full_host + script_name + callback_path)
end
end
end
end
124 changes: 122 additions & 2 deletions spec/omniauth/strategies/participa_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,127 @@
require 'spec_helper'

describe OmniAuth::Strategies::Participa do
it 'has a version number' do
expect(Omniauth::Participa::VERSION).not_to be nil
let(:request) { double('Request', params: {}, cookies: {}, env: {}) }
let(:app) { -> {[200, {}, ['Participa']]} }
let(:raw_info) { {'id' => 'uid', 'admin' => true, 'email' => '[email protected]', 'username' => 'jane-doe', 'full_name' => 'Jane Doe'} }

subject do
OmniAuth::Strategies::Participa.new(app, 'appid', 'secret', @options || {}).tap do |strategy|
allow(strategy).to receive(:request) {
request
}
end
end

before do
OmniAuth.config.test_mode = true
end

after do
OmniAuth.config.test_mode = false
end

describe '#client_options' do
it 'has correct authorize_url' do
expect(subject.client.options[:authorize_url]).to eq('/oauth/authorize')
end

it 'has correct token_url' do
expect(subject.client.options[:token_url]).to eq('/oauth/token')
end

describe 'overrides' do
it 'should allow overriding the site' do
@options = { client_options: {'site' => 'https://example.com'} }
expect(subject.client.site).to eq('https://example.com')
end

it 'should allow overriding the authorize_url' do
@options = { client_options: {'authorize_url' => 'https://example.com/oauth/authorize'} }
expect(subject.client.options[:authorize_url]).to eq('https://example.com/oauth/authorize')
end

it 'should allow overriding the token_url' do
@options = { client_options: {'token_url' => 'https://example.com/oauth/token'} }
expect(subject.client.options[:token_url]).to eq('https://example.com/oauth/token')
end
end
end

describe '#authorize_options' do
[:redirect_uri, :scope].each do |k|
it "should support #{k}" do
@options = { k => 'someval' }
expect(subject.authorize_params[k.to_s]).to eq('someval')
end
end

describe 'redirect_uri' do
it 'should default to nil' do
@options = {}
expect(subject.authorize_params['redirect_uri']).to eq(nil)
end

it 'should set the redirect_uri parameter if present' do
@options = { redirect_uri: 'htts://example.com/auth/participa/callback' }
expect(subject.authorize_params['redirect_uri']).to eq('htts://example.com/auth/participa/callback')
end
end

describe 'scope' do
it 'should set default scope to public' do
expect(subject.authorize_params['scope']).to eq('public')
end

it 'should join scopes' do
@options = { scope: 'public,write' }
expect(subject.authorize_params['scope']).to eq('public write')
end

it 'should support space delimited scopes' do
@options = { scope: 'public write' }
expect(subject.authorize_params['scope']).to eq('public write')
end
end
end

describe '#callback_path' do
it 'has the correct default callback path' do
expect(subject.callback_path).to eq('/auth/participa/callback')
end

it 'should set the callback_path parameter if present' do
@options = { callback_path: '/auth/foo/callback' }
expect(subject.callback_path).to eq('/auth/foo/callback')
end
end

describe '#uid' do
it 'should return the user id' do
allow(subject).to receive(:raw_info).and_return(raw_info)
expect(subject.uid).to eq(raw_info['id'])
end
end

describe '#info' do
before do
allow(subject).to receive(:raw_info).and_return(raw_info)
end

it 'should include the user email' do
expect(subject.info[:email]).to eq(raw_info['email'])
end

it 'should include the user full name' do
expect(subject.info[:name]).to eq(raw_info['full_name'])
end

it 'should include the username' do
expect(subject.info[:username]).to eq(raw_info['username'])
end

it 'should include the user admin flag' do
expect(subject.info[:admin]).to eq(raw_info['admin'])
end
end
end

0 comments on commit ace99f2

Please sign in to comment.