safer type assertion

This commit is contained in:
grumbulon 2023-05-28 01:23:18 -04:00
parent 64c43839de
commit 2a6c9eee4d

View file

@ -91,7 +91,17 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
return
}
zoneFile := newDNSRequest(header.Filename, claims["username"].(string), b)
user, ok := claims["username"].(string)
if !ok {
logger.Response = Response{
Message: "Expected username to be a string",
Status: http.StatusInternalServerError,
Err: errors.New("unable to assert string type to claims interface{}"),
}
logger.newLogEntry().errorLogger(logger.Response)
}
zoneFile := newDNSRequest(header.Filename, user, b)
if err := zoneFile.parse(); err != nil {
logger.Response = Response{
@ -120,7 +130,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
}
// check if request is coming from user not in the DB but has a valid JWT
db.Where("username = ?", claims["username"].(string)).First(&result)
db.Where("username = ?", user).First(&result)
if result.Username == "" {
logger.Response = Response{
@ -136,7 +146,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
db.Create(
&ZoneRequest{
User: claims["username"].(string),
User: user,
Zone: &Zone{
FileName: header.Filename,
},