Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bitbucket to use the oAuth2 endpoint #174

Merged
merged 4 commits into from Jan 6, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -37,17 +37,16 @@ credentials.profile = {

- `scope`: not applicable
- `config`: not applicable
- `temporary`: https://bitbucket.org/api/1.0/oauth/request_token
- `auth`: https://bitbucket.org/api/1.0/oauth/authenticate
- `token`: https://bitbucket.org/api/1.0/oauth/access_token
- `auth`: https://bitbucket.org/site/oauth2/authorize
- `token`: https://bitbucket.org/site/oauth2/access_token

The default profile response will look like this:

```javascript
credentials.profile = {};
credentials.profile.id = profile.user.username;
credentials.profile.username = profile.user.username;
credentials.profile.displayName = profile.user.first_name + (profile.user.last_name ? ' ' + profile.user.last_name : '');
credentials.profile.id = profile.username;
credentials.profile.username = profile.username;
credentials.profile.displayName = profile.display_name;
credentials.profile.raw = profile;
```

@@ -303,8 +303,7 @@ exports.Client = internals.Client = function (options) {
temporary: internals.Client.baseUri(options.provider.temporary),
token: internals.Client.baseUri(options.provider.token),
clientId: options.clientId,
clientSecret: internals.encode(options.clientSecret || '') + '&',
version: options.provider.version || ''
clientSecret: internals.encode(options.clientSecret || '') + '&'
};
};

@@ -358,10 +357,6 @@ internals.Client.prototype._request = function (method, uri, params, oauth, opti
oauth.oauth_signature_method = 'HMAC-SHA1';
oauth.oauth_signature = this.signature(method, uri, params, oauth, options.secret);

if (this.settings.version) {
oauth.oauth_version = this.settings.version;
}

// Calculate OAuth header

const requestOptions = {
@@ -3,19 +3,17 @@
exports = module.exports = function () {

return {
protocol: 'oauth',
temporary: 'https://bitbucket.org/api/1.0/oauth/request_token',
auth: 'https://bitbucket.org/api/1.0/oauth/authenticate',
token: 'https://bitbucket.org/api/1.0/oauth/access_token',
version: '1.0',
protocol: 'oauth2',
auth: 'https://bitbucket.org/site/oauth2/authorize',
token: 'https://bitbucket.org/site/oauth2/access_token',
profile: function (credentials, params, get, callback) {

get('https://bitbucket.org/api/1.0/user', null, (profile) => {
get('https://api.bitbucket.org/2.0/user', null, (profile) => {

credentials.profile = {};
credentials.profile.id = profile.user.username;
credentials.profile.username = profile.user.username;
credentials.profile.displayName = profile.user.first_name + (profile.user.last_name ? ' ' + profile.user.last_name : '');
credentials.profile.id = profile.username;
credentials.profile.username = profile.username;
credentials.profile.displayName = profile.display_name;
credentials.profile.raw = profile;
return callback();
});
@@ -22,7 +22,7 @@ describe('bitbucket', () => {

it('authenticates with mock', { parallel: false }, (done) => {

const mock = new Mock.V1();
const mock = new Mock.V2();
mock.start((provider) => {

const server = new Hapi.Server();
@@ -34,13 +34,11 @@ describe('bitbucket', () => {
const custom = Bell.providers.bitbucket();
Hoek.merge(custom, provider);

Mock.override('https://bitbucket.org/api/1.0/user', {
Mock.override('https://api.bitbucket.org/2.0/user', {
repositories: [{}],
user: {
first_name: 'Steve',
last_name: 'Stevens',
username: 'steve_stevens'
}
id: 'steve',
username: 'steve',
display_name: 'steve'
});

server.auth.strategy('custom', 'bell', {
@@ -73,101 +71,19 @@ describe('bitbucket', () => {
Mock.clear();
expect(response.result).to.deep.equal({
provider: 'custom',
token: 'final',
secret: 'secret',
query: {},
profile: {
id: 'steve_stevens',
username: 'steve_stevens',
displayName: 'Steve Stevens',
raw: {
repositories: [{}],
user: {
first_name: 'Steve',
last_name: 'Stevens',
username: 'steve_stevens'
}
}
}
});

mock.stop(done);
});
});
});
});
});
});

it('authenticates with mock (last_name is empty)', { parallel: false }, (done) => {

const mock = new Mock.V1();
mock.start((provider) => {


const server = new Hapi.Server();
server.connection({ host: 'localhost', port: 80 });
server.register(Bell, (err) => {

expect(err).to.not.exist();

const custom = Bell.providers.bitbucket();
Hoek.merge(custom, provider);

Mock.override('https://bitbucket.org/api/1.0/user', {
// source: https://confluence.atlassian.com/display/BITBUCKET/user+Endpoint
repositories: [{}],
user: {
first_name: 'Steve',
last_name: '',
username: 'steve_stevens'
}
});

server.auth.strategy('custom', 'bell', {
password: 'password',
isSecure: false,
clientId: 'twitter',
clientSecret: 'secret',
provider: custom
});

server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, reply) {

reply(request.auth.credentials);
}
}
});

server.inject('/login', (res) => {

const cookie = res.headers['set-cookie'][0].split(';')[0] + ';';
mock.server.inject(res.headers.location, (mockRes) => {

server.inject({ url: mockRes.headers.location, headers: { cookie: cookie } }, (response) => {

Mock.clear();
expect(response.result).to.deep.equal({
provider: 'custom',
token: 'final',
secret: 'secret',
token: '456',
refreshToken: undefined,
expiresIn: 3600,
query: {},
profile: {
id: 'steve_stevens',
username: 'steve_stevens',
displayName: 'Steve',
id: 'steve',
username: 'steve',
displayName: 'steve',
raw: {
repositories: [{}],
user: {
first_name: 'Steve',
last_name: '',
username: 'steve_stevens'
}
id: 'steve',
username: 'steve',
display_name: 'steve'
}
}
});
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.