@@ -43,35 +43,38 @@ func reqPackageAccess(accessMode perm.AccessMode) func(ctx *context.Context) {
4343 }
4444}
4545
46- // CommonRoutes provide endpoints for most package managers (except containers - see below)
47- // These are mounted on `/api/packages` (not `/api/v1/packages`)
48- func CommonRoutes (ctx gocontext.Context ) * web.Route {
49- r := web .NewRoute ()
50-
51- r .Use (context .PackageContexter (ctx ))
52-
53- authMethods := []auth.Method {
54- & auth.OAuth2 {},
55- & auth.Basic {},
56- & nuget.Auth {},
57- & conan.Auth {},
58- & chef.Auth {},
59- }
46+ func verifyAuth (r * web.Route , authMethods []auth.Method ) {
6047 if setting .Service .EnableReverseProxyAuth {
6148 authMethods = append (authMethods , & auth.ReverseProxy {})
6249 }
63-
6450 authGroup := auth .NewGroup (authMethods ... )
51+
6552 r .Use (func (ctx * context.Context ) {
6653 var err error
6754 ctx .Doer , err = authGroup .Verify (ctx .Req , ctx .Resp , ctx , ctx .Session )
6855 if err != nil {
69- log .Error ("Verify : %v" , err )
56+ log .Error ("Failed to verify user : %v" , err )
7057 ctx .Error (http .StatusUnauthorized , "authGroup.Verify" )
7158 return
7259 }
7360 ctx .IsSigned = ctx .Doer != nil
7461 })
62+ }
63+
64+ // CommonRoutes provide endpoints for most package managers (except containers - see below)
65+ // These are mounted on `/api/packages` (not `/api/v1/packages`)
66+ func CommonRoutes (ctx gocontext.Context ) * web.Route {
67+ r := web .NewRoute ()
68+
69+ r .Use (context .PackageContexter (ctx ))
70+
71+ verifyAuth (r , []auth.Method {
72+ & auth.OAuth2 {},
73+ & auth.Basic {},
74+ & nuget.Auth {},
75+ & conan.Auth {},
76+ & chef.Auth {},
77+ })
7578
7679 r .Group ("/{username}" , func () {
7780 r .Group ("/cargo" , func () {
@@ -401,24 +404,9 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route {
401404
402405 r .Use (context .PackageContexter (ctx ))
403406
404- authMethods := []auth.Method {
407+ verifyAuth ( r , []auth.Method {
405408 & auth.Basic {},
406409 & container.Auth {},
407- }
408- if setting .Service .EnableReverseProxyAuth {
409- authMethods = append (authMethods , & auth.ReverseProxy {})
410- }
411-
412- authGroup := auth .NewGroup (authMethods ... )
413- r .Use (func (ctx * context.Context ) {
414- var err error
415- ctx .Doer , err = authGroup .Verify (ctx .Req , ctx .Resp , ctx , ctx .Session )
416- if err != nil {
417- log .Error ("Failed to verify user: %v" , err )
418- ctx .Error (http .StatusUnauthorized , "Verify" )
419- return
420- }
421- ctx .IsSigned = ctx .Doer != nil
422410 })
423411
424412 r .Get ("" , container .ReqContainerAccess , container .DetermineSupport )
0 commit comments