-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 feat: session in header && recover middleware && encrypt session da…
…ta (#1004)
- Loading branch information
1 parent
cb8f59f
commit 8ae7c56
Showing
12 changed files
with
183 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package config | ||
|
||
import m "github.com/garrettladley/mattress" | ||
|
||
type SessionSettings struct { | ||
Redis RedisSettings | ||
PassPhrase *m.Secret[string] | ||
} | ||
|
||
type intermediateSessionSettings struct { | ||
PassPhrase string `env:"PASS_PHRASE"` | ||
} | ||
|
||
func (i *intermediateSessionSettings) into(redis RedisSettings) (*SessionSettings, error) { | ||
passPhrase, err := m.NewSecret(i.PassPhrase) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SessionSettings{ | ||
Redis: redis, | ||
PassPhrase: passPhrase, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package crypt | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func Encrypt(data string, passphrase string) (string, error) { | ||
block, err := createCipherBlock(passphrase) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
plaintext := []byte(data) | ||
if len(plaintext) > 1028 { | ||
return "", fmt.Errorf("plaintext too long") | ||
} | ||
|
||
ciphertext := make([]byte, aes.BlockSize+len(plaintext)) | ||
iv := ciphertext[:aes.BlockSize] | ||
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | ||
return "", err | ||
} | ||
|
||
stream := cipher.NewCFBEncrypter(block, iv) | ||
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext) | ||
|
||
return hex.EncodeToString(ciphertext), nil | ||
} | ||
|
||
func Decrypt(encryptedData, passphrase string) (string, error) { | ||
block, err := createCipherBlock(passphrase) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
ciphertext, _ := hex.DecodeString(encryptedData) | ||
if len(ciphertext) < aes.BlockSize { | ||
return "", fmt.Errorf("ciphertext too short") | ||
} | ||
|
||
iv := ciphertext[:aes.BlockSize] | ||
ciphertext = ciphertext[aes.BlockSize:] | ||
|
||
stream := cipher.NewCFBDecrypter(block, iv) | ||
stream.XORKeyStream(ciphertext, ciphertext) | ||
|
||
return string(ciphertext), nil | ||
} | ||
|
||
func createCipherBlock(key string) (cipher.Block, error) { | ||
hash := sha256.Sum256([]byte(key)) | ||
block, err := aes.NewCipher(hash[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return block, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package crypt | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GenerateNU/sac/backend/integrations/oauth/crypt" | ||
) | ||
|
||
func TestCryptEncryptDecrypt(t *testing.T) { | ||
t.Parallel() | ||
|
||
data := "test data" | ||
passphrase := "test passphrase" | ||
|
||
encrypted, err := crypt.Encrypt(data, passphrase) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
decrypted, err := crypt.Decrypt(encrypted, passphrase) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if decrypted != data { | ||
t.Fatalf("expected %s, got %s", data, decrypted) | ||
} | ||
} |
Oops, something went wrong.