Repository navigation
feat(node)!: [high] support JWT auth token revocation #5364
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
Open
jcstein
wants to merge
6
commits into
celestiaorg:main
Choose a base branch
from
jcstein:jcstein/4816-make-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
383d0f3
feat(node)!: [high] support JWT auth token revocation
jcstein 81694de
fix(deps): [low] tidy Tastora revocation test checksums
jcstein 0226fcb
Merge branch 'main' into jcstein/4816-make-pr
chatton 2e8bb49
refactor(auth): [high] parse JWTs once for persistent token revocation
jcstein 9174ce7
fix(ci): [low] serialize coverage tests to avoid port collisions
jcstein 255bf31
ci: retry transient Go proxy download failures
jcstein 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package authtoken | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/cristalhq/jwt/v5" | ||
| "github.com/filecoin-project/go-jsonrpc/auth" | ||
| "github.com/ipfs/go-datastore" | ||
| ) | ||
|
|
||
| // ErrRevoked indicates that a token has been revoked. | ||
| var ErrRevoked = errors.New("token revoked") | ||
|
|
||
| // Verifier checks JWT signatures, expiry and persistent revocations. | ||
| // Active requests are canceled when their token is revoked. | ||
| type Verifier struct { | ||
| verifier jwt.Verifier | ||
| store datastore.Datastore | ||
| mu sync.Mutex | ||
| sessions map[datastore.Key]map[context.Context]context.CancelFunc | ||
| } | ||
|
|
||
| // NewVerifier uses the node's datastore to persist revocations across restarts. | ||
| func NewVerifier(verifier jwt.Verifier, store datastore.Datastore) *Verifier { | ||
| return &Verifier{ | ||
| verifier: verifier, | ||
| store: store, | ||
| sessions: make(map[datastore.Key]map[context.Context]context.CancelFunc), | ||
| } | ||
| } | ||
|
|
||
| // Verify returns the permissions of a valid, unrevoked token. | ||
| func (v *Verifier) Verify(ctx context.Context, token string) ([]auth.Permission, error) { | ||
| tk, err := jwt.Parse([]byte(token), v.verifier) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return v.verify(ctx, tk) | ||
| } | ||
|
|
||
| // VerifyAndRegister verifies a token and cancels the request when it is revoked. | ||
| // Call the returned function when the request finishes. The context must be unique per request. | ||
| func (v *Verifier) VerifyAndRegister( | ||
| ctx context.Context, token string, cancel context.CancelFunc, | ||
| ) ([]auth.Permission, func(), error) { | ||
| tk, err := jwt.Parse([]byte(token), v.verifier) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| v.mu.Lock() | ||
| defer v.mu.Unlock() | ||
| // Checking under the same lock as Revoke closes the authentication/registration race. | ||
| permissions, err := v.verify(ctx, tk) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| key := tokenKey(tk) | ||
| if v.sessions[key] == nil { | ||
| v.sessions[key] = make(map[context.Context]context.CancelFunc) | ||
| } | ||
| v.sessions[key][ctx] = cancel | ||
| return permissions, func() { | ||
| v.mu.Lock() | ||
| defer v.mu.Unlock() | ||
| delete(v.sessions[key], ctx) | ||
| if len(v.sessions[key]) == 0 { | ||
| delete(v.sessions, key) | ||
| } | ||
| }, nil | ||
| } | ||
|
|
||
| // Revoke permanently invalidates a token signed by this node, including tokens | ||
| // issued before nonces were introduced. Repeated revocations are safe. | ||
| func (v *Verifier) Revoke(ctx context.Context, token string) error { | ||
| tk, err := jwt.Parse([]byte(token), v.verifier) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| key := tokenKey(tk) | ||
| v.mu.Lock() | ||
| defer v.mu.Unlock() | ||
| if err := v.store.Put(ctx, key, nil); err != nil { | ||
| return fmt.Errorf("persist token revocation: %w", err) | ||
| } | ||
| err = v.store.Sync(ctx, key) | ||
| // Even if syncing fails, reject the token and terminate its active sessions. | ||
| for _, cancel := range v.sessions[key] { | ||
| cancel() | ||
| } | ||
| delete(v.sessions, key) | ||
| if err != nil { | ||
| return fmt.Errorf("sync token revocation: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (v *Verifier) verify(ctx context.Context, tk *jwt.Token) ([]auth.Permission, error) { | ||
| permissions, err := extractPermissions(tk) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| revoked, err := v.store.Has(ctx, tokenKey(tk)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("check token revocation: %w", err) | ||
| } | ||
| if revoked { | ||
| return nil, ErrRevoked | ||
| } | ||
| return permissions, nil | ||
| } | ||
|
|
||
| func tokenKey(tk *jwt.Token) datastore.Key { | ||
| // Hash decoded signatures so alternate base64 encodings cannot bypass revocation. | ||
| digest := sha256.Sum256(tk.Signature()) | ||
| return datastore.NewKey("/auth/revoked/" + hex.EncodeToString(digest[:])) | ||
| } |
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.