-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Support v2 storage and send notification for each token #4
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1868224
Log only first 8B of commit and first 10B of date on startup
kenstir 8f342f2
Add TokenStore and some tests
kenstir 8ae6aa3
Add NewTokenStoreFromString
kenstir 86b8cbb
Handle either string or JSON as token param
kenstir b9aa4b0
Refactor and rename resultAndCodeFromError
kenstir 7ba4d46
Use const for cutoff time
kenstir 0e59580
Tweak commentary
kenstir dcb86e8
Trim ws before unmarshal per copilot review
kenstir a7167f1
Avoid rune() for test strings
kenstir 81c62d8
Send only 1 http response for multiple tokens
kenstir 0c5654b
Changed the names used for serialization
kenstir 3258197
Change TokenEntry to use unix time
kenstir fce0ca9
Switch to base64url-encoded storage
kenstir ae8c3c6
Switch to RawURLEncoding to omit padding
kenstir 9ea24c1
Add v2 note to README
kenstir d96d1c8
Tweak commentary
kenstir dd165db
Add TestEncodingIsCompatible
kenstir 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,86 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| const MaxEntries = 3 | ||
|
|
||
| // decoder/encoder for v2 tokens, declared here so they can be tested as compatible | ||
| var V2DecodeString = base64.RawURLEncoding.DecodeString | ||
| var V2EncodeString = base64.RawURLEncoding.EncodeToString | ||
|
|
||
| // prefix on all v2 base64url-encoded tokens, which when decoded is `{"entries":[` | ||
| const V2EncodedTokenPrefix = "eyJlbnRyaWVzIjpb" | ||
|
|
||
| type TokenEntry struct { | ||
| Token string `json:"token"` | ||
| AddedAt int64 `json:"added_at"` | ||
| } | ||
|
|
||
| type TokenStore struct { | ||
| Entries []TokenEntry `json:"entries"` | ||
| } | ||
|
|
||
| func NewTokenStore() *TokenStore { | ||
| return &TokenStore{ | ||
| Entries: make([]TokenEntry, 0, MaxEntries), | ||
| } | ||
| } | ||
|
|
||
| func NewTokenStoreFromString(str string) *TokenStore { | ||
| ts := NewTokenStore() | ||
| ts.FromString(str) | ||
| return ts | ||
| } | ||
|
|
||
| func (ts *TokenStore) AddToken(token string) { | ||
| ts.AddTokenEntry(TokenEntry{ | ||
| Token: token, | ||
| AddedAt: time.Now().Unix(), | ||
| }) | ||
| } | ||
|
|
||
| func (ts *TokenStore) AddTokenEntry(entry TokenEntry) { | ||
| if len(ts.Entries) >= MaxEntries { | ||
| ts.Entries = ts.Entries[1:] | ||
| } | ||
| ts.Entries = append(ts.Entries, entry) | ||
| } | ||
|
|
||
| func (ts *TokenStore) FindToken(token string) *TokenEntry { | ||
| for i := len(ts.Entries) - 1; i >= 0; i-- { | ||
| if ts.Entries[i].Token == token { | ||
| return &ts.Entries[i] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (ts *TokenStore) ToJSON() ([]byte, error) { | ||
| return json.Marshal(ts) | ||
| } | ||
|
|
||
| func (ts *TokenStore) FromJSON(data []byte) error { | ||
| return json.Unmarshal(data, ts) | ||
|
kenstir marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // FromString creates a TokenStore from a string, either a plain PN token (v1) or a base64url-encoded JSON TS object (v2). | ||
| func (ts *TokenStore) FromString(str string) { | ||
| // if it looks like a v2 encoded object, try to parse it | ||
| if strings.HasPrefix(str, V2EncodedTokenPrefix) { | ||
| decoded, err := V2DecodeString(str) | ||
|
kenstir marked this conversation as resolved.
|
||
| if err == nil { | ||
| err = ts.FromJSON(decoded) | ||
| if err == nil { | ||
|
kenstir marked this conversation as resolved.
|
||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // treat it as a single token string | ||
| ts.AddToken(str) | ||
| } | ||
Oops, something went wrong.
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.