-
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.
- Loading branch information
Son Roy Almerol
committed
Nov 18, 2024
1 parent
eca8fa5
commit 5e870a7
Showing
5 changed files
with
110 additions
and
1 deletion.
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
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,51 @@ | ||
package store | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/sonroyaalmerol/pbs-plus/internal/utils" | ||
) | ||
|
||
func CheckAgentAuth(r *http.Request) error { | ||
auth := r.Header.Get("Authorization") | ||
if !strings.HasPrefix(auth, "PBSPlusAPIAgent=") { | ||
return fmt.Errorf("CheckAgentAuth: invalid auth prefix") | ||
} | ||
|
||
privKeyDir := filepath.Join(DbBasePath, "agent_keys") | ||
|
||
authTok := strings.TrimPrefix(auth, "PBSPlusAPIAgent=") | ||
authSplit := strings.Split(authTok, ":") | ||
|
||
privKeyFilePath := filepath.Join( | ||
privKeyDir, | ||
fmt.Sprintf("%s.key", authSplit[0]), | ||
) | ||
|
||
privKeyFile, err := os.ReadFile(privKeyFilePath) | ||
if err != nil { | ||
return fmt.Errorf("CheckAgentAuth: error opening private key file \"%s\" -> %w", privKeyFilePath, err) | ||
} | ||
|
||
pubKey, err := utils.GeneratePublicKeyFromPrivateKey(privKeyFile) | ||
if err != nil { | ||
return fmt.Errorf("CheckAgentAuth: error generating pub key \"%s\" -> %w", privKeyFilePath, err) | ||
} | ||
|
||
passedPub, err := base64.StdEncoding.DecodeString(authSplit[1]) | ||
if err != nil { | ||
return fmt.Errorf("CheckAgentAuth: error pub key -> %w", err) | ||
} | ||
|
||
if !reflect.DeepEqual(pubKey, passedPub) { | ||
return fmt.Errorf("CheckAgentAuth: invalid auth") | ||
} | ||
|
||
return nil | ||
} |
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