-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: temp access tokens #3603
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
feat: temp access tokens #3603
Changes from all commits
Commits
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,57 @@ | ||
| package tables | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/maximhq/bifrost/framework/encrypt" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // TempToken is a short-lived, narrow-scope credential that authorizes access | ||
| // to a specific set of endpoints without requiring dashboard login. | ||
| // | ||
| // Each row is bound to a (scope, resource_id) pair: the scope names a set of | ||
| // allowed routes (registered in framework/temptoken), and the resource_id ties | ||
| // the token to the specific resource those routes act on (e.g. the OAuth flow | ||
| // ID for the mcp_auth scope). The plaintext token is hashed for lookup and | ||
| // encrypted at rest, matching the SessionsTable pattern. | ||
| type TempToken struct { | ||
| ID string `gorm:"type:varchar(255);primaryKey" json:"id"` // UUID | ||
| Token string `gorm:"type:text;not null" json:"-"` // encrypted at rest when encryption is enabled | ||
| TokenHash string `gorm:"type:varchar(64);uniqueIndex:idx_temp_token_hash" json:"-"` // SHA-256 of plaintext for lookup | ||
| Scope string `gorm:"type:varchar(64);index;not null" json:"scope"` // e.g. "mcp_auth" — keys into the scope registry | ||
| ResourceID string `gorm:"type:text;index" json:"resource_id,omitempty"` // resource the scope binds to (semantics per scope); indexed for lifecycle-driven deletes | ||
| ExpiresAt time.Time `gorm:"index;not null" json:"expires_at"` | ||
| CreatedAt time.Time `gorm:"index;not null" json:"created_at"` | ||
| UpdatedAt time.Time `gorm:"index;not null" json:"updated_at"` | ||
| EncryptionStatus string `gorm:"type:varchar(20);default:'plain_text'" json:"-"` | ||
| } | ||
|
|
||
| // TableName sets the table name for the model. | ||
| func (TempToken) TableName() string { return "temp_tokens" } | ||
|
|
||
| // BeforeSave hashes the plaintext for lookup and encrypts it for storage. | ||
| // Hash must be computed before encryption so it always covers the plaintext. | ||
| func (t *TempToken) BeforeSave(tx *gorm.DB) error { | ||
| if t.Token != "" { | ||
| t.TokenHash = encrypt.HashSHA256(t.Token) | ||
| } | ||
| if encrypt.IsEnabled() && t.Token != "" { | ||
| if err := encryptString(&t.Token); err != nil { | ||
| return fmt.Errorf("failed to encrypt temp token: %w", err) | ||
| } | ||
| t.EncryptionStatus = EncryptionStatusEncrypted | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // AfterFind decrypts the stored plaintext when encryption is in effect. | ||
| func (t *TempToken) AfterFind(tx *gorm.DB) error { | ||
| if t.EncryptionStatus == EncryptionStatusEncrypted { | ||
| if err := decryptString(&t.Token); err != nil { | ||
| return fmt.Errorf("failed to decrypt temp token: %w", err) | ||
| } | ||
| } | ||
| return nil | ||
|
roroghost17 marked this conversation as resolved.
|
||
| } | ||
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.