@@ -2,7 +2,6 @@ package api
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"net/http"
7
6
8
7
"github.com/netlify/git-gateway/conf"
@@ -15,7 +14,17 @@ type Auth struct {
15
14
version string
16
15
}
17
16
18
- // authenicate checks incoming requests for tokens presented using the Authorization header
17
+ // check both authentication and authorization
18
+ func (a * Auth ) accessControl (w http.ResponseWriter , r * http.Request ) (context.Context , error ) {
19
+ _ , err := a .authenticate (w , r )
20
+ if err != nil {
21
+ return nil , err
22
+ }
23
+
24
+ return a .authorize (w , r )
25
+ }
26
+
27
+ // authenticate checks incoming requests for tokens presented using the Authorization header
19
28
func (a * Auth ) authenticate (w http.ResponseWriter , r * http.Request ) (context.Context , error ) {
20
29
logrus .Info ("Getting auth token" )
21
30
token , err := a .extractBearerToken (w , r )
@@ -33,13 +42,9 @@ func (a *Auth) authorize(w http.ResponseWriter, r *http.Request) (context.Contex
33
42
claims := getClaims (ctx )
34
43
config := getConfig (ctx )
35
44
36
- logrus .Infof ("authenticate context : %v+" , ctx )
45
+ logrus .Infof ("authenticate url : %v+" , r . URL )
37
46
if claims == nil {
38
- return nil , errors .New ("Access to endpoint not allowed: no claims found in Bearer token" )
39
- }
40
-
41
- if ! allowedRegexp .MatchString (r .URL .Path ) {
42
- return nil , errors .New ("Access to endpoint not allowed: this part of GitHub's API has been restricted" )
47
+ return nil , unauthorizedError ("Access to endpoint not allowed: no claims found in Bearer token" )
43
48
}
44
49
45
50
if len (config .Roles ) == 0 {
@@ -59,7 +64,7 @@ func (a *Auth) authorize(w http.ResponseWriter, r *http.Request) (context.Contex
59
64
}
60
65
}
61
66
62
- return nil , errors . New ("Access to endpoint not allowed: your role doesn't allow access" )
67
+ return nil , unauthorizedError ("Access to endpoint not allowed: your role doesn't allow access" )
63
68
}
64
69
65
70
func NewAuthWithVersion (ctx context.Context , globalConfig * conf.GlobalConfiguration , version string ) * Auth {
0 commit comments