Skip to content

feat: update to support array middleware#200

Merged
chmaltsp merged 22 commits intomainfrom
pc/update-auth-middleware
Aug 5, 2024
Merged

feat: update to support array middleware#200
chmaltsp merged 22 commits intomainfrom
pc/update-auth-middleware

Conversation

@chmaltsp
Copy link
Copy Markdown
Contributor

No description provided.

@seambot
Copy link
Copy Markdown
Contributor

seambot commented Jun 14, 2024

Bundle dist size: 26M

@chmaltsp chmaltsp marked this pull request as ready for review June 18, 2024 01:01
@chmaltsp chmaltsp requested a review from razor-x as a code owner June 18, 2024 01:01
Copy link
Copy Markdown
Member

@razor-x razor-x left a comment

Choose a reason for hiding this comment

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

Got about 1/2 way though.
image

Comment thread test/api/devices/list.test.ts Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's try to replace all cases of the cst abbreviation with client session or client session token. So name this withClientSession

Comment thread src/lib/middleware/with-cst.ts Outdated

import { withSimulatedOutage } from "./with-simulated-outage.ts"

export const withCst: Middleware<
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
export const withCst: Middleware<
export const withClientSession: Middleware<


const relevant_cs = req.db.client_sessions.find((cs) =>
cs.connect_webview_ids.includes(connect_webview.connect_webview_id),
cs.workspace_id.includes(connect_webview.workspace_id),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why was this logic updated? The original logic looks correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The original logic was not working because the client session actually never got updated with the connect_webview_id. I'm not sure how that was working in the first place and I think it was skipping over the relevant_cs most of the time.

Comment thread src/pages/api/devices/get.ts Outdated
Comment thread src/pages/api/devices/list.ts Outdated
manufacturer,
} = req.commonParams
const { workspace_id } = req.auth
const { workspace_id } = req.auth as { workspace_id: string }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why did we loose type safety?

