-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new auth endpoint /getSignatures
- Loading branch information
Showing
6 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
./listener/tmp/* | ||
./listener/bin/* | ||
./listener/bin/* | ||
jwt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/dappnode/validator-monitoring/listener/internal/api/types" | ||
"github.com/dappnode/validator-monitoring/listener/internal/logger" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
// GetSignatures fetches signatures from MongoDB based on the network, tag, and timestamp criteria. | ||
func GetSignatures(w http.ResponseWriter, r *http.Request, dbCollection *mongo.Collection) { | ||
logger.Debug("Received new POST '/getSignatures' request") | ||
|
||
var params types.GetSignatureParams | ||
err := json.NewDecoder(r.Body).Decode(¶ms) | ||
if err != nil { | ||
logger.Error("Failed to decode request body: " + err.Error()) | ||
respondError(w, http.StatusBadRequest, "Invalid request format") | ||
return | ||
} | ||
|
||
// Calculate the cutoff time in Unix milliseconds as a string | ||
cutoffTime := time.Now().Add(-time.Duration(params.Hours) * time.Hour).UnixMilli() | ||
cutoffTimeStr := fmt.Sprintf("%d", cutoffTime) | ||
|
||
filter := bson.M{ | ||
"network": params.Network, | ||
"tag": params.Tag, | ||
"entries.decodedPayload.timestamp": bson.M{ | ||
"$gt": cutoffTimeStr, | ||
}, | ||
} | ||
|
||
var results []bson.M | ||
cursor, err := dbCollection.Find(context.Background(), filter) | ||
if err != nil { | ||
logger.Error("Failed to fetch signatures from MongoDB: " + err.Error()) | ||
respondError(w, http.StatusInternalServerError, "Failed to fetch signatures") | ||
return | ||
} | ||
defer cursor.Close(context.Background()) | ||
|
||
if err = cursor.All(context.Background(), &results); err != nil { | ||
logger.Error("Failed to decode signatures: " + err.Error()) | ||
respondError(w, http.StatusInternalServerError, "Failed to decode signatures") | ||
return | ||
} | ||
|
||
// Set the content type to application/json | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
// Write the JSON response directly to the ResponseWriter | ||
err = json.NewEncoder(w).Encode(results) | ||
if err != nil { | ||
logger.Error("Failed to encode response: " + err.Error()) | ||
respondError(w, http.StatusInternalServerError, "Failed to encode response") | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package middleware | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/dappnode/validator-monitoring/listener/internal/logger" | ||
) | ||
|
||
// AuthMiddleware checks if the provided JWT token is valid. | ||
func AuthMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
token := r.Header.Get("Authorization") | ||
if token == "" { | ||
logger.Debug("No token provided") | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
// We expect the token to be in the format "Bearer <token>" | ||
token = strings.TrimPrefix(token, "Bearer ") | ||
|
||
valid, err := isValidToken(token) | ||
if err != nil || !valid { | ||
logger.Debug("Invalid token") | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
// isValidToken checks if the provided token exists in the /jwt directory. | ||
func isValidToken(token string) (bool, error) { | ||
files, err := os.ReadDir("/jwt") | ||
if err != nil { | ||
logger.Error("Failed to read /jwt directory: " + err.Error()) | ||
return false, err | ||
} | ||
|
||
for _, file := range files { | ||
if !file.IsDir() { | ||
content, err := os.ReadFile("/jwt/" + file.Name()) | ||
if err != nil { | ||
logger.Error("Failed to read token file: " + err.Error()) | ||
continue | ||
} | ||
if strings.TrimSpace(string(content)) == token { | ||
return true, nil | ||
} | ||
} | ||
} | ||
|
||
return false, errors.New("token not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters