@@ -39,6 +39,13 @@ const ACCESS_LEVEL_MAPPING = {
39
39
$owner : 50
40
40
} ;
41
41
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
+
42
49
export default class VerdaccioGitLab implements IPluginAuth {
43
50
options : PluginOptions ;
44
51
config : VerdaccioGitlabConfig ;
@@ -140,15 +147,19 @@ export default class VerdaccioGitLab implements IPluginAuth {
140
147
allow_access ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess , cb : Callback ) {
141
148
if ( ! _package . gitlab ) return cb ( null , false ) ;
142
149
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
+ }
152
163
}
153
164
}
154
165
@@ -175,7 +186,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
175
186
if ( packagePermit || packageScopePermit ) {
176
187
const perm = packagePermit ? 'package-name' : 'package-scope' ;
177
188
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 ) ;
179
190
} else {
180
191
this . logger . debug ( `[gitlab] user: ${ user . name || '' } denied from publishing package: ${ _package . name } ` ) ;
181
192
const missingPerm = _package . name . indexOf ( '@' ) === 0 ? 'package-scope' : 'package-name' ;
0 commit comments