Skip to content

Commit

Permalink
fix: Allow using CST created with PAT to authenticate requests (#233)
Browse files Browse the repository at this point in the history
Co-authored-by: Seam Bot <seambot@getseam.com>
  • Loading branch information
andrii-balitskyi and seambot authored Jan 30, 2025
1 parent 0a7fdf7 commit d25f7c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
7 changes: 0 additions & 7 deletions src/lib/middleware/with-client-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ export const withClientSession: Middleware<
})
}

if (publishable_key == null && api_key_id == null) {
throw new UnauthorizedException({
type: "unauthorized",
message: "publishable key or api key must be set",
})
}

req.auth = {
type: "client_session",
client_session_id,
Expand Down
30 changes: 30 additions & 0 deletions test/api/client_sessions/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,33 @@ test("POST /client_sessions/create api key", async (t: ExecutionContext) => {
"Client session is correctly associated with the api key that was used to create it",
)
})

test("POST /client_sessions/create with PAT with workspace", async (t) => {
const { axios, db } = await getTestServer(t, { seed: false })
const seed_result = seedDatabase(db)

const {
data: { client_session },
} = await axios.post(
"/client_sessions/create",
{
user_identifier_key: "john@example.com",
},
{
headers: {
Authorization: `Bearer ${seed_result.seam_at1_token}`,
"Seam-Workspace": seed_result.seed_workspace_1,
},
},
)

t.truthy(client_session.token)
t.truthy(client_session.created_at)

// Verify that the CST can be used to authenticate requests
axios.defaults.headers.common.Authorization = `Bearer ${client_session.token}`
const {
data: { devices },
} = await axios.get("/devices/list")
t.is(devices.length, 0)
})
22 changes: 0 additions & 22 deletions test/middleware/with-client-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,4 @@ test("withClientSession middleware - successful auth", async (t) => {
)
t.is(revokedErr?.status, 401)
t.is(revokedErr?.response.error.type, "client_session_revoked")

// Test client session without api key or publishable key
const invalid_session = db.addClientSession({
workspace_id: seed_result.seed_workspace_1,
})

const invalidSessionErr = await t.throwsAsync<SimpleAxiosError>(
axios.get("/connected_accounts/get", {
params: {
connected_account_id: seed_result.john_connected_account_id,
},
headers: {
Authorization: `Bearer ${invalid_session.token}`,
},
}),
)
t.is(invalidSessionErr?.status, 401)
t.is(invalidSessionErr?.response.error.type, "unauthorized")
t.is(
invalidSessionErr?.response.error.message,
"publishable key or api key must be set",
)
})

0 comments on commit d25f7c3

Please sign in to comment.