11// Copyright 2018 Roger Meier <roger@bufferoverflow.ch>
22// SPDX-License-Identifier: MIT
3- // @flow
43
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' ;
77
88import Gitlab from 'gitlab' ;
99import { AuthCache , UserData } from './authcache' ;
10- import httperror from 'http-errors' ;
1110
1211export type VerdaccioGitlabAccessLevel =
1312 '$guest' |
@@ -26,10 +25,10 @@ export type VerdaccioGitlabConfig = {
2625 publish ?: VerdaccioGitlabAccessLevel
2726} ;
2827
29- export type VerdaccioGitlabPackageAccess = PackageAccess & {
30- name : string ,
28+ export interface VerdaccioGitlabPackageAccess extends PackageAccess {
29+ name ? : string ,
3130 gitlab ?: boolean
32- }
31+ } ;
3332
3433const ACCESS_LEVEL_MAPPING = {
3534 $guest : 10 ,
@@ -46,14 +45,19 @@ const BUILTIN_ACCESS_LEVEL_ANONYMOUS = [ '$anonymous', '$all' ];
4645const DEFAULT_ALLOW_ACCESS_LEVEL = [ '$all' ] ;
4746
4847
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 > ;
5155 config : VerdaccioGitlabConfig ;
5256 authCache : AuthCache ;
5357 logger : Logger ;
5458 publishLevel : VerdaccioGitlabAccessLevel ;
5559
56- constructor ( config : VerdaccioGitlabConfig , options : PluginOptions ) {
60+ constructor ( config : VerdaccioGitlabConfig , options : PluginOptions < VerdaccioGitlabConfig > ) {
5761 this . logger = options . logger ;
5862 this . config = config ;
5963 this . options = options ;
@@ -103,7 +107,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
103107
104108 GitlabAPI . Users . current ( ) . then ( response => {
105109 if ( user !== response . username ) {
106- return cb ( httperror [ 401 ] ( 'wrong gitlab username' ) ) ;
110+ return cb ( getForbidden ( 'wrong gitlab username' ) ) ;
107111 }
108112
109113 const publishLevelId = ACCESS_LEVEL_MAPPING [ this . publishLevel ] ;
@@ -136,12 +140,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
136140 return cb ( null , realGroups ) ;
137141 } ) . catch ( error => {
138142 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' ) ) ;
140144 } ) ;
141145
142146 } ) . catch ( error => {
143147 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' ) ) ;
145149 } ) ;
146150 }
147151
@@ -150,12 +154,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
150154 return cb ( null , true ) ;
151155 }
152156
153- changePassword ( user : string , password : string , newPassword : string , cb : verdaccio$ Callback) {
157+ changePassword ( user : string , password : string , newPassword : string , cb : Callback ) {
154158 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' ) ) ;
156160 }
157161
158- allow_access ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess , cb : Callback ) {
162+ allow_access ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess & PackageAccess , cb : Callback ) {
159163 if ( ! _package . gitlab ) return cb ( null , false ) ;
160164
161165 const packageAccess = ( _package . access && _package . access . length > 0 ) ? _package . access : DEFAULT_ALLOW_ACCESS_LEVEL ;
@@ -169,12 +173,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
169173 return cb ( null , true ) ;
170174 } else {
171175 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' ) ) ;
173177 }
174178 }
175179 }
176180
177- allow_publish ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess , cb : Callback ) {
181+ allow_publish ( user : RemoteUser , _package : VerdaccioGitlabPackageAccess & PackageAccess , cb : Callback ) {
178182 if ( ! _package . gitlab ) return cb ( null , false ) ;
179183
180184 let packageScopePermit = false ;
@@ -185,7 +189,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
185189 for ( let real_group of user . real_groups ) { // jscs:ignore requireCamelCaseOrUpperCaseIdentifiers
186190 this . logger . trace ( `[gitlab] publish: checking group: ${ real_group } for user: ${ user . name || '' } and package: ${ _package . name } ` ) ;
187191
188- if ( this . _matchGroupWithPackage ( real_group , _package . name ) ) {
192+ if ( this . _matchGroupWithPackage ( real_group , _package . name as string ) ) {
189193 packagePermit = true ;
190194 break ;
191195 }
@@ -197,8 +201,8 @@ export default class VerdaccioGitLab implements IPluginAuth {
197201 return cb ( null , true ) ;
198202 } else {
199203 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 } ` ) ) ;
202206 }
203207 }
204208
@@ -227,7 +231,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
227231 return false ;
228232 }
229233
230- _getCachedUserGroups ( username : string , password : string ) : ? UserDataGroups {
234+ _getCachedUserGroups ( username : string , password : string ) : UserDataGroups | null {
231235 if ( ! this . authCache ) {
232236 return null ;
233237 }
0 commit comments