mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-10-22 04:32:02 +02:00
Improvements to Session State code (#536)
* Drop SessionStateJSON wrapper * Use EncrpytInto/DecryptInto to reduce sessionstate Co-authored-by: Henry Jenkins <henry@henryjenkins.name>
This commit is contained in:
co-authored by
Henry Jenkins
parent
6a88da7f7a
commit
f7b28cb1d3
@@ -156,3 +156,30 @@ func (c *Cipher) Decrypt(s string) (string, error) {
|
||||
|
||||
return string(encrypted), nil
|
||||
}
|
||||
|
||||
// EncryptInto encrypts the value and stores it back in the string pointer
|
||||
func (c *Cipher) EncryptInto(s *string) error {
|
||||
return into(c.Encrypt, s)
|
||||
}
|
||||
|
||||
// DecryptInto decrypts the value and stores it back in the string pointer
|
||||
func (c *Cipher) DecryptInto(s *string) error {
|
||||
return into(c.Decrypt, s)
|
||||
}
|
||||
|
||||
// codecFunc is a function that takes a string and encodes/decodes it
|
||||
type codecFunc func(string) (string, error)
|
||||
|
||||
func into(f codecFunc, s *string) error {
|
||||
// Do not encrypt/decrypt nil or empty strings
|
||||
if s == nil || *s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
d, err := f(*s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*s = d
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -133,3 +133,25 @@ func TestEncodeAndDecodeAccessTokenB64(t *testing.T) {
|
||||
assert.NotEqual(t, token, encoded)
|
||||
assert.Equal(t, token, decoded)
|
||||
}
|
||||
|
||||
func TestEncodeIntoAndDecodeIntoAccessToken(t *testing.T) {
|
||||
const secret = "0123456789abcdefghijklmnopqrstuv"
|
||||
c, err := NewCipher([]byte(secret))
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
token := "my access token"
|
||||
originalToken := token
|
||||
|
||||
assert.Equal(t, nil, c.EncryptInto(&token))
|
||||
assert.NotEqual(t, originalToken, token)
|
||||
|
||||
assert.Equal(t, nil, c.DecryptInto(&token))
|
||||
assert.Equal(t, originalToken, token)
|
||||
|
||||
// Check no errors with empty or nil strings
|
||||
empty := ""
|
||||
assert.Equal(t, nil, c.EncryptInto(&empty))
|
||||
assert.Equal(t, nil, c.DecryptInto(&empty))
|
||||
assert.Equal(t, nil, c.EncryptInto(nil))
|
||||
assert.Equal(t, nil, c.DecryptInto(nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user