Skip to content

Commit b5cea9d

Browse files
committed
Adding a seam for read/write auth
1 parent 5964342 commit b5cea9d

7 files changed

+34
-5
lines changed

.example.env

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
#!/bin/bash
2-
3-
export DATABASE_URL="postgres://${PGUSER:-artifacts}:${PGPASSWORD:-dogs}@localhost:5432/artifacts?sslmode=disable"
4-
export PORT='9839'
1+
DATABASE_URL='postgres://artifacts:dogs@localhost:5432/artifacts?sslmode=disable'
2+
PORT='9839'

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ _testmain.go
2121

2222
*.exe
2323
*.test
24+
/gin-bin
2425

2526
*coverage.*
2627
*.out

server/error_handler.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ func (srv *Server) serveError(err error, w http.ResponseWriter, r *http.Request)
1414

1515
w.WriteHeader(http.StatusInternalServerError)
1616
w.Header().Set("Content-Type", "application/vnd.api+json")
17-
fmt.Fprintf(w, fmt.Sprintf("{\"error\":%q}", err.Error()))
17+
fmt.Fprintf(w, `{"error":%q}`, err.Error())
1818
return http.StatusInternalServerError
1919
}
20+
21+
func (srv *Server) serveUnauthorized(w http.ResponseWriter, r *http.Request) int {
22+
srv.log.Warn("serving 401 response")
23+
24+
w.WriteHeader(http.StatusUnauthorized)
25+
w.Header().Set("Content-Type", "application/vnd.api+json")
26+
w.Header().Set("WWW-Authenticate", "token")
27+
fmt.Fprintf(w, `{"error":"unauthorized"}`)
28+
return http.StatusUnauthorized
29+
}

server/get_path_handler.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
)
1111

1212
func (srv *Server) getPathHandler(w http.ResponseWriter, r *http.Request, vars map[string]string) int {
13+
if !srv.canRead(r, vars) {
14+
return srv.serveUnauthorized(w, r)
15+
}
16+
1317
srv.log.WithFields(logrus.Fields{
1418
"slug": vars["slug"],
1519
"filepath": vars["filepath"],

server/list_handler.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ type listMetadataResponseLinksDataPath struct {
3030
}
3131

3232
func (srv *Server) listHandler(w http.ResponseWriter, r *http.Request, vars map[string]string) int {
33+
if !srv.canRead(r, vars) {
34+
return srv.serveUnauthorized(w, r)
35+
}
36+
3337
allMetadata, err := srv.md.LookupAll(vars["job_id"])
3438
if err != nil {
3539
w.WriteHeader(http.StatusInternalServerError)

server/save_handler.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
)
1111

1212
func (srv *Server) saveHandler(w http.ResponseWriter, r *http.Request, vars map[string]string) int {
13+
if !srv.canWrite(r, vars) {
14+
return srv.serveUnauthorized(w, r)
15+
}
16+
1317
repoSlug, repoSlugOK := vars["slug"]
1418
filepath, filepathOK := vars["filepath"]
1519
jobID, jobIDOK := vars["job_id"]

server/server.go

+8
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ func (srv *Server) getDB() error {
160160
srv.md = db
161161
return nil
162162
}
163+
164+
func (srv *Server) canWrite(r *http.Request, vars map[string]string) bool {
165+
return true
166+
}
167+
168+
func (srv *Server) canRead(r *http.Request, vars map[string]string) bool {
169+
return true
170+
}

0 commit comments

Comments
 (0)