-
Notifications
You must be signed in to change notification settings - Fork 563
feat: introduce v2 refresh token algorithm #2216
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e5904ae
feat: introduce v2 refresh token algorithm
hf 55a4c12
add majority of tests
hf cf13bb4
fix TestSafeIntegers
hf e4be94d
adjust concurrency down
hf e95fcaa
actually fix TestSafeIntegers
hf 0ad7fa5
add tests for invalid tokens
hf c230d8a
bump up coverage, change slightly always allow implementation
hf 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
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,155 @@ | ||
| package crypto | ||
|
|
||
| import ( | ||
| "crypto/hmac" | ||
| "crypto/rand" | ||
| "crypto/sha256" | ||
| "crypto/subtle" | ||
| "encoding/base64" | ||
| "encoding/binary" | ||
| "errors" | ||
| "math" | ||
|
|
||
| "github.com/gofrs/uuid" | ||
| ) | ||
|
|
||
| func GenerateRefreshTokenHmacKey() []byte { | ||
| key := make([]byte, 32) | ||
| must(rand.Read(key)) | ||
|
|
||
| return key | ||
| } | ||
|
|
||
| const refreshTokenChecksumLength = 4 | ||
| const refreshTokenSignatureLength = 16 | ||
| const minRefreshTokenLength = 1 + 16 + 1 + refreshTokenSignatureLength + refreshTokenChecksumLength | ||
| const maxRefreshTokenLength = minRefreshTokenLength + 8 | ||
|
|
||
| // RefreshToken is an object that encodes a cryptographically authenticated | ||
| // (signed) message containing a version, session ID and monotonically | ||
| // increasing non-negative counter. | ||
| // | ||
| // The signature is a truncated (first 128 bits) of HMAC-SHA-256, which saves | ||
| // on encoded length without sacrificing security. The checksum of 4 bytes at | ||
| // the end is to lessen the load on the server with invalid strings (those that | ||
| // are not likely to be a proper refresh token). | ||
| type RefreshToken struct { | ||
| Raw []byte | ||
|
|
||
| Version byte | ||
| SessionID uuid.UUID | ||
| Counter int64 | ||
| Signature []byte | ||
| } | ||
|
|
||
| func (RefreshToken) TableName() string { | ||
| panic("crypto.RefreshToken is not meant to be saved in the database") | ||
| } | ||
|
|
||
| func (r *RefreshToken) CheckSignature(hmacSha256Key []byte) bool { | ||
| bytes := r.Raw[:len(r.Raw)-refreshTokenSignatureLength-refreshTokenChecksumLength] | ||
|
|
||
| h := hmac.New(sha256.New, hmacSha256Key) | ||
| h.Write(bytes) | ||
| signature := h.Sum(nil)[:refreshTokenSignatureLength] | ||
|
|
||
| return hmac.Equal(signature, r.Signature) | ||
| } | ||
|
|
||
| func (r *RefreshToken) Encode(hmacSha256Key []byte) string { | ||
| result := make([]byte, 0, maxRefreshTokenLength) | ||
|
|
||
| result = append(result, 0) | ||
| result = append(result, r.SessionID.Bytes()...) | ||
| result = binary.AppendUvarint(result, safeUint64(r.Counter)) | ||
|
|
||
| // Note on truncating the HMAC-SHA-256 output: | ||
| // This does not impact security as the brute-force space is 2^128 and | ||
| // the collision space is 2^64, both unattainable in practice. | ||
|
|
||
| h := hmac.New(sha256.New, hmacSha256Key) | ||
| h.Write(result) | ||
| signature := h.Sum(nil)[:refreshTokenSignatureLength] | ||
|
|
||
| result = append(result, signature...) | ||
|
|
||
| checksum := sha256.Sum256(result) | ||
| result = append(result, checksum[:refreshTokenChecksumLength]...) | ||
|
|
||
| r.Version = 0 | ||
| r.Raw = result | ||
| r.Signature = signature | ||
|
|
||
| return base64.RawURLEncoding.EncodeToString(result) | ||
| } | ||
|
|
||
| var ( | ||
| ErrRefreshTokenLength = errors.New("crypto: refresh token length is not valid") | ||
| ErrRefreshTokenUnknownVersion = errors.New("crypto: refresh token version is not 0") | ||
| ErrRefreshTokenChecksumInvalid = errors.New("crypto: refresh token checksum is not valid") | ||
| ErrRefreshTokenCounterInvalid = errors.New("crypto: refresh token's counter is not valid") | ||
| ) | ||
|
|
||
| func safeInt64(v uint64) int64 { | ||
| if v > math.MaxInt64 { | ||
| return math.MaxInt64 | ||
| } | ||
|
|
||
| return int64(v) | ||
| } | ||
|
|
||
| func safeUint64(v int64) uint64 { | ||
| if v < 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| return uint64(v) | ||
| } | ||
|
|
||
| func ParseRefreshToken(token string) (*RefreshToken, error) { | ||
| bytes, err := base64.RawURLEncoding.DecodeString(token) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(bytes) < minRefreshTokenLength { | ||
| return nil, ErrRefreshTokenLength | ||
| } | ||
|
|
||
| if bytes[0] != 0 { | ||
| return nil, ErrRefreshTokenUnknownVersion | ||
| } | ||
|
|
||
| parseFrom := bytes[1 : len(bytes)-refreshTokenChecksumLength] | ||
|
|
||
| checksum256 := sha256.Sum256(bytes[:len(bytes)-refreshTokenChecksumLength]) | ||
| if subtle.ConstantTimeCompare(checksum256[:refreshTokenChecksumLength], bytes[len(bytes)-refreshTokenChecksumLength:]) != 1 { | ||
| return nil, ErrRefreshTokenChecksumInvalid | ||
| } | ||
|
|
||
| sessionID := uuid.FromBytesOrNil(parseFrom[0:16]) | ||
|
|
||
| parseFrom = parseFrom[16:] | ||
|
|
||
| counter, counterBytes := binary.Uvarint(parseFrom) | ||
| if counterBytes <= 0 { | ||
| return nil, ErrRefreshTokenCounterInvalid | ||
| } | ||
|
|
||
| parseFrom = parseFrom[counterBytes:] | ||
|
|
||
| if len(parseFrom) != 16 { | ||
| return nil, ErrRefreshTokenLength | ||
| } | ||
|
|
||
| signature := parseFrom | ||
|
|
||
| return &RefreshToken{ | ||
| Raw: bytes, | ||
|
|
||
| Version: 0, | ||
| SessionID: sessionID, | ||
| Counter: safeInt64(counter), | ||
hf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Signature: signature, | ||
| }, nil | ||
| } | ||
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.