-
Notifications
You must be signed in to change notification settings - Fork 7
Add Microsoft Teams integration #162
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
Open
daltoniam
wants to merge
6
commits into
main
Choose a base branch
from
switchboard-teams
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd59d8e
Add Microsoft Teams integration
daltoniam 180c5bb
Detach background refresh context to satisfy gosec G118
daltoniam ac2e59f
Address PR review: wire client_secret and cap OAuth response bodies
daltoniam 388b125
Address PR review round 2: race, ctx in poll, tenant URL escaping
daltoniam 0e69030
Address PR review round 3: nil-tenant panic, expires_at, $search escape
daltoniam 4d10779
Stub Graph in tests + clean up background goroutines
daltoniam 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| package teams | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "math" | ||
| "strings" | ||
| "time" | ||
|
|
||
| mcp "github.com/daltoniam/switchboard" | ||
| ) | ||
|
|
||
| // teamsLogin starts (or returns the in-progress) device-code OAuth flow. | ||
| func teamsLogin(ctx context.Context, t *teamsIntegration, args map[string]any) (*mcp.ToolResult, error) { | ||
| r := mcp.NewArgs(args) | ||
| tenantHint := r.Str("tenant") | ||
| if err := r.Err(); err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
|
|
||
| // If a flow is already in progress and not yet done, return the same prompt | ||
| // so the human can keep using the existing user_code. | ||
| activeFlow.mu.Lock() | ||
| existing := activeFlow.flow | ||
| activeFlow.mu.Unlock() | ||
| if existing != nil { | ||
| existing.mu.Lock() | ||
| stillRunning := !existing.done && time.Since(existing.startedAt) < time.Duration(existing.deviceResp.ExpiresIn)*time.Second | ||
| var dcr *deviceCodeResponse | ||
| if stillRunning { | ||
| dcr = existing.deviceResp | ||
| } | ||
| existing.mu.Unlock() | ||
| if dcr != nil { | ||
| return loginResponse(dcr, "resumed"), nil | ||
| } | ||
| } | ||
|
|
||
| dcr, err := t.startOAuth(ctx, tenantHint) | ||
| if err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| return loginResponse(dcr, "started"), nil | ||
| } | ||
|
|
||
| func loginResponse(dcr *deviceCodeResponse, status string) *mcp.ToolResult { | ||
| payload := map[string]any{ | ||
| "status": status, | ||
| "user_code": dcr.UserCode, | ||
| "verification_uri": dcr.VerificationURI, | ||
| "expires_in": dcr.ExpiresIn, | ||
| "interval_seconds": dcr.Interval, | ||
| "message": dcr.Message, | ||
| "next_step": "Visit verification_uri in a browser, enter user_code, then call teams_login_poll to detect completion.", | ||
| } | ||
| data, _ := json.Marshal(payload) | ||
| return &mcp.ToolResult{Data: string(data)} | ||
| } | ||
|
|
||
| // teamsLoginPoll reports the state of the active device-code flow. | ||
| func teamsLoginPoll(_ context.Context, _ *teamsIntegration, _ map[string]any) (*mcp.ToolResult, error) { | ||
| res := pollOAuth() | ||
| return mcp.JSONResult(res) | ||
| } | ||
|
|
||
| // teamsTokenStatus reports per-tenant token health. | ||
| func teamsTokenStatus(_ context.Context, t *teamsIntegration, _ map[string]any) (*mcp.ToolResult, error) { | ||
| type entry struct { | ||
| TenantID string `json:"tenant_id"` | ||
| TenantName string `json:"tenant_name,omitempty"` | ||
| UserUPN string `json:"user_upn,omitempty"` | ||
| UserDisplay string `json:"user_display,omitempty"` | ||
| Status string `json:"status"` | ||
| HasRefresh bool `json:"has_refresh"` | ||
| AgeMinutes float64 `json:"age_minutes"` | ||
| ExpiresIn string `json:"expires_in,omitempty"` | ||
| Source string `json:"source,omitempty"` | ||
| IsDefault bool `json:"is_default"` | ||
| } | ||
| defaultID := t.store.defaultID() | ||
| var entries []entry | ||
| for _, tn := range t.store.all() { | ||
| age := 0.0 | ||
| if !tn.UpdatedAt.IsZero() { | ||
| age = math.Round(time.Since(tn.UpdatedAt).Minutes()*10) / 10 | ||
| } | ||
| status := "healthy" | ||
| expiresIn := "" | ||
| if !tn.ExpiresAt.IsZero() { | ||
| remaining := time.Until(tn.ExpiresAt) | ||
| if remaining <= 0 { | ||
| status = "expired" | ||
| expiresIn = "0s" | ||
| } else { | ||
| if remaining < 5*time.Minute { | ||
| status = "expiring_soon" | ||
| } | ||
| expiresIn = remaining.Round(time.Second).String() | ||
| } | ||
| } | ||
| if tn.AccessToken == "" { | ||
| status = "missing" | ||
| } | ||
| entries = append(entries, entry{ | ||
| TenantID: tn.TenantID, | ||
| TenantName: tn.TenantName, | ||
| UserUPN: tn.UserUPN, | ||
| UserDisplay: tn.UserDisplay, | ||
| Status: status, | ||
| HasRefresh: tn.RefreshToken != "", | ||
| AgeMinutes: age, | ||
| ExpiresIn: expiresIn, | ||
| Source: tn.Source, | ||
| IsDefault: tn.TenantID == defaultID, | ||
| }) | ||
| } | ||
| return mcp.JSONResult(map[string]any{ | ||
| "tenant_count": len(entries), | ||
| "default_tenant_id": defaultID, | ||
| "tenants": entries, | ||
| "auto_refresh": map[string]any{ | ||
| "enabled": true, | ||
| "interval_minutes": 15, | ||
| "skew_seconds": int(refreshSkew.Seconds()), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // teamsRefreshTokens forces a refresh for the (default or specified) tenant. | ||
| func teamsRefreshTokens(ctx context.Context, t *teamsIntegration, args map[string]any) (*mcp.ToolResult, error) { | ||
| r := mcp.NewArgs(args) | ||
| tid := r.Str("tenant_id") | ||
| if err := r.Err(); err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| var tn *tenant | ||
| if tid != "" { | ||
| tn = t.store.get(tid) | ||
| if tn == nil { | ||
| return mcp.ErrResult(fmt.Errorf("unknown tenant: %s", tid)) | ||
| } | ||
| } else { | ||
| tn = t.activeTenant() | ||
| if tn == nil { | ||
| return mcp.ErrResult(fmt.Errorf("no tenant configured — run teams_login first")) | ||
| } | ||
| } | ||
| if tn.RefreshToken == "" { | ||
| return &mcp.ToolResult{ | ||
| Data: fmt.Sprintf("tenant %s has no refresh_token; re-run teams_login to acquire one", tn.TenantID), | ||
| IsError: true, | ||
| }, nil | ||
| } | ||
| if err := t.refreshTenant(ctx, tn); err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| refreshed := t.store.get(tn.TenantID) | ||
| return mcp.JSONResult(map[string]any{ | ||
| "status": "refreshed", | ||
| "tenant_id": refreshed.TenantID, | ||
| "expires_at": refreshed.ExpiresAt.UTC().Format(time.RFC3339), | ||
| }) | ||
| } | ||
|
|
||
| // teamsListTenants returns all configured tenants for the user. | ||
| func teamsListTenants(_ context.Context, t *teamsIntegration, _ map[string]any) (*mcp.ToolResult, error) { | ||
| type info struct { | ||
| TenantID string `json:"tenant_id"` | ||
| TenantName string `json:"tenant_name,omitempty"` | ||
| UserUPN string `json:"user_upn,omitempty"` | ||
| UserDisplay string `json:"user_display,omitempty"` | ||
| IsDefault bool `json:"is_default"` | ||
| } | ||
| defaultID := t.store.defaultID() | ||
| all := t.store.all() | ||
| out := make([]info, 0, len(all)) | ||
| for _, tn := range all { | ||
| out = append(out, info{ | ||
| TenantID: tn.TenantID, | ||
| TenantName: tn.TenantName, | ||
| UserUPN: tn.UserUPN, | ||
| UserDisplay: tn.UserDisplay, | ||
| IsDefault: tn.TenantID == defaultID, | ||
| }) | ||
| } | ||
| return mcp.JSONResult(map[string]any{ | ||
| "count": len(out), | ||
| "default_tenant_id": defaultID, | ||
| "tenants": out, | ||
| }) | ||
| } | ||
|
|
||
| // teamsRemoveTenant forgets cached tokens for a tenant. | ||
| func teamsRemoveTenant(_ context.Context, t *teamsIntegration, args map[string]any) (*mcp.ToolResult, error) { | ||
| r := mcp.NewArgs(args) | ||
| tid := r.Str("tenant_id") | ||
| if err := r.Err(); err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| if tid == "" { | ||
| return mcp.ErrResult(fmt.Errorf("tenant_id is required")) | ||
| } | ||
| if t.store.get(tid) == nil { | ||
| return mcp.ErrResult(fmt.Errorf("unknown tenant: %s", tid)) | ||
| } | ||
| t.store.remove(tid) | ||
| _ = t.store.saveToFile() | ||
| return mcp.JSONResult(map[string]any{ | ||
| "status": "removed", | ||
| "tenant_id": tid, | ||
| "default_tenant_id": t.store.defaultID(), | ||
| }) | ||
| } | ||
|
|
||
| // teamsSetDefault pins the default tenant for tools called without tenant_id. | ||
| func teamsSetDefault(_ context.Context, t *teamsIntegration, args map[string]any) (*mcp.ToolResult, error) { | ||
| r := mcp.NewArgs(args) | ||
| tid := r.Str("tenant_id") | ||
| if err := r.Err(); err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| if tid == "" { | ||
| return mcp.ErrResult(fmt.Errorf("tenant_id is required")) | ||
| } | ||
| if t.store.get(tid) == nil { | ||
| return mcp.ErrResult(fmt.Errorf("unknown tenant: %s", tid)) | ||
| } | ||
| t.store.setDefault(tid) | ||
| _ = t.store.saveToFile() | ||
| return mcp.JSONResult(map[string]any{ | ||
| "status": "ok", | ||
| "default_tenant_id": tid, | ||
| }) | ||
| } | ||
|
|
||
| // teamsGetMe returns the /me Graph profile for the chosen tenant. | ||
| func teamsGetMe(ctx context.Context, t *teamsIntegration, args map[string]any) (*mcp.ToolResult, error) { | ||
| tn, err := t.tenantFromArgs(args) | ||
| if err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| data, err := t.graphGet(ctx, tn.TenantID, "/me") | ||
| if err != nil { | ||
| return mcp.ErrResult(err) | ||
| } | ||
| return mcp.RawResult(data) | ||
| } | ||
|
|
||
| // formatContentType is shared by chat/channel senders. | ||
| func formatContentType(s string) string { | ||
| s = strings.ToLower(strings.TrimSpace(s)) | ||
| if s == "" || s == "html" { | ||
| return "html" | ||
| } | ||
| return "text" | ||
| } | ||
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.
teamsTokenStatushas non-trivial logic — it computesage_minutes, determinesstatus("expired" / "expiring_soon" / "healthy"), and formatsexpires_in— but has no unit test. A subtle bug like checkingremaining <= 0vsremaining < 0, or the5*time.Minutethreshold, would be invisible. Same gap coversteamsRefreshTokens,teamsGetMe,formatContentType, and all the chat/channel/user handlers. The HTTP layer is solid, but argument-parsing and query-construction paths are untested.A lightweight table-driven test covering the three status branches would cost about 20 lines: