Skip to content

Commit

Permalink
Implement Optional Secret Injection
Browse files Browse the repository at this point in the history
  • Loading branch information
MateoGuerreroE committed Jan 15, 2025
1 parent d72dc89 commit 1ec01a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.secret
*.png
*.env
*.db
**/*.db
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func HandleRegisterApp(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Some of the required parameters is missing", http.StatusBadRequest)
}

totp.RegisterApp(requestBody.AppName, requestBody.AccountName)
totp.RegisterApp(requestBody.AppName, requestBody.AccountName, requestBody.Secret)
response := typings.ControllerResponse{Data: "App Registered"}
w.Header().Set("Content-Type", "application/json")

Expand Down
22 changes: 14 additions & 8 deletions totp/totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ func GetSecret(appName string) (string, error) {
return strings.TrimSpace(secret), nil
}

func RegisterApp(appName, accountName string) {
key, err := totp.Generate(totp.GenerateOpts{
Issuer: appName,
AccountName: accountName,
})
if err != nil {
log.Fatal("Error generating key:", err)
func RegisterApp(appName, accountName, secret string) {
var secretKey string
if secret == "" {
key, err := totp.Generate(totp.GenerateOpts{
Issuer: appName,
AccountName: accountName,
})
if err != nil {
log.Fatal("Error generating key:", err)
}
secretKey = key.Secret()
} else {
secretKey = secret
}

// Save secret to database
db := dbhandler.InitDB()
dbhandler.StoreSecret(db, accountName, appName, key.Secret())
dbhandler.StoreSecret(db, accountName, appName, secretKey)

fmt.Printf("App %s registered.\n", appName)
}
Expand Down
1 change: 1 addition & 0 deletions typings/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package typings
type RegisterRequest struct {
AppName string `json:"appName"`
AccountName string `json:"accountName"`
Secret string `json:"secret"`
}

type GenerateRequest struct {
Expand Down

0 comments on commit 1ec01a1

Please sign in to comment.