Skip to content

Commit

Permalink
Merge pull request #3 from assaydepot/v2
Browse files Browse the repository at this point in the history
Version 2
  • Loading branch information
cpetersen authored Apr 23, 2024
2 parents e6b89a6 + 6f321cc commit 1fa3df9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lib/omniauth-scientist/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module OmniAuth
module Scientist
VERSION = "1.0.0"
VERSION = "2.0.0"
end
end
36 changes: 16 additions & 20 deletions lib/omniauth/strategies/scientist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module OmniAuth
module Strategies
class Scientist < OmniAuth::Strategies::OAuth2
option :client_options, {
:site => 'https://app.scientist.com',
:authorize_url => 'https://app.scientist.com/oauth/authorize',
:token_url => 'https://app.scientist.com/oauth/token'
site: 'https://app.scientist.com',
authorize_url: 'https://app.scientist.com/oauth/authorize',
token_url: 'https://app.scientist.com/oauth/token'
}

def request_phase
Expand All @@ -15,27 +15,29 @@ def request_phase

def authorize_params
super.tap do |params|
%w[scope client_options].each do |v|
%w[client_options].each do |v|
if request.params[v]
params[v.to_sym] = request.params[v]
end
end
end
end

uid { user_attribute('id') }
uid {
raw_info['id'].to_s
}

info do
{
'provider' => 'scientist',
'uid' => uid,
'email' => email,
'name' => "#{user_attribute('first_name')} #{user_attribute('last_name')}",
'first_name' => user_attribute('first_name'),
'last_name' => user_attribute('last_name'),
'title' => user_attribute('title'),
'company' => user_attribute('company'),
'site' => user_attribute('site')
'name' => "#{raw_info['first_name']} #{raw_info['last_name']}",
'first_name' => raw_info['first_name'],
'last_name' => raw_info['last_name'],
'title' => raw_info['title'],
'company' => raw_info['company'],
'site' => raw_info['site']
}
end

Expand All @@ -45,21 +47,15 @@ def authorize_params

def raw_info
access_token.options[:mode] = :header
@raw_info ||= { 'user' => access_token['user'] }
@raw_info ||= access_token["user"]
end

def email
user_attribute('email')
raw_info['email']
end

def callback_url
full_host + script_name + callback_path
end

protected

def user_attribute(attribute)
raw_info['user'][attribute] if raw_info['user']
full_host + callback_path
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions omniauth-scientist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = OmniAuth::Scientist::VERSION

gem.add_dependency 'omniauth', '~> 1.9'
gem.add_dependency 'omniauth-oauth2', '>= 1.6.0', '< 2.0'
gem.add_development_dependency 'rspec'
gem.add_dependency 'omniauth', '~> 2.0'
gem.add_dependency 'omniauth-oauth2', '~> 1.8'
gem.add_development_dependency 'rspec', '~> 3.5'
gem.add_development_dependency 'rack-test'
gem.add_development_dependency 'simplecov'
gem.add_development_dependency 'webmock'
Expand Down
57 changes: 18 additions & 39 deletions spec/omniauth/strategies/scientist_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
require 'spec_helper'

describe OmniAuth::Strategies::Scientist do
let(:access_token) { instance_double('AccessToken', :options => {}) }
let(:access_token) { instance_double('AccessToken', :options => {}, :[] => 'user') }
let(:parsed_response) { instance_double('ParsedResponse') }
let(:response) { instance_double('Response', :parsed => parsed_response) }

let(:enterprise_site) { 'https://some.other.site.com/' }
let(:enterprise_authorize_url) { 'https://some.other.site.com/oauth/authorize' }
let(:enterprise_token_url) { 'https://some.other.site.com/oauth/token' }
let(:enterprise_site) { 'https://some.other.site.com/api/v3' }
let(:enterprise_authorize_url) { 'https://some.other.site.com/login/oauth/authorize' }
let(:enterprise_token_url) { 'https://some.other.site.com/login/oauth/access_token' }
let(:enterprise) do
OmniAuth::Strategies::Scientist.new('SCIENTIST_ID', 'SCIENTIST_SECRET',
{
:client_options => {
:site => enterprise_site,
:authorize_url => enterprise_authorize_url,
:token_url => enterprise_token_url
OmniAuth::Strategies::Scientist.new('SIENTIST_KEY', 'SIENTIST_SECRET',
{
:client_options => {
:site => enterprise_site,
:authorize_url => enterprise_authorize_url,
:token_url => enterprise_token_url
}
}
}
)
end

Expand Down Expand Up @@ -61,40 +61,19 @@
allow(subject).to receive(:raw_info).and_return({ 'email' => '[email protected]' })
expect(subject.email).to eq('[email protected]')
end

it 'should return nil if there is no raw_info and email access is not allowed' do
allow(subject).to receive(:raw_info).and_return({})
expect(subject.email).to be_nil
end

it 'should not return the primary email if there is no raw_info and email access is allowed' do
emails = [
{ 'email' => '[email protected]', 'primary' => false },
{ 'email' => '[email protected]', 'primary' => true }
]
allow(subject).to receive(:raw_info).and_return({})
subject.options['scope'] = 'user'
allow(subject).to receive(:emails).and_return(emails)
expect(subject.email).to be_nil
end

it 'should not return the first email if there is no raw_info and email access is allowed' do
emails = [
{ 'email' => '[email protected]', 'primary' => false },
{ 'email' => '[email protected]', 'primary' => false }
]
allow(subject).to receive(:raw_info).and_return({})
subject.options['scope'] = 'user'
allow(subject).to receive(:emails).and_return(emails)
expect(subject.email).to be_nil
end
end

context '#raw_info' do
it 'should use relative paths' do
expect(access_token).to receive(:get).with('user').and_return(response)
expect(subject.raw_info).to eq(parsed_response)
end

it 'should use the header auth mode' do
expect(access_token).to receive(:get).with('user').and_return(response)
subject.raw_info
expect(access_token.options[:mode]).to eq(:header)
end
end

context '#info.email' do
Expand All @@ -113,4 +92,4 @@
expect(subject.callback_url).to eq('https://example.com/sub_uri/auth/scientist/callback')
end
end
end
end

0 comments on commit 1fa3df9

Please sign in to comment.