Skip to content

Commit 250b66a

Browse files
Merge pull request #56 from bufferoverflow/refactor/consistent-plugin-chain
refactor: consistent use of plugin chain
2 parents 17b764c + 14b8b18 commit 250b66a

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

src/gitlab.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
9494
token: password
9595
});
9696

97-
const pUsers = GitlabAPI.Users.current();
98-
return pUsers.then(response => {
97+
GitlabAPI.Users.current().then(response => {
9998
if (user !== response.username) {
10099
return cb(httperror[401]('wrong gitlab username'));
101100
}
@@ -111,31 +110,25 @@ export default class VerdaccioGitLab implements IPluginAuth {
111110
//
112111
// In legacy mode, the groups are:
113112
// - for access, depending on the package settings in verdaccio
114-
// - for publish, the logged in user id and all the groups they can reach as `$owner`
113+
// - for publish, the logged in user id and all the groups they can reach as fixed `$auth.gitlab.publish` = `$owner`
115114
const gitlabPublishQueryParams = this.config.legacy_mode ? { owned: true } : { min_access_level: publishLevelId };
116-
const pPublishGroups = GitlabAPI.Groups.all(gitlabPublishQueryParams).then(groups => {
117-
this.logger.trace('[gitlab] querying gitlab user groups with params:', gitlabPublishQueryParams);
118-
this._addGroupsToArray(groups, userGroups.publish);
119-
}).catch(error => {
120-
this.logger.error(`[gitlab] user: ${user} error querying publish groups: ${error}`);
121-
return cb(httperror[500]('error querying gitlab'));
122-
});
115+
this.logger.trace('[gitlab] querying gitlab user groups with params:', gitlabPublishQueryParams);
123116

124-
const pGroups = Promise.all([pPublishGroups]);
125-
return pGroups.then(() => {
117+
GitlabAPI.Groups.all(gitlabPublishQueryParams).then(groups => {
118+
this._addGroupsToArray(groups, userGroups.publish);
126119
this._setCachedUserGroups(user, password, userGroups);
120+
127121
this.logger.info(`[gitlab] user: ${user} successfully authenticated`);
128122
this.logger.debug(`[gitlab] user: ${user}, with groups:`, userGroups);
123+
129124
return cb(null, userGroups.publish);
130125
}).catch(error => {
131-
this.logger.error(`[gitlab] error authenticating: ${error}`);
132-
return cb(httperror[500]('error authenticating'));
126+
this.logger.error(`[gitlab] user: ${user} error querying gitlab publish groups: ${error}`);
127+
return cb(httperror[401]('error authenticating user'));
133128
});
134129
}).catch(error => {
135-
this.logger.info(`[gitlab] user: ${user} error authenticating: ${error.message || {}}`);
136-
if (error) {
137-
return cb(httperror[401]('personal access token invalid'));
138-
}
130+
this.logger.error(`[gitlab] user: ${user} error querying gitlab user data: ${error.message || {}}`);
131+
return cb(httperror[401]('error authenticating user'));
139132
});
140133
}
141134

@@ -145,29 +138,31 @@ export default class VerdaccioGitLab implements IPluginAuth {
145138
}
146139

147140
allow_access(user: RemoteUser, _package: VerdaccioGitlabPackageAccess, cb: Callback) {
148-
if (!_package.gitlab) return cb();
141+
if (!_package.gitlab) return cb(null, false);
149142

150143
if ((_package.access || []).includes('$authenticated') && user.name !== undefined) {
151144
this.logger.debug(`[gitlab] allow user: ${user.name} access to package: ${_package.name}`);
152-
return cb(null, true);
145+
return cb(null, false);
153146
} else if ((_package.access || []).includes('$all')) {
154147
this.logger.debug(`[gitlab] allow unauthenticated access to package: ${_package.name}`);
155-
return cb(null, true);
148+
return cb(null, false);
156149
} else {
157150
this.logger.debug(`[gitlab] deny user: ${user.name || '<empty>'} access to package: ${_package.name}`);
158151
return cb(httperror[401]('access denied, user not authenticated in gitlab and unauthenticated package access disabled'));
159152
}
160153
}
161154

162155
allow_publish(user: RemoteUser, _package: VerdaccioGitlabPackageAccess, cb: Callback) {
163-
if (!_package.gitlab) return cb();
156+
if (!_package.gitlab) return cb(null, false);
157+
164158
let packageScopePermit = false;
165159
let packagePermit = false;
166160
// Only allow to publish packages when:
167161
// - the package has exactly the same name as one of the user groups, or
168162
// - the package scope is the same as one of the user groups
169163
for (let real_group of user.real_groups) { // jscs:ignore requireCamelCaseOrUpperCaseIdentifiers
170164
this.logger.trace(`[gitlab] publish: checking group: ${real_group} for user: ${user.name || ''} and package: ${_package.name}`);
165+
171166
if (real_group === _package.name) {
172167
packagePermit = true;
173168
break;

test/unit/gitlab.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
6666

6767
const cb: Callback = (err, data) => {
6868
expect(err).toBeFalsy();
69-
expect(data).toBe(true);
69+
// false allows the plugin chain to continue
70+
expect(data).toBe(false);
7071
done();
7172
};
7273

@@ -83,7 +84,8 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
8384

8485
const cb: Callback = (err, data) => {
8586
expect(err).toBeFalsy();
86-
expect(data).toBe(true);
87+
// false allows the plugin chain to continue
88+
expect(data).toBe(false);
8789
done();
8890
};
8991

0 commit comments

Comments
 (0)