export default withRouteSpec({
methods: ["POST"],
auth: "cst_ak_pk",
auth: ["client_session"],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
auth: ["client_session"],
auth: ["api_key", "console_session"],

Comment thread src/types/index.ts
@@ -0,0 +1,42 @@
export type AuthenticatedRequest = Request & {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generally at Seam, we do not write code in index files (existing cases are legacy examples). Types should be defined alongside the module needs them, or in this case we could simply name this authenticated-request.ts

Co-authored-by: Evan Sosenko <evan@getseam.com>
@stale
Copy link
Copy Markdown

stale Bot commented Jun 21, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale Bot added the stale label Jun 21, 2024
@stale stale Bot closed this Jun 22, 2024
@razor-x razor-x reopened this Jun 24, 2024
@stale stale Bot removed the stale label Jun 24, 2024
Copy link
Copy Markdown
Member

@razor-x razor-x left a comment

Choose a reason for hiding this comment

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

Made it through the rest 😄

Comment thread src/pages/api/client_sessions/create.ts Outdated

if (req.auth.auth_mode === "publishable_key" && user_identifier_key == null) {
if (
req.auth.type === "client_session" &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Client sessions cannot create more client sessions, this check only applies to publishable_key auth.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Based on the implementation in seam-connect only client_sessions have the method property in the auth middleware with publishable_key as an option, is this incorrect?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The endpoint should always return 403 if client session auth.

Comment thread src/types/authenticated-request.ts
db,
} = await getTestServer(t)

const cs = db.client_sessions.find((cs) => cs.token === ws2.cst)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aren't only console sessions JWT? A client session cannot reset a sandbox workspace. What is this testing?

Comment thread test/fixtures/get-test-database.ts Outdated
connect_webview_ids: [cw.connect_webview_id],
connected_account_ids: [ca.connected_account_id],
user_identifier_key: "seed_client_session_user",
user_identity_ids: ["john_user_identity_id"],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where does this id come from? I would not expect it to be hard coded here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This value is available when we seed the database in

john_user_identity_id: "john_user_identity_id",
so I was just adding to this client session as well

Comment thread src/lib/middleware/with-route-spec.ts Outdated
Comment thread src/lib/middleware/with-access-token.ts Outdated
auth_mode: "access_token"
export const withAccessToken =
<RequiresWorkspaceId extends boolean>({
require_workspace_id,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
require_workspace_id,
is_workspace_id_required,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

can make this change, but was trying to align naming with current convention in seam-connect, is it worth drifting away?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, if some of this is copy/paste from seam connect, then it's ok to follow that. I can't really know the difference in review, so I just have to flag things that look like they break convention. You can apply best judgement.


if (short_token == null || long_token == null)
return res.status(400).end("malformed token")
if (!is_cst) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (!is_cst) {
if (!is_client_session_token) {

Comment thread src/lib/middleware/with-client-session.ts Outdated
Comment thread src/lib/middleware/with-session-auth.ts Outdated
Comment on lines +42 to +43
workspace_id_from_header !== null &&
typeof workspace_id_from_header !== "undefined" &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is actually the simplest way to do safe null checks

Suggested change
workspace_id_from_header !== null &&
typeof workspace_id_from_header !== "undefined" &&
workspace_id_from_header != null

(or workspace_id_from_header == null for the inverse)

}

if (require_workspace_id) {
;(req.auth as Extract<
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar comment as before.

@stale
Copy link
Copy Markdown

stale Bot commented Jun 27, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale Bot added the stale label Jun 27, 2024
@stale stale Bot closed this Jun 29, 2024
@stale
Copy link
Copy Markdown

stale Bot commented Jul 7, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale Bot added the stale label Jul 7, 2024
@stale stale Bot closed this Jul 8, 2024
@chmaltsp chmaltsp reopened this Jul 8, 2024
@stale stale Bot removed the stale label Jul 8, 2024
@chmaltsp chmaltsp requested a review from razor-x July 8, 2024 18:26
@razor-x
Copy link
Copy Markdown
Member

razor-x commented Jul 9, 2024

Everything looks good expect this change: https://github.com/seamapi/fake-seam-connect/pull/200/files#r1651613761

Maybe we can just revert it and add some more tests here for api key and pat auth methods? https://github.com/seamapi/fake-seam-connect/blob/main/test/api/client_sessions/create.test.ts

@stale
Copy link
Copy Markdown

stale Bot commented Jul 13, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale Bot added the stale label Jul 13, 2024
@stale stale Bot closed this Jul 14, 2024
@chmaltsp
Copy link
Copy Markdown
Contributor Author

chmaltsp commented Aug 1, 2024

Everything looks good expect this change: https://github.com/seamapi/fake-seam-connect/pull/200/files#r1651613761

Maybe we can just revert it and add some more tests here for api key and pat auth methods? https://github.com/seamapi/fake-seam-connect/blob/main/test/api/client_sessions/create.test.ts

Sorry this one kind of fell off the radar. I'd like to get this over the finish line. I'm not sure what you mean by reverting it, are we going to support both auth_method schema as well as the new one? Happy to change it to a 403.

@razor-x razor-x reopened this Aug 1, 2024
@razor-x
Copy link
Copy Markdown
Member

razor-x commented Aug 1, 2024

Everything looks good expect this change: #200 (files)
Maybe we can just revert it and add some more tests here for api key and pat auth methods? main/test/api/client_sessions/create.test.ts

Sorry this one kind of fell off the radar. I'd like to get this over the finish line. I'm not sure what you mean by reverting it, are we going to support both auth_method schema as well as the new one? Happy to change it to a 403.

Sure. The issue is still the one from this comment: #200 (comment)

The endpoint auth method in this PR does not match the behavior for the actual endpoint.

@stale stale Bot closed this Aug 2, 2024
@razor-x razor-x reopened this Aug 5, 2024
@chmaltsp chmaltsp merged commit 3399244 into main Aug 5, 2024
@chmaltsp chmaltsp deleted the pc/update-auth-middleware branch August 5, 2024 21:08
@razor-x
Copy link
Copy Markdown
Member

razor-x commented Aug 5, 2024

@chmaltsp I tried to test out the changes in the SDK but I'm getting errors. I think we are close but either I'm doing something wrong with passing the tokens or the fake is not authorizing them properly: seamapi/javascript-http#150

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants