Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.gem
.DS_Store
.bundle
Gemfile.lock
coverage
pkg/*
.DS_Store
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.5
11 changes: 1 addition & 10 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
source "https://rubygems.org"

source 'https://rubygems.org'
gemspec

group :development, :test do
gem 'guard'
gem 'guard-rspec'
gem 'guard-bundler'
gem 'rb-fsevent'
gem 'growl'
end
10 changes: 0 additions & 10 deletions Guardfile

This file was deleted.

2 changes: 1 addition & 1 deletion lib/omniauth-bitbucket/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Omniauth
module Bitbucket
VERSION = "0.0.1"
VERSION = '0.0.2'
end
end
25 changes: 15 additions & 10 deletions lib/omniauth/strategies/bitbucket.rb
Original file line number Diff line number Diff line change
@@ -1,15 +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
Expand All @@ -21,17 +21,22 @@ 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 callback_url
full_host + script_name + callback_path
end

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
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
Expand Down
10 changes: 5 additions & 5 deletions omniauth-bitbucket.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Gem::Specification.new do |s|
s.version = Omniauth::Bitbucket::VERSION
s.authors = ["Dingding Ye"]
s.email = ["[email protected]"]
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.}

Expand All @@ -17,11 +17,11 @@ 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 'multi_json', '~> 1.7'
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', '~> 2.7'
s.add_development_dependency 'rspec', '~> 3'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'webmock'
Expand Down
29 changes: 27 additions & 2 deletions spec/omniauth/strategies/bitbucket_spec.rb
Original file line number Diff line number Diff line change
@@ -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
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
expect(strategy.raw_info['email']).to eq(email)
end

it "should prefer primary" do
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
expect(strategy.raw_info['email']).to eq(email)
end

it "should ignore non-existing" do
allow(strategy).to receive_message_chain(:access_token, :get) do |url|
body = url =~ /emails$/ ? %{{"pagelen": 0, "values": []}} : '{"user": {}}'
double body: body
end
expect(strategy.raw_info['email']).to eq(nil)
end
end
end