Skip to content

fix: oauth2 inheritance [INS-5023] - #8622

Merged
ryan-willis merged 3 commits into
developfrom
fix/oauth2-headers-issues
Apr 23, 2025
Merged

fix: oauth2 inheritance [INS-5023]#8622
ryan-willis merged 3 commits into
developfrom
fix/oauth2-headers-issues

Conversation

@ryan-willis

@ryan-willis ryan-willis commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

fixes #7508
fixes #7880
fixes #8374

This change alters the authentication model for OAuth2 requests to add an additional optional field that is derived at request time and used to inherit the correct token when executing a request from within a folder that has OAuth2 configured.

EDIT: This change alters the RenderedRequest type to include an _authParentId field that is used to determine what parentId to use when storing/retrieving an OAuth2 token.

The smoke test has been updated with the reproduction steps from the linked issues to confirm that it uses the "nearest" folder's token every attempt, so instead of storing a token with a relationship to the request itself, it maintains the relationship to the folder.

@ryan-willis
ryan-willis requested a review from a team April 16, 2025 14:52
marckong
marckong previously approved these changes Apr 16, 2025

@marckong marckong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Simple and to the point PR!

Comment thread packages/insomnia/src/models/request.ts Outdated
Comment on lines +85 to +86
// to identify stored token inheritance from a RequestGroup (if applicable)
parentId?: string;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what are the consequences of creating a relationship like this?

@marckong marckong Apr 16, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hm good question. I was looking through the code and we are just doing getOrCreateByParentId. It just reads an entity. Only if there is none, then it creates, which I think happens regardless of this change. I can't think of any concerning consequence from this change.

@ryan-willis ryan-willis Apr 16, 2025

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 only thing that comes to mind is how differently we handle folder deletes vs request deletes, and the delete folder code might not look for token entities, leaving them dangling. I'll take a look to see

EDIT: looks like we're clear on that front, any time anything is removed we gather descendants and remove those too

@jackkav jackkav Apr 17, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hmm, I think. we have a database normalisation problem there.
Our database uses parentIds explicitly for joining different objects. There should never be an object with two parentIds. Because the withDescendents code could not compute children if it needs to search every object within the db objects.

This authentication object is a property of request. therefore.

request:{
  parentId:'afolder?',
  authentication:{
    parentId:'somethingelse?'
  }
}

it now becomes possible for a request to have a parentId and a authentication object to have a different parentId.

I would suggest two direction this PR could take:

  1. split and migrate request.authentication into a new db object with a parentIds or request or folder.
  2. improve the code which requires this parentId, to avoid it.

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.

Ah, I see. The authentication object should only have that field on it during a request, and not make it to the db, but if it's doing that, I'm leaning toward option 2 and wrapping that up into a

{
  requestOrGroupId: "parentId",
  authentication: original
}

for the OAuth paths, what do you think?

@marckong marckong Apr 17, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jackkav's concerns are valid. I think 1 might be a better option but would require migration path for previous versions. Option 2 would be a good alternative without the db migration path. I didn't quite understand the 2 option @ryan-willis you are proposing. Could you help me understand?

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.

I'm thinking of changing interface for these methods, rather than the models, let me draft a change that targets this branch that will explain it better

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.

I've confirmed that this field doesn't persist on the request's authentication object, and is only utilized in the active context of a request action.

I can rename this field to make it less conflating, something like sourceId or requestOrGroupId (since we use that for the same purpose) if that's preferred

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

so the auth object is updated for the purpose of something that happens later on, but not persisted in the database, in which case the db object shouldnt be modified at all, perhaps create a new code only object. Let's discuss on zoom since I build the first version I might have some insights that are hard to recall in a PR review.

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.

@jackkav that's what I was leaning toward with option 2, I'll update this PR with that

@ryan-willis
ryan-willis force-pushed the fix/oauth2-headers-issues branch from dec0312 to 0793481 Compare April 17, 2025 00:40
@jackkav
jackkav requested a review from marckong April 17, 2025 07:58
@ryan-willis
ryan-willis force-pushed the fix/oauth2-headers-issues branch from 0793481 to 03b5f62 Compare April 18, 2025 00:42
@ryan-willis
ryan-willis force-pushed the fix/oauth2-headers-issues branch 2 times, most recently from bb2221f to 48d72da Compare April 21, 2025 05:07
@ryan-willis
ryan-willis requested a review from jackkav April 21, 2025 05:19
@ryan-willis
ryan-willis force-pushed the fix/oauth2-headers-issues branch from 48d72da to 93f6a57 Compare April 21, 2025 05:20

@jackkav jackkav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some things come to mind here

  • This e2e test and implemenation decision here would be much clearer if we add a UX to view oauth2 tokens, even just a tooltip
  • the naming closestAuthId would be clearer than authParentId since its not a direct parent in the same sense its used in the db model, but a indirect relationship does exist.
  • An alternative implementation could be re-running the closest auth algorithm to get the id in getExistingAccessTokenAndRefreshIfExpired every time instead of storing the closest auth id and carrying it around.

This one hurts the brain a bit

@ryan-willis
ryan-willis force-pushed the fix/oauth2-headers-issues branch from 93f6a57 to c79c262 Compare April 23, 2025 14:04
@ryan-willis
ryan-willis requested a review from jackkav April 23, 2025 14:26
@ryan-willis
ryan-willis force-pushed the fix/oauth2-headers-issues branch from c79c262 to ef1ee3a Compare April 23, 2025 14:51
@ryan-willis
ryan-willis merged commit c1e7788 into develop Apr 23, 2025
@ryan-willis
ryan-willis deleted the fix/oauth2-headers-issues branch April 23, 2025 15:42

@jackkav jackkav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

would prefer a stateless approach but happy to move it forward now the naming is clarified, would be great to plan some improvements to this feature too

cwangsmv pushed a commit that referenced this pull request Apr 25, 2025
* fix: oauth2 inheritance [INS-5023]

* don't modify existing authentication model, only rendered request

* rename to closestAuthId + isolate existing token retrieval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants