Skip to content

Commit 366bc5c

Browse files
committed
feat: make allow_access behave closer to htpasswd default auth plugin
Fixes #58
1 parent 250b66a commit 366bc5c

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,37 @@ yarn publish --registry http://localhost:4873
9393

9494
Access and publish access rights depend on the mode used.
9595

96-
### Normal Mode
96+
verdaccio-gitlab access control will only be applied to package sections that
97+
are marked with `gitlab: true` as in the configuration sample above. If you
98+
wish to disable gitlab authentication to any package config, just remove the
99+
element from the config.
97100

98-
In the default mode, packages are available:
101+
### Normal Mode (default)
99102

100-
- *access* is allowed depending on verdaccio `package` configuration
101-
directives (unauthenticated / authenticated)
102-
- *publish* is allowed if the package name matches the logged in user
103-
id, or if the package name / scope of the package matches one of the
104-
user groups and the user has `auth.gitlab.publish` access rights on
103+
In normal mode, packages are available:
104+
105+
#### Access
106+
107+
*access* is allowed depending on verdaccio `package` configuration
108+
directives:
109+
110+
- authenticated users are able to access all packages
111+
- unauthenticated users will be able to access packages marked with either
112+
`$all` or `$anonymous` access levels
113+
114+
#### Publish
115+
116+
*publish* is allowed if the package name matches the logged in user
117+
id, or if the package name or scope of the package matches one of the
118+
user's groups, and the user has `auth.gitlab.publish` access rights on
105119
the group
106120

107121
For instance, assuming the following configuration:
108122

109123
- `auth.gitlab.publish` = `$maintainer`
110124
- the gitlab user `sample_user` has access to group `group1` as
111-
`$maintainer` and `group2` as `$reporter`
112-
- then this user could publish any of the npm packages:
125+
`$maintainer` and `group2` as `$reporter` in gitlab
126+
- then this user would be able to publish any of the npm packages in verdaccio:
113127
- `sample_user`
114128
- any package under `group1/**`
115129
- error if the user tries to publish any package under `group2/**`

src/gitlab.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ const ACCESS_LEVEL_MAPPING = {
3939
$owner: 50
4040
};
4141

42+
// List of verdaccio builtin levels that map to anonymous access
43+
const BUILTIN_ACCESS_LEVEL_ANONYMOUS = [ '$anonymous', '$all' ];
44+
45+
// Level to apply on 'allow_access' calls when a package definition does not define one
46+
const DEFAULT_ALLOW_ACCESS_LEVEL = [ '$all' ];
47+
48+
4249
export default class VerdaccioGitLab implements IPluginAuth {
4350
options: PluginOptions;
4451
config: VerdaccioGitlabConfig;
@@ -140,15 +147,19 @@ export default class VerdaccioGitLab implements IPluginAuth {
140147
allow_access(user: RemoteUser, _package: VerdaccioGitlabPackageAccess, cb: Callback) {
141148
if (!_package.gitlab) return cb(null, false);
142149

143-
if ((_package.access || []).includes('$authenticated') && user.name !== undefined) {
144-
this.logger.debug(`[gitlab] allow user: ${user.name} access to package: ${_package.name}`);
145-
return cb(null, false);
146-
} else if ((_package.access || []).includes('$all')) {
147-
this.logger.debug(`[gitlab] allow unauthenticated access to package: ${_package.name}`);
148-
return cb(null, false);
149-
} else {
150-
this.logger.debug(`[gitlab] deny user: ${user.name || '<empty>'} access to package: ${_package.name}`);
151-
return cb(httperror[401]('access denied, user not authenticated in gitlab and unauthenticated package access disabled'));
150+
const packageAccess = (_package.access && _package.access.length > 0) ? _package.access : DEFAULT_ALLOW_ACCESS_LEVEL;
151+
152+
if (user.name !== undefined) { // successfully authenticated
153+
this.logger.debug(`[gitlab] allow user: ${user.name} authenticated access to package: ${_package.name}`);
154+
return cb(null, true);
155+
} else { // unauthenticated
156+
if (BUILTIN_ACCESS_LEVEL_ANONYMOUS.some(level => packageAccess.includes(level))) {
157+
this.logger.debug(`[gitlab] allow anonymous access to package: ${_package.name}`);
158+
return cb(null, true);
159+
} else {
160+
this.logger.debug(`[gitlab] deny access to package: ${_package.name}`);
161+
return cb(httperror[401]('access denied, user not authenticated and anonymous access disabled'));
162+
}
152163
}
153164
}
154165

@@ -175,7 +186,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
175186
if (packagePermit || packageScopePermit) {
176187
const perm = packagePermit ? 'package-name' : 'package-scope';
177188
this.logger.debug(`[gitlab] user: ${user.name || ''} allowed to publish package: ${_package.name} based on ${perm}`);
178-
return cb(null, false);
189+
return cb(null, true);
179190
} else {
180191
this.logger.debug(`[gitlab] user: ${user.name || ''} denied from publishing package: ${_package.name}`);
181192
const missingPerm = _package.name.indexOf('@') === 0 ? 'package-scope' : 'package-name';

test/unit/gitlab.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
6767
const cb: Callback = (err, data) => {
6868
expect(err).toBeFalsy();
6969
// false allows the plugin chain to continue
70-
expect(data).toBe(false);
70+
expect(data).toBe(true);
7171
done();
7272
};
7373

@@ -85,7 +85,7 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
8585
const cb: Callback = (err, data) => {
8686
expect(err).toBeFalsy();
8787
// false allows the plugin chain to continue
88-
expect(data).toBe(false);
88+
expect(data).toBe(true);
8989
done();
9090
};
9191

@@ -123,7 +123,7 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
123123

124124
const cb: Callback = (err, data) => {
125125
expect(err).toBeFalsy();
126-
expect(data).toBe(false);
126+
expect(data).toBe(true);
127127
done();
128128
};
129129

@@ -139,7 +139,7 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
139139

140140
const cb: Callback = (err, data) => {
141141
expect(err).toBeFalsy();
142-
expect(data).toBe(false);
142+
expect(data).toBe(true);
143143
done();
144144
};
145145

0 commit comments

Comments
 (0)