From e788d26699452432f452f6e4072ebbd2d8c1884d Mon Sep 17 00:00:00 2001 From: Clemens Helm Date: Wed, 5 Jun 2013 11:56:06 +0200 Subject: [PATCH 01/16] Added fallbacks for non-primary Bitbucket emails. --- lib/omniauth/strategies/bitbucket.rb | 5 ++-- spec/omniauth/strategies/bitbucket_spec.rb | 29 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb index dc81f38..54e960a 100644 --- a/lib/omniauth/strategies/bitbucket.rb +++ b/lib/omniauth/strategies/bitbucket.rb @@ -30,8 +30,9 @@ class Bitbucket < OmniAuth::Strategies::OAuth def raw_info @raw_info ||= begin ri = MultiJson.decode(access_token.get('/api/1.0/user').body)['user'] - email = (MultiJson.decode(access_token.get('/api/1.0/emails').body).find { |email| email['primary'] })['email'] - ri.merge('email' => email) if email + emails = MultiJson.decode(access_token.get('/api/1.0/emails').body) + email_hash = emails.find { |email| email['primary'] } || emails.first || {} + ri.merge('email' => email_hash['email']) end end end diff --git a/spec/omniauth/strategies/bitbucket_spec.rb b/spec/omniauth/strategies/bitbucket_spec.rb index 74fcf29..32a9f8a 100644 --- a/spec/omniauth/strategies/bitbucket_spec.rb +++ b/spec/omniauth/strategies/bitbucket_spec.rb @@ -1,7 +1,32 @@ require 'spec_helper' describe OmniAuth::Strategies::Bitbucket do - it 'should do some testing' do - pending + context "email" do + let(:email) { "#{("a".."z").to_a.sample(6).join}@example.com" } + let(:strategy) { OmniAuth::Strategies::Bitbucket.new nil } + + it 'should fall back to non-primary' do + strategy.stub_chain(:access_token, :get) do |url| + body = url =~ /emails$/ ? %{[{"active": true, "email": "#{email}", "primary": false}]} : '{"user": {}}' + double body: body + end + strategy.raw_info['email'].should == email + end + + it "should prefer primary" do + strategy.stub_chain(:access_token, :get) do |url| + body = url =~ /emails$/ ? %{[{"active": true, "email": "someemail@example.com", "primary": false}, {"active": true, "email": "#{email}", "primary": true}]} : '{"user": {}}' + double body: body + end + strategy.raw_info['email'].should == email + end + + it "should ignore non-existing" do + strategy.stub_chain(:access_token, :get) do |url| + body = url =~ /emails$/ ? %{[]} : '{"user": {}}' + double body: body + end + strategy.raw_info['email'].should be_nil + end end end From 24db9360ef6490e458eb7bc32f9fe45f6abc6a00 Mon Sep 17 00:00:00 2001 From: Mauro Morales Date: Thu, 27 Jun 2019 15:41:10 +0200 Subject: [PATCH 02/16] Switch to api v2 endpoints --- lib/omniauth/strategies/bitbucket.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb index 54e960a..54850cb 100644 --- a/lib/omniauth/strategies/bitbucket.rb +++ b/lib/omniauth/strategies/bitbucket.rb @@ -21,17 +21,18 @@ class Bitbucket < OmniAuth::Strategies::OAuth info do { - :name => "#{raw_info['first_name']} #{raw_info['last_name']}", - :avatar => raw_info['avatar'], + :name => raw_info['nickname'], + :avatar => raw_info['links']['avatar']['href'], :email => raw_info['email'] } end def raw_info @raw_info ||= begin - ri = MultiJson.decode(access_token.get('/api/1.0/user').body)['user'] - emails = MultiJson.decode(access_token.get('/api/1.0/emails').body) - email_hash = emails.find { |email| email['primary'] } || emails.first || {} + debugger + ri = MultiJson.decode(access_token.get('/api/2.0/user').body) + emails = MultiJson.decode(access_token.get('/api/2.0/user/emails').body) + email_hash = emails['values'].find { |email| email['is_primary'] && email['type'] == 'email' } ri.merge('email' => email_hash['email']) end end From c7e19db03be20836507f0d1a9b0605a5d8185c65 Mon Sep 17 00:00:00 2001 From: Mauro Morales Date: Thu, 27 Jun 2019 15:56:03 +0200 Subject: [PATCH 03/16] remove debugger --- lib/omniauth/strategies/bitbucket.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb index 54850cb..58bce4e 100644 --- a/lib/omniauth/strategies/bitbucket.rb +++ b/lib/omniauth/strategies/bitbucket.rb @@ -1,4 +1,5 @@ require 'omniauth-oauth' +require 'multi_json' module OmniAuth module Strategies @@ -29,9 +30,8 @@ class Bitbucket < OmniAuth::Strategies::OAuth def raw_info @raw_info ||= begin - debugger - ri = MultiJson.decode(access_token.get('/api/2.0/user').body) - emails = MultiJson.decode(access_token.get('/api/2.0/user/emails').body) + ri = ::MultiJson.decode(access_token.get('/api/2.0/user').body) + emails = ::MultiJson.decode(access_token.get('/api/2.0/user/emails').body) email_hash = emails['values'].find { |email| email['is_primary'] && email['type'] == 'email' } ri.merge('email' => email_hash['email']) end From 6c3d12ee2d43c6c6d2f52f7ff1b5f860cfe09fff Mon Sep 17 00:00:00 2001 From: Mauro Morales Date: Thu, 27 Jun 2019 16:04:17 +0200 Subject: [PATCH 04/16] Update tests --- lib/omniauth/strategies/bitbucket.rb | 6 +++++- spec/omniauth/strategies/bitbucket_spec.rb | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb index 58bce4e..f63a857 100644 --- a/lib/omniauth/strategies/bitbucket.rb +++ b/lib/omniauth/strategies/bitbucket.rb @@ -33,7 +33,11 @@ def raw_info ri = ::MultiJson.decode(access_token.get('/api/2.0/user').body) emails = ::MultiJson.decode(access_token.get('/api/2.0/user/emails').body) email_hash = emails['values'].find { |email| email['is_primary'] && email['type'] == 'email' } - ri.merge('email' => email_hash['email']) + if email_hash + ri.merge('email' => email_hash['email']) + else + ri + end end end end diff --git a/spec/omniauth/strategies/bitbucket_spec.rb b/spec/omniauth/strategies/bitbucket_spec.rb index 32a9f8a..5b1df48 100644 --- a/spec/omniauth/strategies/bitbucket_spec.rb +++ b/spec/omniauth/strategies/bitbucket_spec.rb @@ -7,7 +7,7 @@ it 'should fall back to non-primary' do strategy.stub_chain(:access_token, :get) do |url| - body = url =~ /emails$/ ? %{[{"active": true, "email": "#{email}", "primary": false}]} : '{"user": {}}' + body = url =~ /emails$/ ? %{{"pagelen": 10, "values": [{"is_primary": true, "is_confirmed": true, "type": "email", "email": "#{email}", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/user/emails/#{email}"}}}], "page": 1, "size": 1}} : '{"user": {}}' double body: body end strategy.raw_info['email'].should == email @@ -15,7 +15,7 @@ it "should prefer primary" do strategy.stub_chain(:access_token, :get) do |url| - body = url =~ /emails$/ ? %{[{"active": true, "email": "someemail@example.com", "primary": false}, {"active": true, "email": "#{email}", "primary": true}]} : '{"user": {}}' + body = url =~ /emails$/ ? %{{"pagelen": 10, "values": [{"is_primary": true, "is_confirmed": true, "type": "email", "email": "#{email}", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/user/emails/#{email}"}}}], "page": 1, "size": 1}} : '{"user": {}}' double body: body end strategy.raw_info['email'].should == email @@ -23,7 +23,7 @@ it "should ignore non-existing" do strategy.stub_chain(:access_token, :get) do |url| - body = url =~ /emails$/ ? %{[]} : '{"user": {}}' + body = url =~ /emails$/ ? %{{"pagelen": 0, "values": []}} : '{"user": {}}' double body: body end strategy.raw_info['email'].should be_nil From d8d0c3e059ffe897bd3d6487c9e8f76a1066b911 Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:16:01 -0500 Subject: [PATCH 05/16] Sort alphabetically --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0c577db..2370da5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.gem +.DS_Store .bundle Gemfile.lock pkg/* -.DS_Store \ No newline at end of file From 9aa5be6537afbf4bd93c9b68e31f74439ea8cdc3 Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:20:09 -0500 Subject: [PATCH 06/16] Ignore coverage folder --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2370da5..7fb8395 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .DS_Store .bundle Gemfile.lock +coverage pkg/* From f4ffe891e280859455d72705c45c8e10409ef887 Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:30:02 -0500 Subject: [PATCH 07/16] Tidy --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index d28e81a..5988a5e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source "https://rubygems.org" +source 'https://rubygems.org' gemspec From 549e12ce6c072f7251a53b5f3f96e6ca7964dd7d Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:30:32 -0500 Subject: [PATCH 08/16] Use Ruby 3.1.5 --- .ruby-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..3ad0595 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.1.5 From 40c4ea64dbbc03bc1c6fd275c38c704d64879fd4 Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:35:15 -0500 Subject: [PATCH 09/16] Update gem homepage --- omniauth-bitbucket.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omniauth-bitbucket.gemspec b/omniauth-bitbucket.gemspec index 0cf409b..6aba13b 100644 --- a/omniauth-bitbucket.gemspec +++ b/omniauth-bitbucket.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.version = Omniauth::Bitbucket::VERSION s.authors = ["Dingding Ye"] s.email = ["yedingding@gmail.com"] - s.homepage = "https://github.com/sishen/omniauth-bitbucket" + s.homepage = "https://github.com/codeship/omniauth-bitbucket" s.summary = %q{OmniAuth strategy for Bitbucket.} s.description = %q{OmniAuth strategy for Bitbucket.} From 6947a1a9801dc08247e1da9fc5188163f7e93dd3 Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:36:05 -0500 Subject: [PATCH 10/16] multi_json 1.15.0 --- omniauth-bitbucket.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omniauth-bitbucket.gemspec b/omniauth-bitbucket.gemspec index 6aba13b..13f01ec 100644 --- a/omniauth-bitbucket.gemspec +++ b/omniauth-bitbucket.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| # specify any dependencies here; for example: s.add_dependency 'omniauth', '~> 1.1' s.add_dependency 'omniauth-oauth', '~> 1.0' - s.add_dependency 'multi_json', '~> 1.7' + s.add_dependency 'multi_json', '>= 1.15.0', '< 2.0' s.add_development_dependency 'rspec', '~> 2.7' s.add_development_dependency 'rack-test' From 971eaf5dac5f3fb38843c7cabb000fee917c293a Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:40:04 -0500 Subject: [PATCH 11/16] rspec 3 --- omniauth-bitbucket.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omniauth-bitbucket.gemspec b/omniauth-bitbucket.gemspec index 13f01ec..6630e58 100644 --- a/omniauth-bitbucket.gemspec +++ b/omniauth-bitbucket.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |s| s.add_dependency 'omniauth-oauth', '~> 1.0' s.add_dependency 'multi_json', '>= 1.15.0', '< 2.0' - s.add_development_dependency 'rspec', '~> 2.7' + s.add_development_dependency 'rspec', '~> 3' s.add_development_dependency 'rack-test' s.add_development_dependency 'simplecov' s.add_development_dependency 'webmock' From 8ab3144668ffcc4cb1ca10d5254d4de6848bde8c Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:40:45 -0500 Subject: [PATCH 12/16] Update specs for rspec 3 --- spec/omniauth/strategies/bitbucket_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/omniauth/strategies/bitbucket_spec.rb b/spec/omniauth/strategies/bitbucket_spec.rb index 5b1df48..45077a3 100644 --- a/spec/omniauth/strategies/bitbucket_spec.rb +++ b/spec/omniauth/strategies/bitbucket_spec.rb @@ -6,27 +6,27 @@ let(:strategy) { OmniAuth::Strategies::Bitbucket.new nil } it 'should fall back to non-primary' do - strategy.stub_chain(:access_token, :get) do |url| + allow(strategy).to receive_message_chain(:access_token, :get) do |url| body = url =~ /emails$/ ? %{{"pagelen": 10, "values": [{"is_primary": true, "is_confirmed": true, "type": "email", "email": "#{email}", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/user/emails/#{email}"}}}], "page": 1, "size": 1}} : '{"user": {}}' double body: body end - strategy.raw_info['email'].should == email + expect(strategy.raw_info['email']).to eq(email) end it "should prefer primary" do - strategy.stub_chain(:access_token, :get) do |url| + allow(strategy).to receive_message_chain(:access_token, :get) do |url| body = url =~ /emails$/ ? %{{"pagelen": 10, "values": [{"is_primary": true, "is_confirmed": true, "type": "email", "email": "#{email}", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/user/emails/#{email}"}}}], "page": 1, "size": 1}} : '{"user": {}}' double body: body end - strategy.raw_info['email'].should == email + expect(strategy.raw_info['email']).to eq(email) end it "should ignore non-existing" do - strategy.stub_chain(:access_token, :get) do |url| + allow(strategy).to receive_message_chain(:access_token, :get) do |url| body = url =~ /emails$/ ? %{{"pagelen": 0, "values": []}} : '{"user": {}}' double body: body end - strategy.raw_info['email'].should be_nil + expect(strategy.raw_info['email']).to eq(nil) end end end From be66ca278b32f6bd72c96642729a75ae26eff475 Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 12:45:03 -0500 Subject: [PATCH 13/16] Refactor --- lib/omniauth/strategies/bitbucket.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb index f63a857..6127188 100644 --- a/lib/omniauth/strategies/bitbucket.rb +++ b/lib/omniauth/strategies/bitbucket.rb @@ -30,14 +30,10 @@ class Bitbucket < OmniAuth::Strategies::OAuth def raw_info @raw_info ||= begin - ri = ::MultiJson.decode(access_token.get('/api/2.0/user').body) - emails = ::MultiJson.decode(access_token.get('/api/2.0/user/emails').body) - email_hash = emails['values'].find { |email| email['is_primary'] && email['type'] == 'email' } - if email_hash - ri.merge('email' => email_hash['email']) - else - ri - end + ri = MultiJson.decode(access_token.get('/api/2.0/user').body) + email = MultiJson.decode(access_token.get('/api/2.0/user/emails').body)['values'].find { |email| email['is_primary'] } + ri.merge!('email' => email['email']) if email + ri end end end From bbd1ed443e67073a94dd2a12fa9a52ac84b9327a Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 13:20:33 -0500 Subject: [PATCH 14/16] Remove unused gems --- Gemfile | 9 --------- Guardfile | 10 ---------- 2 files changed, 19 deletions(-) delete mode 100644 Guardfile diff --git a/Gemfile b/Gemfile index 5988a5e..851fabc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,2 @@ source 'https://rubygems.org' - gemspec - -group :development, :test do - gem 'guard' - gem 'guard-rspec' - gem 'guard-bundler' - gem 'rb-fsevent' - gem 'growl' -end diff --git a/Guardfile b/Guardfile deleted file mode 100644 index 1c1cded..0000000 --- a/Guardfile +++ /dev/null @@ -1,10 +0,0 @@ -guard 'rspec', :version => 2 do - watch(%r{^spec/.+_spec\.rb$}) - watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } - watch('spec/spec_helper.rb') { "spec" } -end - -guard 'bundler' do - watch('Gemfile') - watch('omniauth-github.gemspec') -end From 71958b8649aa4fed2804dd5b6e7a3f18ac89670c Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 13:25:37 -0500 Subject: [PATCH 15/16] Update to OmniAuth 2.0 and OAuth2 --- lib/omniauth/strategies/bitbucket.rb | 13 ++++++++----- omniauth-bitbucket.gemspec | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb index 6127188..3bce1e3 100644 --- a/lib/omniauth/strategies/bitbucket.rb +++ b/lib/omniauth/strategies/bitbucket.rb @@ -1,16 +1,15 @@ -require 'omniauth-oauth' +require 'omniauth-oauth2' require 'multi_json' module OmniAuth module Strategies - class Bitbucket < OmniAuth::Strategies::OAuth + class Bitbucket < OmniAuth::Strategies::OAuth2 # This is where you pass the options you would pass when # initializing your consumer from the OAuth gem. option :client_options, { :site => 'https://bitbucket.org', - :request_token_path => '/api/1.0/oauth/request_token', - :authorize_path => '/api/1.0/oauth/authenticate', - :access_token_path => '/api/1.0/oauth/access_token' + :authorize_url => 'https://bitbucket.org/site/oauth2/authorize', + :token_url => 'https://bitbucket.org/site/oauth2/access_token' } # These are called after authentication has succeeded. If @@ -28,6 +27,10 @@ class Bitbucket < OmniAuth::Strategies::OAuth } end + def callback_url + full_host + script_name + callback_path + end + def raw_info @raw_info ||= begin ri = MultiJson.decode(access_token.get('/api/2.0/user').body) diff --git a/omniauth-bitbucket.gemspec b/omniauth-bitbucket.gemspec index 6630e58..7247ff3 100644 --- a/omniauth-bitbucket.gemspec +++ b/omniauth-bitbucket.gemspec @@ -17,8 +17,8 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] # specify any dependencies here; for example: - s.add_dependency 'omniauth', '~> 1.1' - s.add_dependency 'omniauth-oauth', '~> 1.0' + s.add_dependency 'omniauth', '~> 2.0' + s.add_dependency 'omniauth-oauth2', '~> 1.7' s.add_dependency 'multi_json', '>= 1.15.0', '< 2.0' s.add_development_dependency 'rspec', '~> 3' From 687d5711933c3a313fa12778e37b507ff39806ce Mon Sep 17 00:00:00 2001 From: Joe Siewert Date: Tue, 21 May 2024 13:30:24 -0500 Subject: [PATCH 16/16] Bump gem version to 0.0.2 --- lib/omniauth-bitbucket/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/omniauth-bitbucket/version.rb b/lib/omniauth-bitbucket/version.rb index 9ca294a..83a1360 100644 --- a/lib/omniauth-bitbucket/version.rb +++ b/lib/omniauth-bitbucket/version.rb @@ -1,5 +1,5 @@ module Omniauth module Bitbucket - VERSION = "0.0.1" + VERSION = '0.0.2' end end