Skip to content

Commit

Permalink
- removed bitbucket-api module
Browse files Browse the repository at this point in the history
- removed user call before privilages call (was not needed at all)

V1.0.4
  • Loading branch information
idangozlan committed Jun 26, 2017
1 parent dcc1a7d commit acbe068
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 37 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepublish": "npm run build"
},
"main": "lib/index.js",
"version": "1.0.3",
"version": "1.0.4",
"description": "Verdaccio module to authenticate users via Bitbucket",
"keywords": [
"sinopia",
Expand All @@ -34,7 +34,7 @@
"node": "*"
},
"dependencies": {
"bitbucket-api": "^0.1.0"
"axios": "^0.16.2"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down
90 changes: 55 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import Crypto from 'crypto';
import Bitbucket from 'bitbucket-api';
import axios from 'axios';

class Bitbucket2 {
constructor(username, password) {
this.apiVersion = '1.0';
this.apiUrl = `https://api.bitbucket.org/${this.apiVersion}`;
this.username = username;
this.password = password;
}

getUser() {
// currently not in use, maybe in the future it will be.
const { username, password, apiUrl } = this;
return axios({
method: 'get',
url: `${apiUrl}/user`,
auth: {
username,
password,
},
}).then(response => response.data);
}

getPrivileges() {
const { username, password, apiUrl } = this;
return axios({
method: 'get',
url: `${apiUrl}/user/privileges/`,
auth: {
username,
password,
},
}).then(response => response.data);
}
}

/**
* Default cache time-to-live in seconds
Expand Down Expand Up @@ -184,13 +218,8 @@ const logError = (logger, err, username) => {
* @access public
*/
Auth.prototype.authenticate = function authenticate(username, password, done) {
const credentials = {
username: decodeUsernameToEmail(username),
password,
};

// make sure we keep memory low
// run in background
// make sure we keep memory low
// run in background
setTimeout(cleanup);

const cache = getCache(username, password);
Expand All @@ -199,38 +228,29 @@ Auth.prototype.authenticate = function authenticate(username, password, done) {
return done(null, cache.teams);
}

this.bitbucket = Bitbucket.createClient(credentials);

return this.bitbucket.user().get((err) => {
if (err) {
logError(this.logger, err, username);
return done(err, false);
}

return this.bitbucket.user().privileges((err2, privileges) => {
if (err2) {
logError(this.logger, err2, username);
return done(err2, false);
}
this.bitbucket = new Bitbucket2(decodeUsernameToEmail(username), password);

const teams = Object.keys(privileges.teams)
.filter((team) => {
if (this.allow[team] === undefined) {
return false;
}
return this.bitbucket.getPrivileges().then((privileges) => {
const teams = Object.keys(privileges.teams)
.filter((team) => {
if (this.allow[team] === undefined) {
return false;
}

if (!this.allow[team].length) {
return true;
}
if (!this.allow[team].length) {
return true;
}

return this.allow[team].includes(privileges.teams[team]);
}, this);
return this.allow[team].includes(privileges.teams[team]);
}, this);

cache.teams = teams;
cache.expires = new Date(new Date().getTime() + this.ttl);
cache.teams = teams;
cache.expires = new Date(new Date().getTime() + this.ttl);

return done(null, teams);
});
return done(null, teams);
}).catch((err) => {
logError(this.logger, err, username);
return done(err, false);
});
};

Expand Down

0 comments on commit acbe068

Please sign in to comment.