-
Notifications
You must be signed in to change notification settings - Fork 8
fix: added fix for https://github.com/highflame-ai/zeroid/issues/15 #76
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
5 commits
Select commit
Hold shift + click to select a range
1971d87
fix: added fix for https://github.com/highflame-ai/zeroid/issues/15
rsharath 4e34e46
fix: updated with code review suggestions
rsharath 9bed0c6
Merge branch 'main' into issue15
rsharath 7541f77
fix: deleted file
rsharath b72dfd5
review: document replay-window race; drop unrelated mcp-scopes doc
rsharath 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package domain | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/uptrace/bun" | ||
| ) | ||
|
|
||
| // AuthCode tracks a consumed authorization code for single-use enforcement | ||
| // per RFC 6749 §4.1.2. Auth codes are stateless HS256 JWTs; this record is | ||
| // created on first exchange and used to detect replays. | ||
| type AuthCode struct { | ||
| bun.BaseModel `bun:"table:auth_codes,alias:ac"` | ||
|
|
||
| JTI string `bun:"jti,pk,type:varchar(255)" json:"jti"` | ||
| ClientID string `bun:"client_id,type:varchar(255)" json:"client_id"` | ||
| AccountID string `bun:"account_id,type:varchar(255)" json:"account_id"` | ||
| ProjectID string `bun:"project_id,type:varchar(255)" json:"project_id"` | ||
| CredentialJTI *string `bun:"credential_jti,type:varchar(255)" json:"credential_jti,omitempty"` | ||
| RefreshFamilyID *string `bun:"refresh_family_id,type:uuid" json:"refresh_family_id,omitempty"` | ||
| ConsumedAt time.Time `bun:"consumed_at,nullzero,notnull,default:current_timestamp" json:"consumed_at"` | ||
| ExpiresAt time.Time `bun:"expires_at,notnull" json:"expires_at"` | ||
| } |
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,75 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/uptrace/bun" | ||
|
|
||
| "github.com/highflame-ai/zeroid/domain" | ||
| ) | ||
|
|
||
| // AuthCodeRepository handles persistence for consumed authorization codes. | ||
| type AuthCodeRepository struct { | ||
| db *bun.DB | ||
| } | ||
|
|
||
| // NewAuthCodeRepository creates a new AuthCodeRepository. | ||
| func NewAuthCodeRepository(db *bun.DB) *AuthCodeRepository { | ||
| return &AuthCodeRepository{db: db} | ||
| } | ||
|
|
||
| // Consume atomically records an auth code as consumed. | ||
| // Returns true if this is the first consumption (INSERT succeeded). | ||
| // Returns false if the code was already consumed (conflict on PK). | ||
| func (r *AuthCodeRepository) Consume(ctx context.Context, code *domain.AuthCode) (bool, error) { | ||
| res, err := r.db.NewInsert(). | ||
| Model(code). | ||
| On("CONFLICT (jti) DO NOTHING"). | ||
| Exec(ctx) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to consume auth code: %w", err) | ||
| } | ||
|
|
||
| rows, err := res.RowsAffected() | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to check auth code insert result: %w", err) | ||
| } | ||
|
|
||
| return rows > 0, nil | ||
| } | ||
|
|
||
| // GetByJTI retrieves a consumed auth code record by its JTI. | ||
| // Used during replay detection to find the credential and refresh token | ||
| // family that need to be revoked. | ||
| func (r *AuthCodeRepository) GetByJTI(ctx context.Context, jti string) (*domain.AuthCode, error) { | ||
| code := &domain.AuthCode{} | ||
| err := r.db.NewSelect().Model(code). | ||
| Where("jti = ?", jti). | ||
| Scan(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get auth code by jti: %w", err) | ||
| } | ||
| return code, nil | ||
| } | ||
|
|
||
| // UpdateTokenInfo stores the credential JTI and refresh token family ID | ||
| // after successful token issuance. These are needed to revoke the tokens | ||
| // if a replay is detected later. | ||
| func (r *AuthCodeRepository) UpdateTokenInfo(ctx context.Context, jti, credentialJTI, refreshFamilyID string) error { | ||
| q := r.db.NewUpdate(). | ||
| Model((*domain.AuthCode)(nil)). | ||
| Set("credential_jti = ?", credentialJTI). | ||
| Where("jti = ?", jti) | ||
|
|
||
| if refreshFamilyID != "" { | ||
| q = q.Set("refresh_family_id = ?::uuid", refreshFamilyID) | ||
| } | ||
|
|
||
| _, err := q.Exec(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to update auth code token info: %w", err) | ||
| } | ||
| return 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
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,2 @@ | ||
| -- 008_auth_codes.down.sql | ||
| DROP TABLE IF EXISTS auth_codes; |
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,20 @@ | ||
| -- 008_auth_codes.up.sql | ||
| -- Tracks consumed authorization codes to enforce single-use per RFC 6749 §4.1.2. | ||
| -- Auth codes are stateless HS256 JWTs; this table records consumption so replays | ||
| -- are rejected. The credential_jti and refresh_family_id columns enable token | ||
| -- revocation when a replay is detected (RFC 6749: "SHOULD revoke all tokens | ||
| -- previously issued based on that authorization code"). | ||
|
|
||
| CREATE TABLE IF NOT EXISTS auth_codes ( | ||
| jti VARCHAR(255) PRIMARY KEY, | ||
| client_id VARCHAR(255) NOT NULL, | ||
| account_id VARCHAR(255) NOT NULL, | ||
| project_id VARCHAR(255) NOT NULL DEFAULT '', | ||
| credential_jti VARCHAR(255), | ||
| refresh_family_id UUID, | ||
| consumed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| expires_at TIMESTAMPTZ NOT NULL | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_auth_codes_expires_at | ||
| ON auth_codes (expires_at); |
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.
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.