Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions internal/auth/token_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ func MaskToken(token string) string {

// GetStoredToken reads the stored UAT for a given (appId, userOpenId) pair.
func GetStoredToken(appId, userOpenId string) *StoredUAToken {
token, _ := readStoredToken(appId, userOpenId)
return token
}

func readStoredToken(appId, userOpenId string) (*StoredUAToken, error) {
jsonStr, err := keychain.Get(keychain.LarkCliService, accountKey(appId, userOpenId))
if err != nil || jsonStr == "" {
return nil
if err != nil {
return nil, err
}
if jsonStr == "" {
return nil, nil
}
var token StoredUAToken
if err := json.Unmarshal([]byte(jsonStr), &token); err != nil {
return nil
return nil, err
}
return &token
return &token, nil
}

// SetStoredToken persists a UAT.
Expand All @@ -66,6 +74,54 @@ func RemoveStoredToken(appId, userOpenId string) error {
return keychain.Remove(keychain.LarkCliService, accountKey(appId, userOpenId))
}

// sameStoredTokenGeneration reports whether two snapshots represent the same
// refresh-token generation. Access tokens are used only for case that does not
// contain a refresh token.
func isSameStoredTokenGeneration(current, expected *StoredUAToken) bool {
Comment on lines +77 to +80

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Fix the doc comment name and remove the trailing space.

The comment names sameStoredTokenGeneration, but the function is isSameStoredTokenGeneration. Line 78 also ends with a trailing space after does not , which gofmt removes. The repository requires no gofmt -l . output.

♻️ Proposed fix
-// sameStoredTokenGeneration reports whether two snapshots represent the same
-// refresh-token generation. Access tokens are used only for case that does not 
-// contain a refresh token.
+// isSameStoredTokenGeneration reports whether two snapshots represent the same
+// refresh-token generation. Access tokens are compared only when neither
+// snapshot contains a refresh token.
 func isSameStoredTokenGeneration(current, expected *StoredUAToken) bool {

As per coding guidelines: "Run gofmt; Go code must be formatted with no gofmt -l . output."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// sameStoredTokenGeneration reports whether two snapshots represent the same
// refresh-token generation. Access tokens are used only for case that does not
// contain a refresh token.
func isSameStoredTokenGeneration(current, expected *StoredUAToken) bool {
// isSameStoredTokenGeneration reports whether two snapshots represent the same
// refresh-token generation. Access tokens are compared only when neither
// snapshot contains a refresh token.
func isSameStoredTokenGeneration(current, expected *StoredUAToken) bool {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/auth/token_store.go` around lines 77 - 80, Update the comment above
isSameStoredTokenGeneration to use the exact function name, remove the trailing
whitespace after “does not,” and run gofmt so the repository produces no
formatting violations.

Source: Coding guidelines

if current == nil || expected == nil ||
current.AppId != expected.AppId ||
current.UserOpenId != expected.UserOpenId {
return false
}
if current.RefreshToken != "" || expected.RefreshToken != "" {
return current.RefreshToken == expected.RefreshToken
}
return current.AccessToken == expected.AccessToken
}

// setStoredTokenIfCurrent stores updated only when expected is still the
// current token generation. It returns the token present after the check and
// whether the update was applied.
func setStoredTokenIfCurrent(expected, updated *StoredUAToken) (*StoredUAToken, bool, error) {
current, err := readStoredToken(expected.AppId, expected.UserOpenId)
if err != nil {
return nil, false, err
}
if !isSameStoredTokenGeneration(current, expected) {
return current, false, nil
}
if err := SetStoredToken(updated); err != nil {
return current, false, err
}
return updated, true, nil
}

// removeStoredTokenIfCurrent removes expected only when it is still the
// current token generation. It returns the token retained on a mismatch.
func removeStoredTokenIfCurrent(expected *StoredUAToken) (*StoredUAToken, bool, error) {
current, err := readStoredToken(expected.AppId, expected.UserOpenId)
if err != nil {
return nil, false, err
}
if !isSameStoredTokenGeneration(current, expected) {
return current, false, nil
}
if err := RemoveStoredToken(expected.AppId, expected.UserOpenId); err != nil {
return current, false, err
}
return nil, true, nil
}

// TokenStatus determines the freshness of a stored token.
func TokenStatus(token *StoredUAToken) string {
now := time.Now().UnixMilli()
Expand Down
Loading
Loading