Skip to content

Commit ba5a13d

Browse files
committed
feat: adjustments in packages and other qol fixes
1 parent 37f0b12 commit ba5a13d

File tree

19 files changed

+208
-270
lines changed

19 files changed

+208
-270
lines changed

contract/session.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ type Session interface {
3333
// for extending a session's lifetime during active use.
3434
Extend(expiresAt time.Time)
3535

36-
// Regenerate creates a new session ID and associates it with this session, updating
37-
// the expiration time. This is commonly used after authentication to prevent session
36+
// Regenerate creates a new session ID and associates it with this session.
37+
// This is commonly used after authentication to prevent session
3838
// fixation attacks. It returns an error if the regeneration process fails.
39-
Regenerate(expiresAt time.Time) error
39+
Regenerate() error
4040

4141
// Clear removes all data from the session while maintaining the session itself.
4242
Clear()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package memory
1+
package cache
22

33
import (
44
"context"
@@ -15,7 +15,7 @@ type Memory struct {
1515
store *cache.Cache
1616
}
1717

18-
func New(expiration time.Duration, cleanup time.Duration) *Memory {
18+
func NewMemory(expiration time.Duration, cleanup time.Duration) *Memory {
1919
return &Memory{
2020
store: cache.New(expiration, cleanup),
2121
mux: sync.Mutex{},
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package redis
1+
package cache
22

33
import (
44
"context"
@@ -11,16 +11,16 @@ import (
1111
"github.com/studiolambda/cosmos/contract"
1212
)
1313

14-
type Options redis.Options
14+
type RedisOptions = redis.Options
1515

16-
type Client redis.Client
16+
type RedisClient redis.Client
1717

18-
func New(options *Options) *Client {
19-
return (*Client)(redis.NewClient((*redis.Options)(options)))
18+
func NewRedis(options *RedisOptions) *RedisClient {
19+
return (*RedisClient)(redis.NewClient((*redis.Options)(options)))
2020
}
2121

2222
// Get retrieves a value by key or returns contract.ErrNotFound if missing.
23-
func (c *Client) Get(ctx context.Context, key string) (any, error) {
23+
func (c *RedisClient) Get(ctx context.Context, key string) (any, error) {
2424
v, err := (*redis.Client)(c).Get(ctx, key).Result()
2525

2626
if errors.Is(err, redis.Nil) {
@@ -35,17 +35,17 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
3535
}
3636

3737
// Put sets a key with value and TTL.
38-
func (c *Client) Put(ctx context.Context, key string, value any, ttl time.Duration) error {
38+
func (c *RedisClient) Put(ctx context.Context, key string, value any, ttl time.Duration) error {
3939
return (*redis.Client)(c).Set(ctx, key, value, ttl).Err()
4040
}
4141

4242
// Delete removes a key.
43-
func (c *Client) Delete(ctx context.Context, key string) error {
43+
func (c *RedisClient) Delete(ctx context.Context, key string) error {
4444
return (*redis.Client)(c).Del(ctx, key).Err()
4545
}
4646

4747
// Has checks if key exists.
48-
func (c *Client) Has(ctx context.Context, key string) (bool, error) {
48+
func (c *RedisClient) Has(ctx context.Context, key string) (bool, error) {
4949
n, err := (*redis.Client)(c).Exists(ctx, key).Result()
5050

5151
if err != nil {
@@ -56,7 +56,7 @@ func (c *Client) Has(ctx context.Context, key string) (bool, error) {
5656
}
5757

5858
// Pull retrieves and deletes a key atomically.
59-
func (c *Client) Pull(ctx context.Context, key string) (v any, e error) {
59+
func (c *RedisClient) Pull(ctx context.Context, key string) (v any, e error) {
6060
encoded, err := (*redis.Client)(c).GetDel(ctx, key).Result()
6161

6262
if errors.Is(err, redis.Nil) {
@@ -75,22 +75,22 @@ func (c *Client) Pull(ctx context.Context, key string) (v any, e error) {
7575
}
7676

7777
// Forever stores a value indefinitely.
78-
func (c *Client) Forever(ctx context.Context, key string, value any) error {
78+
func (c *RedisClient) Forever(ctx context.Context, key string, value any) error {
7979
return c.Put(ctx, key, value, 0)
8080
}
8181

8282
// Increment increases a key's integer value by 'by'.
83-
func (c *Client) Increment(ctx context.Context, key string, by int64) (int64, error) {
83+
func (c *RedisClient) Increment(ctx context.Context, key string, by int64) (int64, error) {
8484
return (*redis.Client)(c).IncrBy(ctx, key, by).Result()
8585
}
8686

8787
// Decrement decreases a key's integer value by 'by'.
88-
func (c *Client) Decrement(ctx context.Context, key string, by int64) (int64, error) {
88+
func (c *RedisClient) Decrement(ctx context.Context, key string, by int64) (int64, error) {
8989
return (*redis.Client)(c).DecrBy(ctx, key, by).Result()
9090
}
9191

9292
// Remember gets or computes and caches a value with TTL.
93-
func (c *Client) Remember(ctx context.Context, key string, ttl time.Duration, compute func() (any, error)) (any, error) {
93+
func (c *RedisClient) Remember(ctx context.Context, key string, ttl time.Duration, compute func() (any, error)) (any, error) {
9494
val, err := c.Get(ctx, key)
9595

9696
if err == nil {
@@ -115,6 +115,6 @@ func (c *Client) Remember(ctx context.Context, key string, ttl time.Duration, co
115115
}
116116

117117
// RememberForever caches a computed value indefinitely.
118-
func (c *Client) RememberForever(ctx context.Context, key string, compute func() (any, error)) (any, error) {
118+
func (c *RedisClient) RememberForever(ctx context.Context, key string, compute func() (any, error)) (any, error) {
119119
return c.Remember(ctx, key, 0, compute)
120120
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package aes
1+
package crypto
22

33
import (
44
"crypto/aes"
@@ -8,25 +8,25 @@ import (
88
"io"
99
)
1010

11-
type Encrypter struct {
11+
type AES struct {
1212
key []byte
1313
}
1414

15-
var ErrMissmatchedNonceSize = errors.New("missmatched nonce size")
15+
var ErrMissmatchedAESNonceSize = errors.New("missmatched nonce size")
1616

17-
// NewEncrypter creates a new AES encrypter with the provided key.
17+
// NewAES creates a new AES encrypter with the provided key.
1818
// Key should be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256.
19-
func NewEncrypter(key []byte) (*Encrypter, error) {
19+
func NewAES(key []byte) (*AES, error) {
2020
if len(key) != 16 && len(key) != 24 && len(key) != 32 {
2121
return nil, aes.KeySizeError(len(key))
2222
}
2323

24-
return &Encrypter{key: key}, nil
24+
return &AES{key: key}, nil
2525
}
2626

2727
// Encrypt encrypts the plaintext using AES-GCM.
2828
// Returns ciphertext with nonce prepended.
29-
func (e *Encrypter) Encrypt(value []byte) ([]byte, error) {
29+
func (e *AES) Encrypt(value []byte) ([]byte, error) {
3030
block, err := aes.NewCipher(e.key)
3131

3232
if err != nil {
@@ -51,7 +51,7 @@ func (e *Encrypter) Encrypt(value []byte) ([]byte, error) {
5151

5252
// Decrypt decrypts the ciphertext using AES-GCM.
5353
// Expects nonce to be prepended to ciphertext.
54-
func (e *Encrypter) Decrypt(value []byte) ([]byte, error) {
54+
func (e *AES) Decrypt(value []byte) ([]byte, error) {
5555
block, err := aes.NewCipher(e.key)
5656

5757
if err != nil {
@@ -67,7 +67,7 @@ func (e *Encrypter) Decrypt(value []byte) ([]byte, error) {
6767
nonceSize := gcm.NonceSize()
6868

6969
if len(value) < nonceSize {
70-
return nil, ErrMissmatchedNonceSize
70+
return nil, ErrMissmatchedAESNonceSize
7171
}
7272

7373
nonce, ciphertext := value[:nonceSize], value[nonceSize:]
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package aes_test
1+
package crypto_test
22

33
import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7-
"github.com/studiolambda/cosmos/framework/crypto/aes"
7+
"github.com/studiolambda/cosmos/framework/crypto"
88
)
99

10-
func TestItCanCreateEncrypter(t *testing.T) {
10+
func TestItCanCreateAESEncrypter(t *testing.T) {
1111
key := []byte("12345678901234567890123456789012")
12-
_, err := aes.NewEncrypter(key)
12+
_, err := crypto.NewAES(key)
1313

1414
require.NoError(t, err)
1515
}
1616

17-
func TestItCanEncrypt(t *testing.T) {
17+
func TestItCanEncryptAES(t *testing.T) {
1818
key := []byte("12345678901234567890123456789012")
19-
e, err := aes.NewEncrypter(key)
19+
e, err := crypto.NewAES(key)
2020

2121
require.NoError(t, err)
2222

@@ -26,9 +26,9 @@ func TestItCanEncrypt(t *testing.T) {
2626
require.NoError(t, err)
2727
}
2828

29-
func TestItCanDecrypt(t *testing.T) {
29+
func TestItCanDecryptAES(t *testing.T) {
3030
key := []byte("12345678901234567890123456789012")
31-
e, _ := aes.NewEncrypter(key)
31+
e, _ := crypto.NewAES(key)
3232
plain := []byte("Hello, World!")
3333
cypher, _ := e.Encrypt(plain)
3434
res, err := e.Decrypt(cypher)
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chacha20
1+
package crypto
22

33
import (
44
"crypto/cipher"
@@ -9,27 +9,27 @@ import (
99
"golang.org/x/crypto/chacha20poly1305"
1010
)
1111

12-
type Encrypter struct {
12+
type ChaCha20 struct {
1313
aead cipher.AEAD
1414
}
1515

16-
var ErrMissmatchedNonceSize = errors.New("missmatched nonce size")
16+
var ErrMissmatchedChaCha20NonceSize = errors.New("missmatched nonce size")
1717

18-
// NewEncrypter creates a new ChaCha20-Poly1305 encrypter.
18+
// NewChaCha20 creates a new ChaCha20-Poly1305 encrypter.
1919
// Key must be exactly 32 bytes.
20-
func NewEncrypter(key []byte) (*Encrypter, error) {
20+
func NewChaCha20(key []byte) (*ChaCha20, error) {
2121
aead, err := chacha20poly1305.New(key)
2222

2323
if err != nil {
2424
return nil, err
2525
}
2626

27-
return &Encrypter{aead: aead}, nil
27+
return &ChaCha20{aead: aead}, nil
2828
}
2929

3030
// Encrypt encrypts the plaintext using ChaCha20-Poly1305.
3131
// Returns ciphertext with nonce prepended.
32-
func (e *Encrypter) Encrypt(value []byte) ([]byte, error) {
32+
func (e *ChaCha20) Encrypt(value []byte) ([]byte, error) {
3333
nonce := make([]byte, e.aead.NonceSize())
3434

3535
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
@@ -42,11 +42,11 @@ func (e *Encrypter) Encrypt(value []byte) ([]byte, error) {
4242

4343
// Decrypt decrypts the ciphertext using ChaCha20-Poly1305.
4444
// Expects nonce to be prepended to ciphertext.
45-
func (e *Encrypter) Decrypt(value []byte) ([]byte, error) {
45+
func (e *ChaCha20) Decrypt(value []byte) ([]byte, error) {
4646
nonceSize := e.aead.NonceSize()
4747

4848
if len(value) < nonceSize {
49-
return nil, ErrMissmatchedNonceSize
49+
return nil, ErrMissmatchedChaCha20NonceSize
5050
}
5151

5252
nonce, ciphertext := value[:nonceSize], value[nonceSize:]

framework/crypto/chacha20/encrypter_test.go renamed to framework/crypto/chacha20_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package chacha20_test
1+
package crypto_test
22

33
import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7-
"github.com/studiolambda/cosmos/framework/crypto/chacha20"
7+
"github.com/studiolambda/cosmos/framework/crypto"
88
)
99

10-
func TestItCanCreateEncrypter(t *testing.T) {
10+
func TestItCanCreateChaCha20Encrypter(t *testing.T) {
1111
key := []byte("12345678901234567890123456789012")
12-
_, err := chacha20.NewEncrypter(key)
12+
_, err := crypto.NewChaCha20(key)
1313

1414
require.NoError(t, err)
1515
}
1616

17-
func TestItCanEncrypt(t *testing.T) {
17+
func TestItCanEncryptChaCha20(t *testing.T) {
1818
key := []byte("12345678901234567890123456789012")
19-
e, err := chacha20.NewEncrypter(key)
19+
e, err := crypto.NewChaCha20(key)
2020

2121
require.NoError(t, err)
2222

@@ -26,9 +26,9 @@ func TestItCanEncrypt(t *testing.T) {
2626
require.NoError(t, err)
2727
}
2828

29-
func TestItCanDecrypt(t *testing.T) {
29+
func TestItCanDecryptChaCha20(t *testing.T) {
3030
key := []byte("12345678901234567890123456789012")
31-
e, _ := chacha20.NewEncrypter(key)
31+
e, _ := crypto.NewChaCha20(key)
3232
plain := []byte("Hello, World!")
3333
cypher, _ := e.Encrypt(plain)
3434
res, err := e.Decrypt(cypher)

0 commit comments

Comments
 (0)