-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat: Use encrypted cookie to store OAuth2 state nonce (instead of redis) #8241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd0e1b8
feat: Use encrypted cookie to store OAuth2 state nonce (instead of re…
alexmt 4e134db
apply reviewer notes: switch to sha256 and move encrypt/decrypt metho…
alexmt 6780ccc
apply reviewer notes: check data size during decrypting; check empty …
alexmt a398353
apply reviewer notes: get rid of unnecessary hashing
alexmt ba1f8d6
apply reviewer notes: use server signature as a passphrase to generat…
alexmt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
| package crypto | ||
|
|
||
| import ( | ||
| "crypto/aes" | ||
| "crypto/cipher" | ||
| "crypto/rand" | ||
| "errors" | ||
| "io" | ||
| ) | ||
|
|
||
| // Encrypt encrypts the given data with the given passphrase. | ||
| func Encrypt(data []byte, key []byte) ([]byte, error) { | ||
| block, err := aes.NewCipher(key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| gcm, err := cipher.NewGCM(block) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| nonce := make([]byte, gcm.NonceSize()) | ||
| if _, err = io.ReadFull(rand.Reader, nonce); err != nil { | ||
| return nil, err | ||
| } | ||
| ciphertext := gcm.Seal(nonce, nonce, data, nil) | ||
| return ciphertext, nil | ||
| } | ||
|
|
||
| // Decrypt decrypts the given data using the given passphrase. | ||
| func Decrypt(data []byte, key []byte) ([]byte, error) { | ||
| block, err := aes.NewCipher(key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| gcm, err := cipher.NewGCM(block) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| nonceSize := gcm.NonceSize() | ||
| if len(data) < nonceSize { | ||
| return nil, errors.New("data length is less than nonce size") | ||
| } | ||
| nonce, ciphertext := data[:nonceSize], data[nonceSize:] | ||
| plaintext, err := gcm.Open(nil, nonce, ciphertext, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return plaintext, nil | ||
| } | ||
This file contains hidden or 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,43 @@ | ||
| package crypto | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func newKey() ([]byte, error) { | ||
| b := make([]byte, 32) | ||
| _, err := rand.Read(b) | ||
| if err != nil { | ||
| b = nil | ||
| } | ||
| return b, err | ||
| } | ||
|
|
||
| func TestEncryptDecrypt_Successful(t *testing.T) { | ||
| key, err := newKey() | ||
| require.NoError(t, err) | ||
| encrypted, err := Encrypt([]byte("test"), key) | ||
| require.NoError(t, err) | ||
|
|
||
| decrypted, err := Decrypt(encrypted, key) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, "test", string(decrypted)) | ||
| } | ||
|
|
||
| func TestEncryptDecrypt_Failed(t *testing.T) { | ||
| key, err := newKey() | ||
| require.NoError(t, err) | ||
| encrypted, err := Encrypt([]byte("test"), key) | ||
| require.NoError(t, err) | ||
|
|
||
| wrongKey, err := newKey() | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = Decrypt(encrypted, wrongKey) | ||
| assert.Error(t, err) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.