Skip to content

Commit

Permalink
feat: check if user is authenticated in server
Browse files Browse the repository at this point in the history
Signed-off-by: Jad Chahed <[email protected]>
  • Loading branch information
Jad31 committed Aug 30, 2024
1 parent 9f63108 commit ef1aa34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
9 changes: 7 additions & 2 deletions go/admin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ func (a *Admin) handleExecutionsAdd(c *gin.Context) {
return
}

encryptedToken := server.Encrypt(token.AccessToken, a.auth)
encryptedToken, err := server.Encrypt(token.AccessToken, a.auth)

if err != nil {
slog.Error("Failed to encrypt token: ", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to encrypt token"})
return
}

requestPayload := ExecutionRequest{
Auth: encryptedToken,
Expand All @@ -239,7 +245,6 @@ func (a *Admin) handleExecutionsAdd(c *gin.Context) {

jsonData, err := json.Marshal(requestPayload)


if err != nil {
slog.Error("Failed to marshal request payload: ", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to marshal request payload"})
Expand Down
40 changes: 37 additions & 3 deletions go/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,21 @@ func (s *Server) addExecutions(c *gin.Context) {
return
}

decryptedToken := server.Decrypt(req.Auth, s.ghTokenSalt)
decryptedToken, err := server.Decrypt(req.Auth, s.ghTokenSalt)

slog.Info(req.Auth, decryptedToken)
if err != nil {
c.JSON(http.StatusUnauthorized, &ErrorAPI{Error: "Unauthorized"})
return
}

isUserAuthenticated, err := IsUserAuthenticated(decryptedToken)

if err != nil || !isUserAuthenticated {
c.JSON(http.StatusUnauthorized, &ErrorAPI{Error: "Unauthorized"})
return
}

slog.Info(decryptedToken)

if req.Source == "" || req.SHA == "" || len(req.Workloads) == 0 || req.NumberOfExecutions == "" {
c.JSON(http.StatusBadRequest, &ErrorAPI{Error: "missing argument"})
Expand All @@ -582,7 +594,29 @@ func (s *Server) addExecutions(c *gin.Context) {
}
}

s.appendToQueue(newElements)
// s.appendToQueue(newElements)

c.JSON(http.StatusOK, "ok")
}

func IsUserAuthenticated(accessToken string) (bool, error) {

client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
if err != nil {
slog.Error("Error creating request to Github: %v", err)
return false, err
}

req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Accept", "application/vnd.github+json")

resp, err := client.Do(req)
if err != nil {
slog.Error("Error making request to Github: %v", err)
return false, err
}
defer resp.Body.Close()

return resp.StatusCode == http.StatusOK, nil
}
24 changes: 14 additions & 10 deletions go/tools/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,57 @@ const (
ErrorIncorrectConfiguration = "incorrect configuration"
)

func Encrypt(stringToEncrypt string, keyString string) (encryptedString string) {
func Encrypt(stringToEncrypt string, keyString string) (string, error) {
log.Println(stringToEncrypt, keyString)
key, _ := hex.DecodeString(keyString)
plaintext := []byte(stringToEncrypt)

block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
return "", err
}

aesGCM, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
return "", err
}

nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
return "", err
}

ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil)
return fmt.Sprintf("%x", ciphertext)
return fmt.Sprintf("%x", ciphertext), nil
}

func Decrypt(encryptedString string, keyString string) (decryptedString string) {
func Decrypt(encryptedString string, keyString string) (string, error) {

key, _ := hex.DecodeString(keyString)
enc, _ := hex.DecodeString(encryptedString)

block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
return "", err
}

aesGCM, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
return "", err
}

nonceSize := aesGCM.NonceSize()

if len(enc) <= nonceSize {
return "", fmt.Errorf("Encrypted string is too short")
}

nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]

plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
panic(err.Error())
return "", err
}

return fmt.Sprintf("%s", plaintext)
return fmt.Sprintf("%s", plaintext), nil

Check failure on line 87 in go/tools/server/utils.go

View workflow job for this annotation

GitHub Actions / Lint all go files

S1025: the argument's underlying type is a slice of bytes, should use a simple conversion instead of fmt.Sprintf (gosimple)
}

0 comments on commit ef1aa34

Please sign in to comment.