refactor: move string generator to crypto package

This commit is contained in:
Trong Huu Nguyen
2025-06-16 09:55:35 +02:00
parent 4e1c8e68f8
commit 4bf3b1bdd4
5 changed files with 21 additions and 36 deletions

16
internal/crypto/text.go Normal file
View File

@@ -0,0 +1,16 @@
package crypto
import (
"crypto/rand"
"encoding/base64"
)
// Text generates a cryptographically secure random string of a given length, and base64 URL-encodes it.
func Text(length int) (string, error) {
data := make([]byte, length)
if _, err := rand.Read(data); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(data), nil
}

View File

@@ -16,7 +16,6 @@ import (
mw "github.com/nais/wonderwall/pkg/middleware"
"github.com/nais/wonderwall/pkg/openid"
"github.com/nais/wonderwall/pkg/openid/acr"
"github.com/nais/wonderwall/pkg/strings"
"github.com/nais/wonderwall/pkg/url"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
@@ -76,12 +75,12 @@ func (c *Client) newAuthorizationCodeParams(r *http.Request) (openid.Authorizati
return req, fmt.Errorf("generating callback url: %w", err)
}
nonce, err := strings.GenerateBase64(32)
nonce, err := crypto.Text(32)
if err != nil {
return req, fmt.Errorf("creating nonce: %w", err)
}
state, err := strings.GenerateBase64(32)
state, err := crypto.Text(32)
if err != nil {
return req, fmt.Errorf("creating state: %w", err)
}

View File

@@ -8,7 +8,6 @@ import (
"github.com/nais/wonderwall/internal/crypto"
"github.com/nais/wonderwall/pkg/cookie"
"github.com/nais/wonderwall/pkg/openid"
"github.com/nais/wonderwall/pkg/strings"
urlpkg "github.com/nais/wonderwall/pkg/url"
)
@@ -24,7 +23,7 @@ func NewLogout(c *Client, r *http.Request) (*Logout, error) {
return nil, fmt.Errorf("generating logout callback url: %w", err)
}
state, err := strings.GenerateBase64(32)
state, err := crypto.Text(32)
if err != nil {
return nil, fmt.Errorf("generating state: %w", err)
}

View File

@@ -4,9 +4,9 @@ import (
"fmt"
"net/http"
"github.com/nais/wonderwall/internal/crypto"
"github.com/nais/wonderwall/pkg/openid"
openidconfig "github.com/nais/wonderwall/pkg/openid/config"
"github.com/nais/wonderwall/pkg/strings"
)
// ExternalID returns the external session ID, derived from the given request or id_token; e.g. `sid` or `session_state`.
@@ -33,7 +33,7 @@ func ExternalID(r *http.Request, cfg openidconfig.Provider, idToken *openid.IDTo
}
// 3. generate ID if all else fails
sessionID, err = strings.GenerateBase64(64)
sessionID, err = crypto.Text(64)
if err != nil {
return "", fmt.Errorf("generating session ID: %w", err)
}

View File

@@ -1,29 +0,0 @@
package strings
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)
// GenerateBase64 generates a random string of a given length, and base64 URL-encodes it.
func GenerateBase64(length int) (string, error) {
bytes, err := Generate(length)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(bytes), nil
}
// Generate generates a random byte array of a given length.
func Generate(length int) ([]byte, error) {
bytes := make([]byte, length)
_, err := io.ReadFull(rand.Reader, bytes)
if err != nil {
return nil, fmt.Errorf("reading rand.Reader: %w", err)
}
return bytes, nil
}