-
Notifications
You must be signed in to change notification settings - Fork 0
✨ feat: Feishu auto-provisioning #116
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
390ee84
🐛 fix: auto-pull docker image when not present locally
vaayne 6437198
✨ feat: add ProvisionIdentityUser auth helper
vaayne 6030180
✨ feat: add Provisioner interface and Coordinator.ProvisionUser
vaayne c9fcdbf
✨ feat: add TenantKey/AutoProvision to Feishu config
vaayne c9e0624
✨ feat: Feishu contact API + maybeAutoProvision
vaayne 91d9eca
✨ feat: wire maybeAutoProvision into onMessage and onReaction
vaayne c777fc9
📝 docs: document Feishu auto-provisioning (EN + ZH)
vaayne d14e088
🐛 fix: address Codex P1/P2 review comments
vaayne 2a40cf2
✨ feat: add tenant_key and auto_provision fields to Feishu channel UI
vaayne 3d4100d
✨ feat: auto-detect tenant_key at startup via Feishu tenant API
vaayne d47b390
🐛 fix: address Codex P1/P2 review comments (round 2)
vaayne e967331
♻️ refactor: simplify feishu auto-provision code
vaayne 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,112 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| const maxUsernameAttempts = 20 | ||
|
|
||
| // ProvisionRequest carries the information needed to provision a new user. | ||
| type ProvisionRequest struct { | ||
| Platform string | ||
| ExternalID string | ||
| Name string | ||
| EmailHint string | ||
| } | ||
|
|
||
| // ProvisionIdentityUser creates a new user + identity pair atomically. | ||
| // It is idempotent: if the (platform, externalID) identity already exists, | ||
| // the existing user is returned without creating anything new. | ||
| // | ||
| // On a concurrent race where two callers both miss the initial identity lookup | ||
| // and one loses the unique-constraint insert, the loser re-reads the winning | ||
| // identity/user and returns it rather than propagating an error. | ||
| func ProvisionIdentityUser(ctx context.Context, store AuthStore, req ProvisionRequest) (AuthUser, error) { | ||
| // Fast path: identity already exists. | ||
| existing, err := store.GetIdentityByPlatform(ctx, req.Platform, req.ExternalID) | ||
| if err == nil { | ||
| user, err := store.GetUser(ctx, existing.UserID) | ||
| if err != nil { | ||
| return AuthUser{}, fmt.Errorf("provision: get existing user: %w", err) | ||
| } | ||
| return user, nil | ||
| } | ||
| if !errors.Is(err, sql.ErrNoRows) { | ||
| return AuthUser{}, fmt.Errorf("provision: check identity: %w", err) | ||
| } | ||
|
|
||
| username, err := deriveUsername(ctx, store, req.EmailHint, req.ExternalID, req.Platform) | ||
| if err != nil { | ||
| return AuthUser{}, err | ||
| } | ||
|
|
||
| user, err := store.CreateUser(ctx, username, "") // empty hash = no web login | ||
| if err != nil { | ||
| return AuthUser{}, fmt.Errorf("provision: create user: %w", err) | ||
| } | ||
|
|
||
| _, identErr := store.CreateIdentity(ctx, Identity{ | ||
| UserID: user.ID, | ||
| Platform: req.Platform, | ||
| ExternalID: req.ExternalID, | ||
| Name: req.Name, | ||
| }) | ||
| if identErr != nil { | ||
| _ = store.DeleteUser(ctx, user.ID) | ||
|
|
||
| // A concurrent provision may have won the race on the unique constraint. | ||
| if winner, rerr := store.GetIdentityByPlatform(ctx, req.Platform, req.ExternalID); rerr == nil { | ||
| if winUser, rerr := store.GetUser(ctx, winner.UserID); rerr == nil { | ||
| return winUser, nil | ||
| } | ||
| } | ||
|
|
||
| return AuthUser{}, fmt.Errorf("provision: create identity: %w", identErr) | ||
| } | ||
|
|
||
| return user, nil | ||
| } | ||
|
|
||
| // deriveUsername produces a unique username from an email hint or external ID. | ||
| // It tries the base name, then base-2, base-3, … up to maxUsernameAttempts. | ||
| // Returns an error if all candidates are taken. | ||
| func deriveUsername(ctx context.Context, store AuthStore, emailHint, externalID, platform string) (string, error) { | ||
| base := localPart(emailHint) | ||
| if base == "" { | ||
| id := externalID | ||
| if len(id) > 8 { | ||
| id = id[:8] | ||
| } | ||
| base = platform + "-" + id | ||
| } | ||
|
|
||
| for i := range maxUsernameAttempts { | ||
| candidate := base | ||
| if i > 0 { | ||
| candidate = fmt.Sprintf("%s-%d", base, i+1) | ||
| } | ||
| _, err := store.GetUserByUsername(ctx, candidate) | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return candidate, nil | ||
| } | ||
| if err != nil { | ||
| return "", fmt.Errorf("provision: probe username %q: %w", candidate, err) | ||
| } | ||
| } | ||
|
|
||
| return "", fmt.Errorf("provision: no unique username after %d attempts for base %q", maxUsernameAttempts, base) | ||
| } | ||
|
|
||
| // localPart returns the portion of an email address before the @ sign. | ||
| // Returns empty string if there is no @ or the local part is empty. | ||
| func localPart(email string) string { | ||
| at := strings.Index(email, "@") | ||
| if at <= 0 { | ||
| return "" | ||
| } | ||
| return email[:at] | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProvisionIdentityUsercurrently treats everyGetIdentityByPlatformerror as a cache miss and proceeds to create a user. If that lookup fails for reasons other than not-found (for example transient DB/context failures), this path can perform unintended writes and then surface misleading downstream errors from create/rollback attempts. The provisioning flow should only continue onsql.ErrNoRowsand immediately return other lookup errors.Useful? React with 👍 / 👎.