Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d3faa071e | |||
| 183ad41574 | |||
| 6f9fb2d8ec | |||
| 74e56f55d6 | |||
| ae4020fdcf | |||
| 2c1ceed904 | |||
| 458ef03a7c |
313
README.md
313
README.md
@@ -1,3 +1,312 @@
|
|||||||
# appcore
|
# AppCore
|
||||||
|
|
||||||
go app core packages
|
A comprehensive, production-ready Go utility library providing core functionality for building modern web applications.
|
||||||
|
|
||||||
|
[](https://go.dev/)
|
||||||
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
AppCore is a modular Go library that provides essential building blocks for web applications, with a focus on security, developer experience, and production reliability. The library offers reusable packages for common tasks including cryptography, email handling, JWT authentication, validation, and HTTP utilities.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Security-First Design**: Modern cryptography (ED25519, AES-GCM, Argon2id) following OWASP recommendations
|
||||||
|
- **Type Safety**: Extensive use of Go generics and strong typing throughout
|
||||||
|
- **Production Ready**: Comprehensive error handling and proper resource cleanup
|
||||||
|
- **Developer Experience**: Convenient helper functions with consistent naming conventions
|
||||||
|
- **Cross-Platform Support**: Works seamlessly across macOS, Linux, and Windows
|
||||||
|
- **Modular Architecture**: Import only the packages you need
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go get code.patial.tech/go/appcore
|
||||||
|
```
|
||||||
|
|
||||||
|
## Packages
|
||||||
|
|
||||||
|
### Security & Cryptography (`crypto`)
|
||||||
|
|
||||||
|
Password hashing, encryption, digital signatures, and secure random generation.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/crypto"
|
||||||
|
|
||||||
|
// Password hashing (Argon2id - OWASP compliant)
|
||||||
|
hash, err := crypto.PasswordHash("mypassword")
|
||||||
|
valid := crypto.ComparePasswordHash("mypassword", hash)
|
||||||
|
|
||||||
|
// AES-GCM encryption
|
||||||
|
key := crypto.NewEncryptionKey(32)
|
||||||
|
encrypted, err := crypto.Encrypt([]byte("data"), key)
|
||||||
|
decrypted, err := crypto.Decrypt(encrypted, key)
|
||||||
|
|
||||||
|
// ED25519 key management
|
||||||
|
privateKey, publicKey, err := crypto.NewPersistedED25519("./keys")
|
||||||
|
|
||||||
|
// Secure random generation
|
||||||
|
randomBytes, err := crypto.RandomBytes(32)
|
||||||
|
randomHex := crypto.RandomBytesHex(16)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `MD5()`, `SHA256()`, `PasswordHash()`, `ComparePasswordHash()`, `NewEncryptionKey()`, `Encrypt()`, `Decrypt()`, `NewPersistedED25519()`, `ParseEdPrivateKey()`, `ParseEdPublicKey()`, `RandomBytes()`, `RandomBytesHex()`
|
||||||
|
|
||||||
|
### JWT Authentication (`jwt`)
|
||||||
|
|
||||||
|
JWT token signing and parsing with support for EdDSA and HS256 algorithms.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/jwt"
|
||||||
|
|
||||||
|
// EdDSA (recommended)
|
||||||
|
token, err := jwt.SignEdDSA(privateKey, "issuer", "subject", time.Hour, extraClaims)
|
||||||
|
claims, err := jwt.ParseEdDSA(token, publicKey, "issuer")
|
||||||
|
|
||||||
|
// HS256
|
||||||
|
token, err := jwt.SignHS256(secret, "issuer", "subject", time.Hour, extraClaims)
|
||||||
|
claims, err := jwt.ParseHS256(token, secret, "issuer")
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Sign()`, `Parse()`, `SignEdDSA()`, `ParseEdDSA()`, `SignHS256()`, `ParseHS256()`
|
||||||
|
|
||||||
|
### Email Handling (`email`)
|
||||||
|
|
||||||
|
Email construction and delivery with SMTP support and development mode.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/email"
|
||||||
|
|
||||||
|
msg := &email.Message{
|
||||||
|
From: "sender@example.com",
|
||||||
|
To: []string{"recipient@example.com"},
|
||||||
|
Subject: "Hello",
|
||||||
|
Body: "<h1>Hello World</h1>",
|
||||||
|
}
|
||||||
|
|
||||||
|
// SMTP transport
|
||||||
|
transport := email.NewSMTPTransport("smtp.example.com", 587, "user", "pass")
|
||||||
|
err := msg.Send(transport)
|
||||||
|
|
||||||
|
// Development mode (opens in browser)
|
||||||
|
transport := email.NewDumpToTempTransport()
|
||||||
|
err := msg.Send(transport)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Types**: `Message`, `Transport`, `SMTPTransport`, `DumpToTempTransport`
|
||||||
|
|
||||||
|
### Data Validation (`validate`)
|
||||||
|
|
||||||
|
Struct and map validation using go-playground/validator.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/validate"
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Email string `json:"email" validate:"required,email"`
|
||||||
|
Age int `json:"age" validate:"gte=0,lte=130"`
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &User{Email: "test@example.com", Age: 25}
|
||||||
|
err := validate.Struct(user) // Returns validation errors with JSON field names
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Struct()`, `Map()`, `RegisterAlias()`, `RegisterValidation()`
|
||||||
|
|
||||||
|
### Environment Configuration (`dotenv`)
|
||||||
|
|
||||||
|
Environment file parsing with variable expansion and struct assignment.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/dotenv"
|
||||||
|
|
||||||
|
// Read .env file
|
||||||
|
env, err := dotenv.Read(".env")
|
||||||
|
|
||||||
|
// Assign to struct
|
||||||
|
type Config struct {
|
||||||
|
Port int `env:"PORT"`
|
||||||
|
Host string `env:"HOST,HOSTNAME"` // Supports fallback tags
|
||||||
|
}
|
||||||
|
cfg := &Config{}
|
||||||
|
err := dotenv.Assign(env, cfg)
|
||||||
|
|
||||||
|
// Write .env file
|
||||||
|
err := dotenv.Write(env, ".env")
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Read()`, `LoadFile()`, `Write()`, `Assign()`
|
||||||
|
|
||||||
|
### HTTP Request Utilities (`request`)
|
||||||
|
|
||||||
|
Request parsing, validation, and pagination helpers.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/request"
|
||||||
|
|
||||||
|
// Parse and validate JSON payload
|
||||||
|
type CreateUserRequest struct {
|
||||||
|
Email string `json:"email" validate:"required,email"`
|
||||||
|
}
|
||||||
|
var req CreateUserRequest
|
||||||
|
err := request.PayloadWithValidate(r, &req)
|
||||||
|
|
||||||
|
// Get pagination parameters
|
||||||
|
pager := request.GetPager(r.URL.Query(), 50) // default page size 50
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Payload()`, `PayloadWithValidate()`, `PayloadMap()`, `FormField()`, `GetPager()`
|
||||||
|
|
||||||
|
### HTTP Response Utilities (`response`)
|
||||||
|
|
||||||
|
Standardized JSON response formatting.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/response"
|
||||||
|
|
||||||
|
// Success responses
|
||||||
|
response.Ok(w, data)
|
||||||
|
response.Paged(w, data, pager, totalRecords)
|
||||||
|
|
||||||
|
// Error responses
|
||||||
|
response.BadRequest(w, "Invalid input")
|
||||||
|
response.InternalServerError(w, err)
|
||||||
|
response.NotAuthorized(w, "Invalid credentials")
|
||||||
|
response.Forbidden(w, "Access denied")
|
||||||
|
response.SessionExpired(w)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Ok()`, `Paged()`, `BadRequest()`, `InternalServerError()`, `SessionExpired()`, `NotAuthorized()`, `Forbidden()`
|
||||||
|
|
||||||
|
### Pointer Utilities (`ptr`)
|
||||||
|
|
||||||
|
Safe reference and dereference operations with generic support.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/ptr"
|
||||||
|
|
||||||
|
// Generic reference/dereference
|
||||||
|
strPtr := ptr.Ref("hello")
|
||||||
|
str := ptr.Deref(strPtr, "default")
|
||||||
|
|
||||||
|
// Type-specific helpers
|
||||||
|
boolPtr := ptr.Bool(true)
|
||||||
|
numPtr := ptr.Number(42)
|
||||||
|
strPtr := ptr.Str("test")
|
||||||
|
|
||||||
|
// Safe getters with defaults
|
||||||
|
value := ptr.GetBool(boolPtr, false)
|
||||||
|
num := ptr.GetNumber(numPtr, 0)
|
||||||
|
str := ptr.GetStr(strPtr, "")
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Ref()`, `Deref()`, `Bool()`, `GetBool()`, `Str()`, `GetStr()`, `StrTrim()`, `Number()`, `GetNumber()`
|
||||||
|
|
||||||
|
### Unique ID Generation (`uid`)
|
||||||
|
|
||||||
|
UUID v7 and Sqids-based ID generation.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/uid"
|
||||||
|
|
||||||
|
// UUID v7 (time-based)
|
||||||
|
id := uid.NewUUID()
|
||||||
|
|
||||||
|
// Sqids (URL-friendly encoding)
|
||||||
|
encoded := uid.Encode(123, 456)
|
||||||
|
numbers, err := uid.Decode(encoded)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `NewUUID()`, `Encode()`, `Decode()`
|
||||||
|
|
||||||
|
### Date/Time Utilities (`date`)
|
||||||
|
|
||||||
|
Date parsing, arithmetic, and boundary calculations.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/date"
|
||||||
|
|
||||||
|
// Parse ISO date
|
||||||
|
t, err := date.ParseISODate("2024-01-15")
|
||||||
|
|
||||||
|
// Calculate working days
|
||||||
|
days := date.CountWorkingDays(startDate, endDate)
|
||||||
|
|
||||||
|
// Date boundaries
|
||||||
|
startOfDay := date.StartOfDay(now)
|
||||||
|
endOfMonth := date.EndOfMonth(now)
|
||||||
|
startOfWeek := date.StartOfWeek(now)
|
||||||
|
|
||||||
|
// Date arithmetic
|
||||||
|
lastWeek := date.LastWeek()
|
||||||
|
lastMonth := date.LastMonth()
|
||||||
|
threeMonthsAgo := date.SubtractMonths(now, 3)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `ParseISODate()`, `CountWorkingDays()`, `StartOfDay()`, `EndOfDay()`, `StartOfMonth()`, `EndOfMonth()`, `StartOfWeek()`, `EndOfWeek()`, `StartOfYear()`, `EndOfYear()`, `LastWeek()`, `LastMonth()`, `SubtractMonths()`, `SubtractDays()`, `SubtractAYear()`
|
||||||
|
|
||||||
|
### Compression (`gz`)
|
||||||
|
|
||||||
|
Gzip compression and decompression.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "code.patial.tech/go/appcore/gz"
|
||||||
|
|
||||||
|
// Compress data
|
||||||
|
compressed, err := gz.Zip([]byte("data"))
|
||||||
|
|
||||||
|
// Decompress data
|
||||||
|
decompressed, err := gz.UnZip(compressed)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Functions**: `Zip()`, `UnZip()`
|
||||||
|
|
||||||
|
### Additional Utilities
|
||||||
|
|
||||||
|
- **`open`**: Cross-platform file/URI opening (`WithDefaultApp()`, `App()`)
|
||||||
|
- **`structs`**: Reflection utilities (`Map()` - converts structs to maps)
|
||||||
|
- **`mime`**: MIME type lookups from file extensions
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
- `github.com/go-playground/validator/v10` - Data validation
|
||||||
|
- `github.com/golang-jwt/jwt/v5` - JWT handling
|
||||||
|
- `github.com/google/uuid` - UUID generation
|
||||||
|
- `github.com/sqids/sqids-go` - URL-friendly ID encoding
|
||||||
|
- `golang.org/x/crypto` - Cryptographic primitives
|
||||||
|
|
||||||
|
## Use Cases
|
||||||
|
|
||||||
|
AppCore is particularly well-suited for:
|
||||||
|
|
||||||
|
- Building secure web applications and REST APIs
|
||||||
|
- Standardizing authentication/authorization patterns
|
||||||
|
- Email delivery systems
|
||||||
|
- Configuration management
|
||||||
|
- API response formatting
|
||||||
|
- Data validation and sanitization
|
||||||
|
|
||||||
|
## Security
|
||||||
|
|
||||||
|
- **Password Hashing**: Uses Argon2id with OWASP-recommended parameters (m=19456, t=2, p=1)
|
||||||
|
- **Encryption**: AES-GCM authenticated encryption
|
||||||
|
- **JWT**: Supports EdDSA (recommended) and HS256 algorithms
|
||||||
|
- **Random Generation**: Cryptographically secure random bytes
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT License - see [LICENSE](LICENSE) file for details.
|
||||||
|
|
||||||
|
Copyright 2024/2025 Patial Tech (Ankit Patial).
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Contributions are welcome! Please ensure:
|
||||||
|
- Code follows Go best practices and project conventions
|
||||||
|
- All tests pass (`go test ./...`)
|
||||||
|
- New features include appropriate documentation
|
||||||
|
- Security-sensitive code is reviewed carefully
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
For issues, questions, or feature requests, please open an issue on the project repository.
|
||||||
|
|||||||
@@ -15,10 +15,14 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewEncryptionKey will generate new key as base64 encoded string
|
// NewEncryptionKey will generate new key as base64 encoded string.
|
||||||
//
|
// Size must be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256 respectively.
|
||||||
// should be of 16, 24 or 32 bytes in length
|
|
||||||
func NewEncryptionKey(size uint8) (string, error) {
|
func NewEncryptionKey(size uint8) (string, error) {
|
||||||
|
// Validate AES key sizes
|
||||||
|
if size != 16 && size != 24 && size != 32 {
|
||||||
|
return "", fmt.Errorf("invalid key size %d: must be 16, 24, or 32 bytes for AES", size)
|
||||||
|
}
|
||||||
|
|
||||||
b, err := RandomBytes(size)
|
b, err := RandomBytes(size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -70,8 +74,14 @@ func Decrypt(key, text string) (string, error) {
|
|||||||
nonceSize := gcm.NonceSize()
|
nonceSize := gcm.NonceSize()
|
||||||
enc, err := hex.DecodeString(text)
|
enc, err := hex.DecodeString(text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", fmt.Errorf("invalid hex encoding: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate ciphertext length to prevent panic
|
||||||
|
if len(enc) < nonceSize {
|
||||||
|
return "", fmt.Errorf("ciphertext too short: got %d bytes, need at least %d", len(enc), nonceSize)
|
||||||
|
}
|
||||||
|
|
||||||
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]
|
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]
|
||||||
|
|
||||||
// decrypt
|
// decrypt
|
||||||
|
|||||||
@@ -17,14 +17,6 @@ func MD5(b []byte) string {
|
|||||||
return hex.EncodeToString(hash[:])
|
return hex.EncodeToString(hash[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func MD5Int(b []byte) uint64 {
|
|
||||||
n := new(big.Int)
|
|
||||||
n.SetString(MD5(b), 16)
|
|
||||||
return n.Uint64()
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// SHA256 Sum256 is generally preferred over md5.hash due to its superior security and resistance to collision attacks
|
// SHA256 Sum256 is generally preferred over md5.hash due to its superior security and resistance to collision attacks
|
||||||
func SHA256(b []byte) string {
|
func SHA256(b []byte) string {
|
||||||
hash := sha256.Sum256(b)
|
hash := sha256.Sum256(b)
|
||||||
|
|||||||
89
date/date.go
Normal file
89
date/date.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||||
|
//
|
||||||
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||||
|
// See http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
package date
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ParseISODate ISO date format to utc
|
||||||
|
func ParseISODate(dt string) (time.Time, error) {
|
||||||
|
d, err := time.Parse(time.RFC3339, dt)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.UTC(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountWorkingDays(from, end time.Time) int {
|
||||||
|
workingDays := 0
|
||||||
|
start := from // from.AddDate(0, 0, 1)
|
||||||
|
for d := start; d.Before(end) || d.Equal(end); d = d.AddDate(0, 0, 1) {
|
||||||
|
weekday := d.Weekday()
|
||||||
|
if weekday != time.Saturday && weekday != time.Sunday {
|
||||||
|
workingDays++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return workingDays
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartOfDay(date time.Time) time.Time {
|
||||||
|
year, month, day := date.Date()
|
||||||
|
return time.Date(year, month, day, 0, 0, 0, 0, date.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
func EndOfDay(date time.Time) time.Time {
|
||||||
|
year, month, day := date.Date()
|
||||||
|
return time.Date(year, month, day, 23, 59, 59, 0, date.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartOfMonth(date time.Time) time.Time {
|
||||||
|
year, month, _ := date.Date()
|
||||||
|
return time.Date(year, month, 1, 0, 0, 0, 0, date.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
func EndOfMonth(date time.Time) time.Time {
|
||||||
|
return EndOfDay(date.AddDate(0, 1, -date.Day()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartOfWeek(date time.Time) time.Time {
|
||||||
|
return StartOfDay(date.AddDate(0, 0, int(time.Sunday)-int(date.Weekday())))
|
||||||
|
}
|
||||||
|
|
||||||
|
func EndOfWeek(date time.Time) time.Time {
|
||||||
|
return EndOfDay(date.AddDate(0, 0, int(time.Saturday-date.Weekday())))
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartOfYear(date time.Time) time.Time {
|
||||||
|
year, _, _ := date.Date()
|
||||||
|
return time.Date(year, 1, 1, 0, 0, 0, 0, date.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
func EndOfYear(date time.Time) time.Time {
|
||||||
|
return EndOfDay(date.AddDate(0, int(time.December-date.Month())+1, -date.Day()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func LastWeek(date time.Time) time.Time {
|
||||||
|
return date.AddDate(0, 0, -7)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LastMonth(date time.Time) time.Time {
|
||||||
|
return date.AddDate(0, -1, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SubtractMonths(date time.Time, m int) time.Time {
|
||||||
|
return date.AddDate(0, -m, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SubtractDays(date time.Time, d int) time.Time {
|
||||||
|
return date.AddDate(0, 0, -d)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SubtractAYear(date time.Time) time.Time {
|
||||||
|
return date.AddDate(-1, 0, 0)
|
||||||
|
}
|
||||||
83
dotenv/assign.go
Normal file
83
dotenv/assign.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package dotenv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Assign env tag matching values from envMap
|
||||||
|
func Assign[T any](to *T, envMap map[string]string) error {
|
||||||
|
if to == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(envMap) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
val := reflect.Indirect(reflect.ValueOf(to))
|
||||||
|
name := val.Type().Name()
|
||||||
|
for i := range val.NumField() {
|
||||||
|
f := val.Type().Field(i)
|
||||||
|
tag := f.Tag.Get("env")
|
||||||
|
if tag == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var v string
|
||||||
|
var ok bool
|
||||||
|
if multiTag := strings.Split(tag, ","); len(multiTag) > 1 {
|
||||||
|
// multi tags like env:"DB_URL,PG_DB_URL"
|
||||||
|
for _, mt := range multiTag {
|
||||||
|
if v, ok = envMap[strings.TrimSpace(mt)]; ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v, ok = envMap[tag]
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
field := val.FieldByName(f.Name)
|
||||||
|
if !field.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch f.Type.Kind() {
|
||||||
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
||||||
|
if v, err := strconv.ParseInt(v, 10, 64); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
field.SetInt(v)
|
||||||
|
}
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
if v, err := strconv.ParseFloat(v, 64); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
field.SetFloat(v)
|
||||||
|
}
|
||||||
|
case reflect.Bool:
|
||||||
|
if strings.EqualFold(v, "true") {
|
||||||
|
field.SetBool(true)
|
||||||
|
} else {
|
||||||
|
field.SetBool(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
field.SetString(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
slog.Info("override value from env", "field", f.Name)
|
||||||
|
} else {
|
||||||
|
slog.Info("override value from env", "type", name, "field", f.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -23,7 +23,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func parseBytes(src []byte, out map[string]string) error {
|
func parseBytes(src []byte, out map[string]string) error {
|
||||||
src = bytes.Replace(src, []byte("\r\n"), []byte("\n"), -1)
|
src = bytes.ReplaceAll(src, []byte("\r\n"), []byte("\n"))
|
||||||
cutset := src
|
cutset := src
|
||||||
for {
|
for {
|
||||||
cutset = getStatementStart(cutset)
|
cutset = getStatementStart(cutset)
|
||||||
@@ -76,8 +76,8 @@ func getStatementStart(src []byte) []byte {
|
|||||||
func locateKeyName(src []byte) (key string, cutset []byte, err error) {
|
func locateKeyName(src []byte) (key string, cutset []byte, err error) {
|
||||||
// trim "export" and space at beginning
|
// trim "export" and space at beginning
|
||||||
src = bytes.TrimLeftFunc(src, isSpace)
|
src = bytes.TrimLeftFunc(src, isSpace)
|
||||||
if bytes.HasPrefix(src, []byte(exportPrefix)) {
|
if after, ok := bytes.CutPrefix(src, []byte(exportPrefix)); ok {
|
||||||
trimmed := bytes.TrimPrefix(src, []byte(exportPrefix))
|
trimmed := after
|
||||||
if bytes.IndexFunc(trimmed, isSpace) == 0 {
|
if bytes.IndexFunc(trimmed, isSpace) == 0 {
|
||||||
src = bytes.TrimLeftFunc(trimmed, isSpace)
|
src = bytes.TrimLeftFunc(trimmed, isSpace)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,14 +22,17 @@ func Read(dir string, filenames ...string) (envMap map[string]string, err error)
|
|||||||
|
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
slog.Info("read .env", slog.String("file", filepath.Join(dir, filename)))
|
slog.Info("read .env", slog.String("file", filepath.Join(dir, filename)))
|
||||||
individualEnvMap, individualErr := readFile(filepath.Join(dir, filename))
|
m, er := readFile(filepath.Join(dir, filename))
|
||||||
|
if er != nil {
|
||||||
if individualErr != nil {
|
if os.IsNotExist(er) {
|
||||||
err = individualErr
|
slog.Info(".env not found", slog.String("file", filename))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
maps.Copy(envMap, individualEnvMap)
|
maps.Copy(envMap, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func doubleQuoteEscape(line string) string {
|
|||||||
if c == '\r' {
|
if c == '\r' {
|
||||||
toReplace = `\r`
|
toReplace = `\r`
|
||||||
}
|
}
|
||||||
line = strings.Replace(line, string(c), toReplace, -1)
|
line = strings.ReplaceAll(line, string(c), toReplace)
|
||||||
}
|
}
|
||||||
return line
|
return line
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,21 @@ import (
|
|||||||
txt "text/template"
|
txt "text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RenderHTMLTemplate renders HTML email templates with automatic XSS protection.
|
||||||
|
//
|
||||||
|
// SECURITY WARNING:
|
||||||
|
// - DO NOT pass untrusted user input as layout or content parameters
|
||||||
|
// - Only use trusted, predefined template strings
|
||||||
|
// - User data should ONLY be passed via the data map parameter
|
||||||
|
// - The html/template package auto-escapes data to prevent XSS
|
||||||
|
// - Template injection attacks are possible if template strings come from user input
|
||||||
|
//
|
||||||
|
// Example of SECURE usage:
|
||||||
|
//
|
||||||
|
// const layoutTemplate = "<html><body>{{template \"content\" .}}</body></html>"
|
||||||
|
// const contentTemplate = "<h1>Hello {{.Name}}</h1>"
|
||||||
|
// data := map[string]any{"Name": userInput} // Safe - will be escaped
|
||||||
|
// html, err := RenderHTMLTemplate(layoutTemplate, contentTemplate, data)
|
||||||
func RenderHTMLTemplate(layout, content string, data map[string]any) (string, error) {
|
func RenderHTMLTemplate(layout, content string, data map[string]any) (string, error) {
|
||||||
// layout
|
// layout
|
||||||
tpl, err := template.New("layout").Parse(layout)
|
tpl, err := template.New("layout").Parse(layout)
|
||||||
@@ -34,6 +49,21 @@ func RenderHTMLTemplate(layout, content string, data map[string]any) (string, er
|
|||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RenderTxtTemplate renders plain text email templates WITHOUT escaping.
|
||||||
|
//
|
||||||
|
// SECURITY WARNING:
|
||||||
|
// - DO NOT use this function for HTML content (use RenderHTMLTemplate instead)
|
||||||
|
// - DO NOT pass untrusted user input as the content parameter
|
||||||
|
// - Only use trusted, predefined template strings
|
||||||
|
// - User data should ONLY be passed via the data map parameter
|
||||||
|
// - text/template does NOT provide XSS protection - no auto-escaping
|
||||||
|
// - Template injection attacks are possible if content comes from user input
|
||||||
|
//
|
||||||
|
// Example of SECURE usage:
|
||||||
|
//
|
||||||
|
// const textTemplate = "Hello {{.Name}}, your order #{{.OrderID}} is confirmed."
|
||||||
|
// data := map[string]any{"Name": userName, "OrderID": orderID}
|
||||||
|
// text, err := RenderTxtTemplate(textTemplate, data)
|
||||||
func RenderTxtTemplate(content string, data map[string]any) (string, error) {
|
func RenderTxtTemplate(content string, data map[string]any) (string, error) {
|
||||||
tpl, err := txt.New("content").Parse(content)
|
tpl, err := txt.New("content").Parse(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,36 +5,6 @@
|
|||||||
|
|
||||||
package email
|
package email
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"code.patial.tech/go/appcore/open"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Transport interface {
|
type Transport interface {
|
||||||
Send(*Message) error
|
Send(*Message) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpToTemp transport is for development environment to ensure emails are renderd as HTML ok
|
|
||||||
//
|
|
||||||
// once dump operation is done it will try to open the html with default app for html
|
|
||||||
type DumpToTemp struct{}
|
|
||||||
|
|
||||||
func (DumpToTemp) Send(msg *Message) error {
|
|
||||||
// validate msg first
|
|
||||||
if err := msg.Validate(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
dir := os.TempDir()
|
|
||||||
id := time.Now().Format("20060102T150405999")
|
|
||||||
file := filepath.Join(dir, id+".html")
|
|
||||||
|
|
||||||
if err := os.WriteFile(file, []byte(msg.HtmlBody), 0440); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return open.WithDefaultApp(file)
|
|
||||||
}
|
|
||||||
|
|||||||
30
email/transport_dump.go
Normal file
30
email/transport_dump.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package email
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"code.patial.tech/go/appcore/open"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DumpToTemp transport is for development environment to ensure emails are renderd as HTML ok
|
||||||
|
// once dump operation is done it will try to open the html with default app for html
|
||||||
|
type DumpToTemp struct{}
|
||||||
|
|
||||||
|
func (DumpToTemp) Send(msg *Message) error {
|
||||||
|
// validate msg first
|
||||||
|
if err := msg.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dir := os.TempDir()
|
||||||
|
id, _ := uuid.NewV7()
|
||||||
|
file := filepath.Join(dir, id.String()+".html")
|
||||||
|
|
||||||
|
if err := os.WriteFile(file, []byte(msg.HtmlBody), 0440); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return open.WithDefaultApp(file)
|
||||||
|
}
|
||||||
@@ -17,11 +17,12 @@ import (
|
|||||||
// SMTP mailer
|
// SMTP mailer
|
||||||
type SMTP struct {
|
type SMTP struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
// Domain name for smtp
|
// Domain name for smtp
|
||||||
Domain string
|
Domain string
|
||||||
|
|
||||||
|
Port int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t SMTP) Send(msg *Message) error {
|
func (t SMTP) Send(msg *Message) error {
|
||||||
|
|||||||
15
go.mod
15
go.mod
@@ -1,21 +1,20 @@
|
|||||||
module code.patial.tech/go/appcore
|
module code.patial.tech/go/appcore
|
||||||
|
|
||||||
go 1.24
|
go 1.25
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-playground/validator/v10 v10.26.0
|
github.com/go-playground/validator/v10 v10.28.0
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.2
|
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/sqids/sqids-go v0.4.1
|
github.com/sqids/sqids-go v0.4.1
|
||||||
golang.org/x/crypto v0.39.0
|
golang.org/x/crypto v0.45.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.11 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
golang.org/x/net v0.41.0 // indirect
|
golang.org/x/sys v0.38.0 // indirect
|
||||||
golang.org/x/sys v0.33.0 // indirect
|
golang.org/x/text v0.31.0 // indirect
|
||||||
golang.org/x/text v0.26.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
40
go.sum
40
go.sum
@@ -1,19 +1,21 @@
|
|||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY=
|
github.com/gabriel-vasile/mimetype v1.4.11 h1:AQvxbp830wPhHTqc1u7nzoLT+ZFxGY7emj5DR5DYFik=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
|
github.com/gabriel-vasile/mimetype v1.4.11/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
|
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
|
||||||
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
|
github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688=
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU=
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
@@ -24,15 +26,17 @@ github.com/sqids/sqids-go v0.4.1 h1:eQKYzmAZbLlRwHeHYPF35QhgxwZHLnlmVj9AkIj/rrw=
|
|||||||
github.com/sqids/sqids-go v0.4.1/go.mod h1:EMwHuPQgSNFS0A49jESTfIQS+066XQTVhukrzEPScl8=
|
github.com/sqids/sqids-go v0.4.1/go.mod h1:EMwHuPQgSNFS0A49jESTfIQS+066XQTVhukrzEPScl8=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||||
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
|
||||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
|
||||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
||||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||||
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||||
|
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
|
||||||
|
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
45
gz/gz.go
Normal file
45
gz/gz.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||||
|
//
|
||||||
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||||
|
// See http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
package gz
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Zip(data []byte) ([]byte, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
gz := gzip.NewWriter(&b)
|
||||||
|
if _, err := gz.Write(data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gz.Flush(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gz.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnZip(data []byte) ([]byte, error) {
|
||||||
|
b := bytes.NewBuffer(data)
|
||||||
|
r, err := gzip.NewReader(b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer r.Close() // Ensure reader is closed to prevent resource leak
|
||||||
|
|
||||||
|
var resB bytes.Buffer
|
||||||
|
if _, err := resB.ReadFrom(r); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resB.Bytes(), nil
|
||||||
|
}
|
||||||
56
jwt/jwt.go
56
jwt/jwt.go
@@ -8,14 +8,25 @@ package jwt
|
|||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"fmt"
|
||||||
"maps"
|
"maps"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Sign using EdDSA
|
||||||
func Sign(key ed25519.PrivateKey, claims map[string]any, issuer string, d time.Duration) (string, error) {
|
func Sign(key ed25519.PrivateKey, claims map[string]any, issuer string, d time.Duration) (string, error) {
|
||||||
|
return SignEdDSA(key, claims, issuer, d)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Parse(key ed25519.PrivateKey, tokenString string, issuer string) (jwt.MapClaims, error) {
|
||||||
|
return ParseEdDSA(key, tokenString, issuer)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignEdDSA (Edwards-curve Digital Signature Algorithm, typically Ed25519) is an excellent,
|
||||||
|
// modern choice for JWT signing—arguably safer and more efficient than both HS256 and traditional RSA/ECDSA.
|
||||||
|
func SignEdDSA(key ed25519.PrivateKey, claims map[string]any, issuer string, d time.Duration) (string, error) {
|
||||||
cl := jwt.MapClaims{
|
cl := jwt.MapClaims{
|
||||||
"iss": issuer,
|
"iss": issuer,
|
||||||
"iat": jwt.NewNumericDate(time.Now().UTC()),
|
"iat": jwt.NewNumericDate(time.Now().UTC()),
|
||||||
@@ -27,7 +38,7 @@ func Sign(key ed25519.PrivateKey, claims map[string]any, issuer string, d time.D
|
|||||||
return t.SignedString(key)
|
return t.SignedString(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(key ed25519.PrivateKey, tokenString string, issuer string) (jwt.MapClaims, error) {
|
func ParseEdDSA(key ed25519.PrivateKey, tokenString string, issuer string) (jwt.MapClaims, error) {
|
||||||
token, err := jwt.Parse(
|
token, err := jwt.Parse(
|
||||||
tokenString,
|
tokenString,
|
||||||
func(token *jwt.Token) (any, error) {
|
func(token *jwt.Token) (any, error) {
|
||||||
@@ -39,12 +50,47 @@ func Parse(key ed25519.PrivateKey, tokenString string, issuer string) (jwt.MapCl
|
|||||||
jwt.WithExpirationRequired(),
|
jwt.WithExpirationRequired(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return nil, fmt.Errorf("failed to parse JWT token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
||||||
return claims, nil
|
return claims, nil
|
||||||
} else {
|
|
||||||
return nil, errors.New("no claims found")
|
|
||||||
}
|
}
|
||||||
|
return nil, errors.New("no claims found in token")
|
||||||
|
}
|
||||||
|
|
||||||
|
func SignHS256(secret []byte, claims map[string]any, issuer string, d time.Duration) (string, error) {
|
||||||
|
cl := jwt.MapClaims{
|
||||||
|
"iss": issuer,
|
||||||
|
"iat": jwt.NewNumericDate(time.Now().UTC()),
|
||||||
|
"exp": jwt.NewNumericDate(time.Now().Add(d)),
|
||||||
|
}
|
||||||
|
maps.Copy(cl, claims)
|
||||||
|
|
||||||
|
t := jwt.NewWithClaims(jwt.SigningMethodHS256, cl)
|
||||||
|
return t.SignedString(secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseHS256(secret []byte, tokenString string, issuer string) (jwt.MapClaims, error) {
|
||||||
|
token, err := jwt.Parse(
|
||||||
|
tokenString,
|
||||||
|
func(token *jwt.Token) (any, error) {
|
||||||
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||||
|
}
|
||||||
|
return secret, nil
|
||||||
|
},
|
||||||
|
jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}),
|
||||||
|
jwt.WithIssuer(issuer),
|
||||||
|
jwt.WithIssuedAt(),
|
||||||
|
jwt.WithExpirationRequired(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse JWT token: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
||||||
|
return claims, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("no claims found in token")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,3 +60,28 @@ MC4CAQAwBQYDK2VwBCIEIMMkYUKJ9P0gp+Rm9mR4i0KUBT9nFUzxzxjH7sC0xq/F
|
|||||||
|
|
||||||
fmt.Printf("%v", claims)
|
fmt.Printf("%v", claims)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHS256(t *testing.T) {
|
||||||
|
secret := []byte("c4c5fcb25e289e7a23763b013f04fd11b6b0247729216bb98d07f58332360aec")
|
||||||
|
claims := map[string]any{
|
||||||
|
"id": 1,
|
||||||
|
"email": "aa@aa.com",
|
||||||
|
}
|
||||||
|
issuer := "pat"
|
||||||
|
|
||||||
|
// Sign
|
||||||
|
jwt, err := SignHS256(secret, claims, issuer, time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("jwt", jwt)
|
||||||
|
|
||||||
|
// Parse
|
||||||
|
_, err = ParseHS256(secret, jwt, issuer)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
13
open/open.go
13
open/open.go
@@ -6,12 +6,23 @@
|
|||||||
package open
|
package open
|
||||||
|
|
||||||
// WithDefaultApp open a file, directory, or URI using the OS's default application for that object type.
|
// WithDefaultApp open a file, directory, or URI using the OS's default application for that object type.
|
||||||
|
// The input is validated to prevent path traversal and command injection attacks.
|
||||||
func WithDefaultApp(input string) error {
|
func WithDefaultApp(input string) error {
|
||||||
|
if err := validateInput(input); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
cmd := open(input)
|
cmd := open(input)
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithApp will open a file directory, or URI using the specified application.
|
// App will open a file directory, or URI using the specified application.
|
||||||
|
// Both input and appName are validated to prevent command injection attacks.
|
||||||
func App(input string, appName string) error {
|
func App(input string, appName string) error {
|
||||||
|
if err := validateInput(input); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := validateAppName(appName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return openWith(input, appName).Run()
|
return openWith(input, appName).Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
//go:build darwin
|
|
||||||
|
|
||||||
package open
|
package open
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
//go:build linux
|
|
||||||
|
|
||||||
package open
|
package open
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
//go:build windows
|
|
||||||
|
|
||||||
package open
|
package open
|
||||||
|
|
||||||
import (
|
import (
|
||||||
56
open/validate.go
Normal file
56
open/validate.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||||
|
//
|
||||||
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||||
|
// See http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
package open
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// validateInput checks for common security issues in file paths
|
||||||
|
func validateInput(input string) error {
|
||||||
|
if input == "" {
|
||||||
|
return errors.New("input path cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean the path to resolve . and .. references
|
||||||
|
clean := filepath.Clean(input)
|
||||||
|
|
||||||
|
// Check for suspicious characters that could be used for injection
|
||||||
|
// Allow URIs (http://, https://, etc.) but validate file paths
|
||||||
|
if !strings.Contains(clean, "://") {
|
||||||
|
// For file paths, check for path traversal attempts
|
||||||
|
if strings.Contains(clean, "..") {
|
||||||
|
return errors.New("path traversal detected in input")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if path exists (for file paths)
|
||||||
|
if _, err := os.Stat(clean); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("path does not exist: %s", clean)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("cannot access path: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// For URIs, validate scheme
|
||||||
|
allowedSchemes := []string{"http://", "https://", "file://", "ftp://", "mailto:"}
|
||||||
|
valid := false
|
||||||
|
for _, scheme := range allowedSchemes {
|
||||||
|
if strings.HasPrefix(strings.ToLower(clean), scheme) {
|
||||||
|
valid = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !valid {
|
||||||
|
return fmt.Errorf("URI scheme not allowed: %s", clean)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
28
open/validate_darwin.go
Normal file
28
open/validate_darwin.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||||
|
//
|
||||||
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||||
|
// See http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
package open
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// validateAppName validates application names on macOS
|
||||||
|
func validateAppName(appName string) error {
|
||||||
|
if appName == "" {
|
||||||
|
return errors.New("application name cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for suspicious characters that could be used for command injection
|
||||||
|
dangerous := []string{";", "|", "&", "$", "`", "\n", "\r", "$(", "&&", "||"}
|
||||||
|
for _, char := range dangerous {
|
||||||
|
if strings.Contains(appName, char) {
|
||||||
|
return errors.New("application name contains invalid characters")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
34
open/validate_linux.go
Normal file
34
open/validate_linux.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||||
|
//
|
||||||
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||||
|
// See http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
package open
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// validateAppName validates application names on Linux with strict security checks
|
||||||
|
func validateAppName(appName string) error {
|
||||||
|
if appName == "" {
|
||||||
|
return errors.New("application name cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for dangerous characters that could be used for command injection
|
||||||
|
dangerous := []string{";", "|", "&", "$", "`", "\n", "\r", "$(", "&&", "||", ">", "<", "*"}
|
||||||
|
for _, char := range dangerous {
|
||||||
|
if strings.Contains(appName, char) {
|
||||||
|
return errors.New("application name contains invalid characters")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the application exists in PATH (additional security check)
|
||||||
|
if _, err := exec.LookPath(appName); err != nil {
|
||||||
|
return errors.New("application not found in system PATH")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
28
open/validate_windows.go
Normal file
28
open/validate_windows.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||||
|
//
|
||||||
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||||
|
// See http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
package open
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// validateAppName validates application names on Windows
|
||||||
|
func validateAppName(appName string) error {
|
||||||
|
if appName == "" {
|
||||||
|
return errors.New("application name cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for dangerous characters in Windows cmd context
|
||||||
|
dangerous := []string{";", "|", "&", "$", "`", "\n", "\r", "&&", "||", ">", "<", "^"}
|
||||||
|
for _, char := range dangerous {
|
||||||
|
if strings.Contains(appName, char) {
|
||||||
|
return errors.New("application name contains invalid characters")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
37
ptr/ptr.go
37
ptr/ptr.go
@@ -7,26 +7,32 @@ package ptr
|
|||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
|
func Ref[T string | bool | Num](v T) *T {
|
||||||
|
return &v
|
||||||
|
}
|
||||||
|
|
||||||
|
func Deref[T any](v *T) T {
|
||||||
|
if v == nil {
|
||||||
|
var a T
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return *v
|
||||||
|
}
|
||||||
|
|
||||||
func Bool(v bool) *bool {
|
func Bool(v bool) *bool {
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBool(v *bool) bool {
|
func GetBool(v *bool) bool {
|
||||||
if v == nil {
|
return Deref(v)
|
||||||
return false
|
|
||||||
}
|
|
||||||
return *v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Str(v string) *string {
|
func Str(v string) *string {
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
|
|
||||||
func NumStr(v *string) string {
|
func GetStr(v *string) string {
|
||||||
if v == nil {
|
return Deref(v)
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return *v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func StrTrim(v *string) *string {
|
func StrTrim(v *string) *string {
|
||||||
@@ -38,17 +44,14 @@ func StrTrim(v *string) *string {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
type N interface {
|
type Num interface {
|
||||||
uint8 | int8 | uint16 | int16 | uint | int | uint32 | int32 | uint64 | int64 | float32 | float64
|
uint8 | int8 | uint16 | int16 | uint32 | int32 | uint64 | int64 | uint | int | float32 | float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func Number[T N](v T) *T {
|
func Number[T Num](v T) *T {
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNumber[T N](v *T) T {
|
func GetNumber[T Num](v *T) T {
|
||||||
if v == nil {
|
return Deref(v)
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return *v
|
|
||||||
}
|
}
|
||||||
|
|||||||
36
ptr/ptr_test.go
Normal file
36
ptr/ptr_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package ptr
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRefDeref(t *testing.T) {
|
||||||
|
a := 10
|
||||||
|
if Deref(Ref(a)) != a {
|
||||||
|
t.Log("a) had a issue")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := 10.1
|
||||||
|
if Deref(Ref(b)) != b {
|
||||||
|
t.Log("b) had a issue")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c := true
|
||||||
|
if Deref(Ref(c)) != c {
|
||||||
|
t.Log("c) had a issue")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
d := "hello there"
|
||||||
|
if Deref(Ref(d)) != d {
|
||||||
|
t.Log("d) had a issue")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var e string
|
||||||
|
if Deref(Ref(e)) != e {
|
||||||
|
t.Log("e) had a issue")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -14,13 +14,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Pager struct {
|
type Pager struct {
|
||||||
Search string
|
OrderBy *string `json:"orderBy"`
|
||||||
OrderBy *string `json:"orderBy"`
|
OrderAsc *bool `json:"orderAsc"`
|
||||||
OrderAsc *bool `json:"orderAsc"`
|
|
||||||
Page int `json:"page"`
|
Search string
|
||||||
Size int `json:"size"`
|
|
||||||
Total int `json:"total"`
|
Page int `json:"page"`
|
||||||
TotalNeeded bool `json:"-"`
|
Size int `json:"size"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
|
||||||
|
TotalNeeded bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Pager) HasSearch() bool {
|
func (i *Pager) HasSearch() bool {
|
||||||
|
|||||||
@@ -7,12 +7,19 @@ package request
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"code.patial.tech/go/appcore/validate"
|
"code.patial.tech/go/appcore/validate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MaxRequestBodySize is the maximum allowed size for request bodies (1MB default).
|
||||||
|
// This prevents resource exhaustion attacks from large payloads.
|
||||||
|
// Override this value if you need to accept larger requests.
|
||||||
|
var MaxRequestBodySize int64 = 1 << 20 // 1MB
|
||||||
|
|
||||||
func FormField(r *http.Request, key string) (string, error) {
|
func FormField(r *http.Request, key string) (string, error) {
|
||||||
f, err := Payload[map[string]any](r)
|
f, err := Payload[map[string]any](r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -41,8 +48,45 @@ func PayloadWithValidate[T any](r *http.Request) (T, error) {
|
|||||||
|
|
||||||
func Payload[T any](r *http.Request) (T, error) {
|
func Payload[T any](r *http.Request) (T, error) {
|
||||||
var p T
|
var p T
|
||||||
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
|
||||||
|
// Limit request body size to prevent resource exhaustion
|
||||||
|
limited := io.LimitReader(r.Body, MaxRequestBodySize)
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(limited)
|
||||||
|
if err := decoder.Decode(&p); err != nil {
|
||||||
|
// Check if we hit the size limit
|
||||||
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
||||||
|
// Try to read one more byte to see if there's more data
|
||||||
|
var buf [1]byte
|
||||||
|
if n, _ := limited.Read(buf[:]); n == 0 {
|
||||||
|
// We hit the limit
|
||||||
|
return p, errors.New("request body too large")
|
||||||
|
}
|
||||||
|
}
|
||||||
return p, err
|
return p, err
|
||||||
}
|
}
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DecodeJSON decodes JSON from the request body into the provided pointer.
|
||||||
|
// This is useful when you want to decode into an existing variable.
|
||||||
|
// The request body size is limited by MaxRequestBodySize to prevent DoS attacks.
|
||||||
|
func DecodeJSON(r *http.Request, v any) error {
|
||||||
|
// Limit request body size to prevent resource exhaustion
|
||||||
|
limited := io.LimitReader(r.Body, MaxRequestBodySize)
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(limited)
|
||||||
|
if err := decoder.Decode(v); err != nil {
|
||||||
|
// Check if we hit the size limit
|
||||||
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
||||||
|
// Try to read one more byte to see if there's more data
|
||||||
|
var buf [1]byte
|
||||||
|
if n, _ := limited.Read(buf[:]); n == 0 {
|
||||||
|
// We hit the limit
|
||||||
|
return errors.New("request body too large")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ package response
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"code.patial.tech/go/appcore/request"
|
"code.patial.tech/go/appcore/request"
|
||||||
@@ -49,7 +50,10 @@ func reply(w http.ResponseWriter, data any, p *request.Pager) {
|
|||||||
// if data is nil, let's pass it on as null
|
// if data is nil, let's pass it on as null
|
||||||
if data == nil {
|
if data == nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("{\"data\":null}"))
|
_, writeErr := fmt.Fprint(w, "{\"data\":null}")
|
||||||
|
if writeErr != nil {
|
||||||
|
slog.Error(writeErr.Error())
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,30 +68,45 @@ func reply(w http.ResponseWriter, data any, p *request.Pager) {
|
|||||||
func BadRequest(w http.ResponseWriter, err error) {
|
func BadRequest(w http.ResponseWriter, err error) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write(fmt.Appendf(nil, "{\"error\": %q}", err.Error()))
|
_, writeErr := fmt.Fprintf(w, "{\"error\": %q}", err.Error())
|
||||||
|
if writeErr != nil {
|
||||||
|
slog.Error(writeErr.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InternalServerError(w http.ResponseWriter, err error) {
|
func InternalServerError(w http.ResponseWriter, err error) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write(fmt.Appendf(nil, "{\"error\": %q}", err.Error()))
|
_, writeErr := fmt.Fprintf(w, "{\"error\": %q}", err.Error())
|
||||||
|
if writeErr != nil {
|
||||||
|
slog.Error(writeErr.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SessionExpired(w http.ResponseWriter) {
|
func SessionExpired(w http.ResponseWriter) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
w.Write([]byte("{\"error\": \"Session is expired, please login again\"}"))
|
_, writeErr := fmt.Fprint(w, "{\"error\": \"Session is expired, please login again\"}")
|
||||||
|
if writeErr != nil {
|
||||||
|
slog.Error(writeErr.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NotAutorized(w http.ResponseWriter) {
|
func NotAutorized(w http.ResponseWriter) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("{\"error\": \"You are not authorized to perform this action\"}"))
|
_, writeErr := fmt.Fprint(w, "{\"error\": \"You are not authorized to perform this action\"}")
|
||||||
|
if writeErr != nil {
|
||||||
|
slog.Error(writeErr.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forbidden response error
|
// Forbidden response error
|
||||||
func Forbidden(w http.ResponseWriter, err error) {
|
func Forbidden(w http.ResponseWriter, err error) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
w.Write(fmt.Appendf(nil, "{\"error\": %q}", err.Error()))
|
_, writeErr := fmt.Fprintf(w, "{\"error\": %q}", err.Error())
|
||||||
|
if writeErr != nil {
|
||||||
|
slog.Error(writeErr.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
func Map(obj any) map[string]any {
|
func Map(obj any) map[string]any {
|
||||||
result := make(map[string]any)
|
result := make(map[string]any)
|
||||||
val := reflect.ValueOf(obj)
|
val := reflect.ValueOf(obj)
|
||||||
if val.Kind() == reflect.Ptr {
|
if val.Kind() == reflect.Pointer {
|
||||||
val = val.Elem()
|
val = val.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
package validate
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -24,8 +26,51 @@ func init() {
|
|||||||
}
|
}
|
||||||
return name
|
return name
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Struct(s any) error {
|
// RegisterAlias for single/multuple tags
|
||||||
return validate.Struct(s)
|
func RegisterAlias(alias, tags string) {
|
||||||
|
validate.RegisterAlias(alias, tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterValidation(tagName string, fn validator.Func, callValidationEvenIfNull ...bool) {
|
||||||
|
validate.RegisterValidation(tagName, fn, callValidationEvenIfNull...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Struct validator
|
||||||
|
func Struct(s any) error {
|
||||||
|
err := validate.Struct(s)
|
||||||
|
if IsInvalidValidationError(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var valErrs validator.ValidationErrors
|
||||||
|
if !errors.As(err, &valErrs) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var sb strings.Builder
|
||||||
|
for _, err := range valErrs {
|
||||||
|
switch err.Tag() {
|
||||||
|
case "required":
|
||||||
|
sb.WriteString(fmt.Sprintf("%s: is required.\n", err.Field()))
|
||||||
|
case "email":
|
||||||
|
sb.WriteString(fmt.Sprintf("%s: is invalid.\n", err.Field()))
|
||||||
|
default:
|
||||||
|
sb.WriteString(fmt.Sprintf("%s: %q validation failed.\n", err.Field(), err.Tag()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New(sb.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map validator
|
||||||
|
func Map(data map[string]any, rules map[string]any) map[string]any {
|
||||||
|
return validate.ValidateMap(data, rules)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsInvalidValidationError(err error) bool {
|
||||||
|
var v *validator.InvalidValidationError
|
||||||
|
return errors.As(err, &v)
|
||||||
}
|
}
|
||||||
|
|||||||
30
validate/validate_test.go
Normal file
30
validate/validate_test.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestStruct(t *testing.T) {
|
||||||
|
type person struct {
|
||||||
|
FirstName string `validate:"required,max=10"`
|
||||||
|
LastName string `validate:"required"`
|
||||||
|
Email string `validate:"email"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var p *person
|
||||||
|
t.Log("check for nil value")
|
||||||
|
if err := Struct(p); err == nil {
|
||||||
|
t.Fatal("nil value must report and error")
|
||||||
|
} else {
|
||||||
|
t.Log(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
p = new(person)
|
||||||
|
t.Log("validation checks")
|
||||||
|
if err := Struct(p); err == nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Structure error string
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user