Skip to content

Commit 0ed5d2b

Browse files
committed
Actually read roles from config instead of hard coding
1 parent 7347ca3 commit 0ed5d2b

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

api/context.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ func getClaims(ctx context.Context) *GatewayClaims {
5454
return token.Claims.(*GatewayClaims)
5555
}
5656

57-
// TODO: actually get this from the config rather than hardcoding
58-
func getRoles(ctx context.Context) []Role {
59-
return []Role{Role{Name: "admin"}, Role{Name: "cms"}}
60-
}
61-
6257
func withRequestID(ctx context.Context, id string) context.Context {
6358
return context.WithValue(ctx, requestIDKey, id)
6459
}

api/github.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (gh *GitHubGateway) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9191
func (gh *GitHubGateway) authenticate(w http.ResponseWriter, r *http.Request) error {
9292
ctx := r.Context()
9393
claims := getClaims(ctx)
94-
adminRoles := getRoles(ctx)
94+
config := getConfig(ctx)
9595

9696
if claims == nil {
9797
return errors.New("Access to endpoint not allowed: no claims found in Bearer token")
@@ -101,7 +101,7 @@ func (gh *GitHubGateway) authenticate(w http.ResponseWriter, r *http.Request) er
101101
return errors.New("Access to endpoint not allowed: this part of GitHub's API has been restricted")
102102
}
103103

104-
if len(adminRoles) == 0 {
104+
if len(config.Roles) == 0 {
105105
return nil
106106
}
107107

@@ -110,8 +110,8 @@ func (gh *GitHubGateway) authenticate(w http.ResponseWriter, r *http.Request) er
110110
roleStrings, _ := roles.([]interface{})
111111
for _, data := range roleStrings {
112112
role, _ := data.(string)
113-
for _, adminRole := range adminRoles {
114-
if role == adminRole.Name {
113+
for _, adminRole := range config.Roles {
114+
if role == adminRole {
115115
return nil
116116
}
117117
}

0 commit comments

Comments
 (0)