-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from HilkopterBob/devel
Devel
- Loading branch information
Showing
14 changed files
with
604 additions
and
172 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
certs/** |
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,11 +1,11 @@ | ||
general: | ||
debug: true | ||
production: false | ||
production: true | ||
network: | ||
forcehttp: true | ||
fqdn: 0.0.0.0 | ||
port: 8080 | ||
ssl: | ||
allowselfsigned: true | ||
certificatepath: /etc/packagelock/ssl/cert.pem | ||
privatekeypath: /etc/packagelock/ssl/privkey.pem | ||
certificatepath: ./certs/testing.crt | ||
privatekeypath: ./certs/testing.key |
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
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,86 @@ | ||
package handler | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"packagelock/config" | ||
"packagelock/structs" | ||
"time" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func LoginHandler(c *fiber.Ctx) error { | ||
type LoginRequest struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
var loginReq LoginRequest | ||
if err := c.BodyParser(&loginReq); err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"error": "Failed to parse request", | ||
}) | ||
} | ||
|
||
var user structs.User | ||
// Find the user by username | ||
for _, u := range Users { | ||
if u.Username == loginReq.Username { | ||
user = u | ||
break | ||
} | ||
} | ||
|
||
// As 'user' is a struct, check for a must-have value (USerID) | ||
// If UserID == "" the user couldn't be found -> doesn't exist! | ||
if user.UserID == "" { | ||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ | ||
"error": "Invalid username or password", | ||
}) | ||
} | ||
|
||
// Validate the password | ||
if user.Password != loginReq.Password { | ||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ | ||
"error": "Invalid username or password", | ||
}) | ||
} | ||
|
||
// Generate JWT token | ||
token := jwt.New(jwt.SigningMethodRS256) | ||
claims := token.Claims.(jwt.MapClaims) | ||
claims["username"] = user.Username | ||
claims["userID"] = user.UserID | ||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix() // 3 days expiry | ||
|
||
// Sign and get the encoded token | ||
keyData, err := os.ReadFile(config.Config.GetString("network.ssl.privatekeypath")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(keyData) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
tokenString, err := token.SignedString(privateKey) | ||
if err != nil { | ||
log.Println("Failed to generate JWT token:", err) | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"error": "Failed to generate token", | ||
}) | ||
} | ||
|
||
// Add the token to the user's APIToken slice | ||
user.APIToken = append(user.APIToken, tokenString) | ||
|
||
// Return the token and user information | ||
return c.JSON(fiber.Map{ | ||
"message": "Login successful", | ||
"token": tokenString, | ||
"username": user.Username, | ||
}) | ||
} |
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,61 +1,70 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
"packagelock/structs" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// GetAgentByID filters a slice of Agents for a matching Agent.Agent_ID. | ||
// It returns IndentedJSON with: http.StatusOK or http.StatusNotFound. | ||
func GetAgentByID(c *gin.Context) { | ||
id := c.Param("id") | ||
// It returns a JSON response with fiber.StatusOK or fiber.StatusNotFound. | ||
func GetAgentByID(c *fiber.Ctx) error { | ||
id := c.Params("id") | ||
|
||
for _, a := range Agents { | ||
if strconv.Itoa(a.Host_ID) == id { | ||
c.IndentedJSON(http.StatusOK, a) | ||
return | ||
return c.Status(fiber.StatusOK).JSON(a) | ||
} | ||
} | ||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "no agent under that id"}) | ||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "no agent under that id"}) | ||
} | ||
|
||
// POST Functions | ||
func RegisterAgent(c *gin.Context) { | ||
// RegisterAgent handles POST requests to register a new agent. | ||
func RegisterAgent(c *fiber.Ctx) error { | ||
var newAgent structs.Agent | ||
|
||
if err := c.BindJSON(&newAgent); err != nil { | ||
// Parse the JSON request body into newAgent | ||
if err := c.BodyParser(&newAgent); err != nil { | ||
// TODO: Add logs | ||
// TODO: Add errorhandling | ||
return | ||
// TODO: Add error handling | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"error": "Cannot parse JSON", | ||
}) | ||
} | ||
|
||
// Add new agent to the Agents slice | ||
Agents = append(Agents, newAgent) | ||
c.IndentedJSON(http.StatusCreated, newAgent) | ||
|
||
// Respond with the newly created agent | ||
return c.Status(fiber.StatusCreated).JSON(newAgent) | ||
} | ||
|
||
func GetHostByAgentID(c *gin.Context) { | ||
var agent_by_id structs.Agent | ||
// GetHostByAgentID finds the host for a given agent ID. | ||
func GetHostByAgentID(c *fiber.Ctx) error { | ||
var agentByID structs.Agent | ||
|
||
// gets the value from /agent/:id/host | ||
id := c.Param("id") | ||
// Get the value from /agent/:id/host | ||
id := c.Params("id") | ||
|
||
// finds the agent by the URL-ID | ||
// Find the agent by the URL-ID | ||
for _, a := range Agents { | ||
if strconv.Itoa(a.Host_ID) == id { | ||
// c.IndentedJSON(http.StatusOK, a) | ||
agent_by_id = a | ||
agentByID = a | ||
break | ||
} | ||
} | ||
|
||
// finds host with same id as agent | ||
if agentByID.Agent_ID == 0 { | ||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "no agent under that id"}) | ||
} | ||
|
||
// Find the host with the same id as the agent | ||
for _, host := range Hosts { | ||
if host.ID == agent_by_id.Agent_ID { | ||
c.IndentedJSON(http.StatusOK, host) | ||
return | ||
if host.ID == agentByID.Agent_ID { | ||
return c.Status(fiber.StatusOK).JSON(host) | ||
} | ||
} | ||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "no agent under that id"}) | ||
|
||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "no host found for that agent"}) | ||
} |
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,15 +1,15 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func GetHosts(c *gin.Context) { | ||
c.IndentedJSON(http.StatusOK, Hosts) | ||
// GetHosts responds with a list of all hosts. | ||
func GetHosts(c *fiber.Ctx) error { | ||
return c.Status(fiber.StatusOK).JSON(Hosts) | ||
} | ||
|
||
func GetAgents(c *gin.Context) { | ||
c.IndentedJSON(http.StatusOK, Agents) | ||
// GetAgents responds with a list of all agents. | ||
func GetAgents(c *fiber.Ctx) error { | ||
return c.Status(fiber.StatusOK).JSON(Agents) | ||
} |
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,21 +1,27 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
"packagelock/structs" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func RegisterHost(c *gin.Context) { | ||
// RegisterHost handles the registration of a new host. | ||
func RegisterHost(c *fiber.Ctx) error { | ||
var newHost structs.Host | ||
|
||
if err := c.BindJSON(&newHost); err != nil { | ||
// Parse the JSON request body into newHost | ||
if err := c.BodyParser(&newHost); err != nil { | ||
// TODO: Add logs | ||
// TODO: Add errorhandling | ||
return | ||
// TODO: Add error handling | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"error": "Cannot parse JSON", | ||
}) | ||
} | ||
|
||
// Append new host to the Hosts slice | ||
Hosts = append(Hosts, newHost) | ||
c.IndentedJSON(http.StatusCreated, newHost) | ||
|
||
// Respond with the newly created host | ||
return c.Status(fiber.StatusCreated).JSON(newHost) | ||
} |
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
Oops, something went wrong.