Restructure project
We previously didn't really had any structure in our project apart from creating a new folder for each package in our project root. Now that we have accumulated some packages, we use the well-known Golang project layout in order to clearly communicate our intent with packages. See https://github.com/golang-standards/project-layout
This commit is contained in:
36
internal/api/auth/auth.go
Normal file
36
internal/api/auth/auth.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/internal/config"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/logging"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var log = logging.GetLogger("api/auth")
|
||||
|
||||
const TokenHeader = "X-Poseidon-Token"
|
||||
|
||||
var correctAuthenticationToken []byte
|
||||
|
||||
// InitializeAuthentication returns true iff the authentication is initialized successfully and can be used.
|
||||
func InitializeAuthentication() bool {
|
||||
token := config.Config.Server.Token
|
||||
if token == "" {
|
||||
return false
|
||||
}
|
||||
correctAuthenticationToken = []byte(token)
|
||||
return true
|
||||
}
|
||||
|
||||
func HTTPAuthenticationMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.Header.Get(TokenHeader)
|
||||
if subtle.ConstantTimeCompare([]byte(token), correctAuthenticationToken) == 0 {
|
||||
log.WithField("token", token).Warn("Incorrect token")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user