Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

37 password generation bug with high length #39

Merged
merged 4 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions internal/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ import (
)

const (
letterChars = "abcdefghijklmnopqrstuvwxyz"
letterChars = "abcdefghijklmnopqrstuvwxyz"
upperLetterChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberChars = "0123456789"
specialChars = "!@#$%^&*()_+{}[]:;<>,.?/~`"
numberChars = "0123456789"
specialChars = "!@#$%^&*()_+{}[]:;<>,.?/~`"
MaxLength = 2048
)

func GeneratePassword(length int, enableSpecialChars bool, enableNumeric bool, enableCapital bool) string {
var chars string
chars += letterChars

if enableSpecialChars && len(specialChars) > 0 {
chars += specialChars
func getCharacters(enableCapital bool, enableNumeric bool, enableSpecialChars bool) string {
chars := letterChars
if enableCapital {
chars += upperLetterChars
}

if enableNumeric && len(numberChars) > 0 {
if enableNumeric {
chars += numberChars
}
if enableSpecialChars {
chars += specialChars
}
return chars
}

if enableCapital && len(upperLetterChars) > 0 {
chars += upperLetterChars
func GeneratePassword(length int, enableSpecialChars bool, enableNumeric bool, enableCapital bool) string {
if length > MaxLength {
length = MaxLength
}
chars := getCharacters(enableCapital, enableNumeric, enableSpecialChars)

rand.NewSource(time.Now().UnixNano())
password := make([]byte, length)
Expand All @@ -52,4 +57,3 @@ func GeneratePassword(length int, enableSpecialChars bool, enableNumeric bool, e

return string(password)
}

6 changes: 6 additions & 0 deletions internal/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ func TestGeneratePassword(t *testing.T) {
if len(password3) != 6 || strings.ContainsAny(password3, upperLetterChars) || strings.ContainsAny(password3, numberChars) || strings.ContainsAny(password3, specialChars) {
t.Errorf("Expected password length to be 6, without special characters and numeric characters or capital. but got %d", len(password3))
}

// Test case 4: Generate password with length greater than 2048
password4 := GeneratePassword(MaxLength*2, false, false, false)
if len(password4) != MaxLength || strings.ContainsAny(password4, upperLetterChars) || strings.ContainsAny(password4, numberChars) || strings.ContainsAny(password4, specialChars) {
t.Errorf("Expected password length to be %d, without special characters and numeric characters or capital. but got %v", MaxLength, password4)
}
}
69 changes: 35 additions & 34 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func jsonPage(app *fiber.App) {

// Render index within layouts/main
return c.Render(Prefix+"json", newMap(map[string]any{
"Title": "JSON",
"JSON": c.FormValue("json"),
"Result": result,
"AlertMessage": errorStr,
"action": action,
"Title": "JSON",
"JSON": c.FormValue("json"),
"Result": result,
"AlertMessage": errorStr,
"action": action,
}), MainLayout)
})
}
Expand Down Expand Up @@ -168,12 +168,13 @@ func passwordPage(app *fiber.App) {
}
password := internal.GeneratePassword(length, isEspecial, isNumberic, isCapital)
return c.Render(Prefix+"password", newMap(map[string]any{
"Title": "Password Generator",
"Password": password,
"length": length,
"especial": especial,
"capital": capital,
"number": number,
"Title": "Password Generator",
"Password": password,
"length": len(password),
"MaxLength": internal.MaxLength,
"especial": especial,
"capital": capital,
"number": number,
}), MainLayout)
})
}
Expand All @@ -197,11 +198,11 @@ func yamlPage(app *fiber.App) {

// Render index within layouts/main
return c.Render(Prefix+"yaml", newMap(map[string]any{
"Title": "YAML",
"YAML": c.FormValue("yaml"),
"Result": result,
"AlertMessage": errorStr,
"action": action,
"Title": "YAML",
"YAML": c.FormValue("yaml"),
"Result": result,
"AlertMessage": errorStr,
"action": action,
}), MainLayout)
})
}
Expand Down Expand Up @@ -242,12 +243,12 @@ func jwtPage(app *fiber.App) {

// Render index within layouts/main
return c.Render(Prefix+"jwt", newMap(map[string]any{
"Title": "JWT",
"JWT": jwtToken,
"Header": jwt.Header,
"Claims": jwt.Claims,
"Secret": secret,
"AlertMessage": errorStr,
"Title": "JWT",
"JWT": jwtToken,
"Header": jwt.Header,
"Claims": jwt.Claims,
"Secret": secret,
"AlertMessage": errorStr,
}), MainLayout)
})
}
Expand All @@ -269,10 +270,10 @@ func base64Page(app *fiber.App) {

// Render index within layouts/main
return c.Render(Prefix+"base64", newMap(map[string]any{
"Title": "Base64 encode/decode",
"Decoded": decoded,
"Encoded": encoded,
"AlertMessage": errorStr,
"Title": "Base64 encode/decode",
"Decoded": decoded,
"Encoded": encoded,
"AlertMessage": errorStr,
}), MainLayout)
})
}
Expand Down Expand Up @@ -300,10 +301,10 @@ func urlPage(app *fiber.App) {

// Render index within layouts/main
return c.Render(Prefix+"url", newMap(map[string]any{
"Title": "URL encode/decode",
"Decoded": decoded,
"Encoded": encoded,
"AlertMessage": errorStr,
"Title": "URL encode/decode",
"Decoded": decoded,
"Encoded": encoded,
"AlertMessage": errorStr,
}), MainLayout)
})
}
Expand All @@ -326,10 +327,10 @@ func hashPage(app *fiber.App) {

// Render index within layouts/main
return c.Render(Prefix+"hash", newMap(map[string]any{
"Title": "Hashing",
"String": str,
"Result": result,
"AlertMessage": errorStr,
"Title": "Hashing",
"String": str,
"Result": result,
"AlertMessage": errorStr,
}), MainLayout)
})
}
Expand Down
10 changes: 8 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestJWTPage(t *testing.T) {
jwtPage(app)
t.Run("Test JWT Page", func(t *testing.T) {
// Create a test request to the "/jwt" route with jwt=...
req := httptest.NewRequest(http.MethodGet, "/jwt?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", nil)
req := httptest.NewRequest(http.MethodGet, "/jwt?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&header=&claims=&secret=&action=decode", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
Expand All @@ -198,7 +198,7 @@ func TestJWTPage(t *testing.T) {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

req = httptest.NewRequest(http.MethodGet, "/jwt?jwt=xyz", nil)
req = httptest.NewRequest(http.MethodGet, "/jwt?jwt=xyz&action=decode", nil)
resp, err = app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
Expand All @@ -219,6 +219,12 @@ func TestJWTPage(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

req = httptest.NewRequest(http.MethodGet, `/jwt?jwt=&header=&claims=&secret=&action=encode`, nil)
_, err = app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}
// TODO: Add assertions for the response body or other expectations
})
// TODO: Add assertions for the response body or other expectations
Expand Down
4 changes: 2 additions & 2 deletions server/ui/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div class="container-fluid">
<div class="container-fluid text-center">
<div class="row">
<div class="col-12">
&nbsp;
</div>
</div>
<div class="row g-5">
<div class="row g-5 row-cols-auto">
<div class="col">
<a href="/uuid" class="card-link">
<div class="card min-h-200" style="width: 18rem;">
Expand Down
2 changes: 1 addition & 1 deletion server/ui/password.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<label for="length" class="col-sm-2 col-form-label">Length</label>
<div class="col-sm-10">
<input type="number" onchange="this.form.submit()" class="form-control" id="length" value="{{.length}}" name="length" min="8"
max="512" />
max="{{.MaxLength}}" />
</div>
</div>
</div>
Expand Down