-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow creating limited use keys by setting 'remaining'
- Loading branch information
Showing
25 changed files
with
511 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,52 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
// Decrement the `remaining` field and return the new value | ||
// The returned value is the number of remaining verifications after the current one. | ||
// This means the returned value can be negative, for example when the remaining is 0 and we call this function. | ||
func (db *database) DecrementRemainingKeyUsage(ctx context.Context, keyId string) (int64, error) { | ||
tx, err := db.write().BeginTx(ctx, nil) | ||
if err != nil { | ||
return 0, fmt.Errorf("unable to start transaction: %w", err) | ||
} | ||
|
||
_, err = tx.Exec(`UPDATE unkey.keys SET remaining_requests = remaining_requests - 1 WHERE id = ?`, keyId) | ||
if err != nil { | ||
rollbackErr := tx.Rollback() | ||
if rollbackErr != nil { | ||
return 0, fmt.Errorf("unable to roll back: %w", rollbackErr) | ||
} | ||
return 0, fmt.Errorf("unable to decrement: %w", err) | ||
} | ||
|
||
row := tx.QueryRow(`SELECT remaining_requests FROM unkey.keys WHERE id = ?`, keyId) | ||
if err != nil { | ||
rollbackErr := tx.Rollback() | ||
if rollbackErr != nil { | ||
return 0, fmt.Errorf("unable to roll back: %w", rollbackErr) | ||
} | ||
return 0, fmt.Errorf("unable to query: %w", err) | ||
} | ||
var remainingAfter sql.NullInt64 | ||
err = row.Scan(&remainingAfter) | ||
|
||
if err != nil { | ||
return 0, fmt.Errorf("unable to scan result: %w", err) | ||
} | ||
if !remainingAfter.Valid { | ||
return 0, fmt.Errorf("this key did not have a remaining config") | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return 0, fmt.Errorf("unable to commit transaction: %w", err) | ||
} | ||
|
||
return remainingAfter.Int64, err | ||
|
||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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.