1
1
// Copyright 2018 Roger Meier <[email protected] >
2
2
// SPDX-License-Identifier: MIT
3
- // @flow
4
3
5
- import type { Callback , IPluginAuth , Logger , PluginOptions , RemoteUser , PackageAccess } from '@verdaccio/types' ;
6
- import type { UserDataGroups } from './authcache' ;
4
+ import { Callback , IPluginAuth , Logger , PluginOptions , RemoteUser , PackageAccess } from '@verdaccio/types' ;
5
+ import { getInternalError , getUnauthorized , getForbidden } from '@verdaccio/commons-api' ;
6
+ import { UserDataGroups } from './authcache' ;
7
7
8
8
import Gitlab from 'gitlab' ;
9
9
import { AuthCache , UserData } from './authcache' ;
10
- import httperror from 'http-errors' ;
11
10
12
11
export type VerdaccioGitlabAccessLevel =
13
12
'$guest' |
@@ -26,10 +25,10 @@ export type VerdaccioGitlabConfig = {
26
25
publish ?: VerdaccioGitlabAccessLevel
27
26
} ;
28
27
29
- export type VerdaccioGitlabPackageAccess = PackageAccess & {
30
- name : string ,
28
+ export interface VerdaccioGitlabPackageAccess extends PackageAccess {
29
+ name ? : string ,
31
30
gitlab ?: boolean
32
- }
31
+ } ;
33
32
34
33
const ACCESS_LEVEL_MAPPING = {
35
34
$guest : 10 ,
@@ -46,14 +45,19 @@ const BUILTIN_ACCESS_LEVEL_ANONYMOUS = [ '$anonymous', '$all' ];
46
45
const DEFAULT_ALLOW_ACCESS_LEVEL = [ '$all' ] ;
47
46
48
47
49
- export default class VerdaccioGitLab implements IPluginAuth {
50
- options : PluginOptions ;
48
+ export interface VerdaccioGitLabPlugin extends IPluginAuth < VerdaccioGitlabConfig > {
49
+ authCache : AuthCache ;
50
+ }
51
+
52
+
53
+ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
54
+ options : PluginOptions < VerdaccioGitlabConfig > ;
51
55
config : VerdaccioGitlabConfig ;
52
56
authCache : AuthCache ;
53
57
logger : Logger ;
54
58
publishLevel : VerdaccioGitlabAccessLevel ;
55
59
56
- constructor ( config : VerdaccioGitlabConfig , options : PluginOptions ) {
60
+ constructor ( config : VerdaccioGitlabConfig , options : PluginOptions < VerdaccioGitlabConfig > ) {
57
61
this . logger = options . logger ;
58
62
this . config = config ;
59
63
this . options = options ;
@@ -103,7 +107,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
103
107
104
108
GitlabAPI . Users . current ( ) . then ( response => {
105
109
if ( user !== response . username ) {
106
- return cb ( httperror [ 401 ] ( 'wrong gitlab username' ) ) ;
110
+ return cb ( getForbidden ( 'wrong gitlab username' ) ) ;
107
111
}
108
112
109
113
const publishLevelId = ACCESS_LEVEL_MAPPING [ this . publishLevel ] ;
@@ -136,12 +140,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
136
140
return cb ( null , realGroups ) ;
137
141
} ) . catch ( error => {
138
142
this . logger . error ( `[gitlab] user: ${ user } error querying gitlab: ${ error } ` ) ;
139
- return cb ( httperror [ 401 ] ( 'error authenticating user' ) ) ;
143
+ return cb ( getUnauthorized ( 'error authenticating user' ) ) ;
140
144
} ) ;
141
145
142
146
} ) . catch ( error => {
143
147
this . logger . error ( `[gitlab] user: ${ user } error querying gitlab user data: ${ error . message || { } } ` ) ;
144
- return cb ( httperror [ 401 ] ( 'error authenticating user' ) ) ;
148
+ return cb ( getUnauthorized ( 'error authenticating user' ) ) ;
145
149
} ) ;
146
150
}
147
151
@@ -150,12 +154,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
150
154
return cb ( null , true ) ;
151
155
}
152
156
153
- changePassword ( user : string , password : string , newPassword : string , cb : verdaccio$ Callback) {
157
+ changePassword ( user : string , password : string , newPassword : string , cb : Callback ) {
154
158
this . logger . trace ( `[gitlab] changePassword called for user: ${ user } ` ) ;
155
- return cb ( httperror [ 501 ] ( 'You are using verdaccio-gitlab integration. Please change your password in gitlab' ) ) ;
159
+ return cb ( getInternalError ( 'You are using verdaccio-gitlab integration. Please change your password in gitlab' ) ) ;
156
160
}
157
161
158
- allow_access ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess , cb : Callback ) {
162
+ allow_access ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess & PackageAccess , cb : Callback ) {
159
163
if ( ! _package . gitlab ) return cb ( null , false ) ;
160
164
161
165
const packageAccess = ( _package . access && _package . access . length > 0 ) ? _package . access : DEFAULT_ALLOW_ACCESS_LEVEL ;
@@ -169,12 +173,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
169
173
return cb ( null , true ) ;
170
174
} else {
171
175
this . logger . debug ( `[gitlab] deny access to package: ${ _package . name } ` ) ;
172
- return cb ( httperror [ 401 ] ( 'access denied, user not authenticated and anonymous access disabled' ) ) ;
176
+ return cb ( getForbidden ( 'access denied, user not authenticated and anonymous access disabled' ) ) ;
173
177
}
174
178
}
175
179
}
176
180
177
- allow_publish ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess , cb : Callback ) {
181
+ allow_publish ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess & PackageAccess , cb : Callback ) {
178
182
if ( ! _package . gitlab ) return cb ( null , false ) ;
179
183
180
184
let packageScopePermit = false ;
@@ -185,7 +189,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
185
189
for ( let real_group of user . real_groups ) { // jscs:ignore requireCamelCaseOrUpperCaseIdentifiers
186
190
this . logger . trace ( `[gitlab] publish: checking group: ${ real_group } for user: ${ user . name || '' } and package: ${ _package . name } ` ) ;
187
191
188
- if ( this . _matchGroupWithPackage ( real_group , _package . name ) ) {
192
+ if ( this . _matchGroupWithPackage ( real_group , _package . name as string ) ) {
189
193
packagePermit = true ;
190
194
break ;
191
195
}
@@ -197,8 +201,8 @@ export default class VerdaccioGitLab implements IPluginAuth {
197
201
return cb ( null , true ) ;
198
202
} else {
199
203
this . logger . debug ( `[gitlab] user: ${ user . name || '' } denied from publishing package: ${ _package . name } ` ) ;
200
- const missingPerm = _package . name . indexOf ( '@' ) === 0 ? 'package-scope' : 'package-name' ;
201
- return cb ( httperror [ 403 ] ( `must have required permissions: ${ this . publishLevel || '' } at ${ missingPerm } ` ) ) ;
204
+ const missingPerm = _package . name ?? _package . name . indexOf ( '@' ) === 0 ? 'package-scope' : 'package-name' ;
205
+ return cb ( getForbidden ( `must have required permissions: ${ this . publishLevel || '' } at ${ missingPerm } ` ) ) ;
202
206
}
203
207
}
204
208
@@ -227,7 +231,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
227
231
return false ;
228
232
}
229
233
230
- _getCachedUserGroups ( username : string , password : string ) : ? UserDataGroups {
234
+ _getCachedUserGroups ( username : string , password : string ) : UserDataGroups | null {
231
235
if ( ! this . authCache ) {
232
236
return null ;
233
237
}
0 commit comments