Record provider auth sessions

This commit is contained in:
ZacharyZhang-NY committed 2026-05-23 23:03:09 -04:00
1 parent 216d485c58
commit 648544114a
16 files changed
+280 -14

No files matched your search

@@ -107,6 +107,28 @@ describe("compliance service", () => {
createdByEmployeeId: employeeId,
authorizedEmployeeId: employeeId,
});
const providerId = (connection.db.prepare("select id from providers where org_id = ? and provider_key = 'github-copilot'")
.get(orgId) as { id: string }).id;
connection.db.prepare(`
insert into provider_auth_sessions(
id, org_id, provider_id, provider_key, employee_id, state, nonce, code_verifier_sha256,
redirect_uri, sign_in_url, partition, created_at, expires_at, completed_at
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
`).run(
"auths-test",
orgId,
providerId,
"github-copilot",
employeeId,
"state",
"nonce",
"a".repeat(64),
`agentcompany://provider-auth/${providerId}`,
"https://github.com/login",
`persist:agentcompany-provider-${providerId}`,
new Date().toISOString(),
new Date(Date.now() + 60000).toISOString(),
);
holdId = compliance.createLegalHold({
orgId,
entityType: "employee",
@@ -144,6 +166,7 @@ describe("compliance service", () => {
expect(redactedApproval?.decidedByEmployeeId).toBeNull();
expect(redactedSocialAccount?.authorizedEmployeeId).toBeNull();
expect(state.socialAccountGrants).toHaveLength(0);
expect(state.providerAuthSessions).toHaveLength(0);
expect(state.complianceExports[0]?.exportType).toBe("gdpr_delete");
expect(state.complianceExports[0]?.requestedByEmployeeId).toBeNull();
expect(state.auditEvents.some((event) => event.entityType === "compliance_export" && event.action === "gdpr_deleted")).toBe(true);
@@ -23,7 +23,7 @@ describe("domain services", () => {
const connection = createConnection();
const rows = connection.db.prepare("select version from schema_migrations order by version asc").all() as Array<{ version: number }>;
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]);
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]);
connection.close();
});
@@ -152,6 +152,8 @@ describe("migration service", () => {
create table provider_requests(id text primary key, org_id text, provider_id text, workspace_id text);
create table provider_usage_windows(id text primary key, org_id text, provider_id text);
create table provider_rate_limit_observations(id text primary key, org_id text, provider_id text);
create table provider_auth_profiles(id text primary key, org_id text, provider_id text, credential_id text);
create table provider_auth_sessions(id text primary key, org_id text, provider_id text, employee_id text);
create table credentials(id text primary key, org_id text);
create table skill_definitions(id text primary key, org_id text);
create table skill_revisions(id text primary key, skill_id text);
@@ -228,6 +230,8 @@ describe("migration service", () => {
insert into provider_requests(id, org_id, provider_id, workspace_id) values ('req-1', 'org-1', 'prov-1', 'wsp-1');
insert into provider_usage_windows(id, org_id, provider_id) values ('usage-1', 'org-1', 'prov-1');
insert into provider_rate_limit_observations(id, org_id, provider_id) values ('quota-1', 'org-1', 'prov-1');
insert into provider_auth_profiles(id, org_id, provider_id, credential_id) values ('authp-1', 'org-1', 'prov-1', 'cred-1');
insert into provider_auth_sessions(id, org_id, provider_id, employee_id) values ('auths-1', 'org-1', 'prov-1', 'emp-1');
insert into skill_definitions(id, org_id) values ('skill-1', 'org-1');
insert into skill_revisions(id, skill_id) values ('rev-1', 'skill-1'), ('rev-2', 'skill-1');
insert into skill_artifacts(id, revision_id) values ('artifact-1', 'rev-1');
@@ -289,6 +293,8 @@ describe("migration service", () => {
expect(body.tableCounts.provider_requests).toBe(1);
expect(body.tableCounts.provider_usage_windows).toBe(1);
expect(body.tableCounts.provider_rate_limit_observations).toBe(1);
expect(body.tableCounts.provider_auth_profiles).toBe(1);
expect(body.tableCounts.provider_auth_sessions).toBe(1);
expect(body.tableCounts.browser_session_containers).toBe(1);
expect(body.tableCounts.browser_action_policies).toBe(1);
expect(body.tableCounts.social_approval_routes).toBe(1);
@@ -317,6 +323,8 @@ describe("migration service", () => {
expect(body.idMaps.migration_reconciliation_items).toEqual(["reconcile-1"]);
expect(body.idMaps.provider_usage_windows).toEqual(["usage-1"]);
expect(body.idMaps.provider_rate_limit_observations).toEqual(["quota-1"]);
expect(body.idMaps.provider_auth_profiles).toEqual(["authp-1"]);
expect(body.idMaps.provider_auth_sessions).toEqual(["auths-1"]);
expect(body.idMaps.browser_session_containers).toEqual(["container-1"]);
expect(body.idMaps.browser_action_policies).toEqual(["browser-policy-1"]);
expect(body.idMaps.social_approval_routes).toEqual(["social-route-1"]);
@@ -173,7 +173,7 @@ describe("provider service", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const snapshot = new SnapshotService(connection.db);
let launched: { providerId: string; label: string; signInUrl: string } | null = null;
let launched: { providerId: string; label: string; signInUrl: string; partition: string } | null = null;
const providers = new ProviderService(connection.db, providerAdapters, testVault, (input) => { launched = input; });
let orgId = "";
let employeeId = "";
@@ -198,7 +198,19 @@ describe("provider service", () => {
providerId: copilotProviderId,
label: "GitHub Copilot",
signInUrl: "https://github.com/login",
partition: `persist:agentcompany-provider-${copilotProviderId}`,
});
expect(state.providerAuthSessions[0]).toMatchObject({
orgId,
providerId: copilotProviderId,
providerKey: "github-copilot",
employeeId,
partition: `persist:agentcompany-provider-${copilotProviderId}`,
completedAt: null,
});
expect(state.providerAuthSessions[0]?.state).toHaveLength(32);
expect(state.providerAuthSessions[0]?.nonce).toHaveLength(32);
expect(state.providerAuthSessions[0]?.codeVerifierSha256).toHaveLength(64);
expect(state.auditEvents).toContainEqual(expect.objectContaining({
entityType: "provider",
entityId: copilotProviderId,
@@ -209,6 +221,50 @@ describe("provider service", () => {
connection.close();
});
test("binds OAuth credentials to a sign-in session source", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const snapshot = new SnapshotService(connection.db);
const providers = new ProviderService(connection.db, providerAdapters, testVault, () => {});
let orgId = "";
let employeeId = "";
let copilotProviderId = "";
connection.transaction(() => {
orgId = createOrganization(organization);
employeeId = (connection.db.prepare("select id from employees where org_id = ?").get(orgId) as { id: string }).id;
copilotProviderId = providerId(connection, orgId, "github-copilot");
});
expect(() => connection.transaction(() => providers.saveCredential({
orgId,
providerId: copilotProviderId,
secret: "ghu_live_provider",
model: "claude-opus-4-5",
employeeId,
}))).toThrow("Provider sign-in session is required.");
connection.transaction(() => {
providers.launchSignInWindow({ orgId, providerId: copilotProviderId, employeeId });
providers.saveCredential({
orgId,
providerId: copilotProviderId,
secret: "ghu_live_provider",
model: "claude-opus-4-5",
employeeId,
});
});
const state = snapshot.load();
const profile = state.providerAuthProfiles.find((item) => item.providerId === copilotProviderId && item.active);
const session = state.providerAuthSessions.find((item) => item.providerId === copilotProviderId);
if (!profile || !session) throw new Error("Provider auth evidence was not created.");
expect(profile).toMatchObject({ sourceType: "provider_sign_in_session", sourceRef: session.id });
expect(session.completedAt).toBeTruthy();
expect(state.providers.find((item) => item.id === copilotProviderId)?.enabled).toBe(true);
connection.close();
});
test("records provider request results through direct execution adapters", async () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);