Skip to content
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

[WIP] Authentication context (resolves #94) #97

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

willstott101
Copy link
Contributor

  • Built on top of Bugfix: Fix logging on response streams. #96
  • Added a generic context type to the Git class which allows the authenticate function to respond with a context value (inspired by the context callback in ApolloServer).
  • Context value included in all request-related events
  • Refactored the request flow to ensure all HTTP requests are authenticated (even if that means a push results in multiple authenticate calls).

Aside from the changes to authenticate every request the re-factoring was mostly to help me reduce the number of code paths that need to be supported.

The more optionally-async optionally-promise callbacks we support the messier the code becomes and it was hard for me to be confident all code branches were correct.

That being said I'm happy to go backwards on some of this refactoring now that it all seems to work.

Left as a WIP as I want to expand the test suite to cover more error cases.

src/git.test.ts Outdated Show resolved Hide resolved
}
export class Git extends EventEmitter implements GitEvents {
export class Git<T = any> extends EventEmitter implements GitEvents<T> {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we add a comment on why this is any?

Copy link
Contributor Author

@willstott101 willstott101 Jan 27, 2022

Choose a reason for hiding this comment

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

Yeah, I'm not decided on this totally. I guess it could should probably default to undefined/void

Then TSC will warn you to add the type to your Git constructor if you return something from authenticate

well... no it won't it'll tell you that you can't return anything but you see what I mean


export type ParsedGitRequest = ParsedServiceGitRequest | ParsedHeadGitRequest;

export function parseRequest(req: http.IncomingMessage): ParsedGitRequest {
Copy link
Owner

Choose a reason for hiding this comment

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

This is a nice refactor, thank you!

src/util.test.ts Outdated
expect('').not.toEqual('should not have entered this callback');
done();
});
expect(parseBasicAuth.bind(null, req)).toThrow(new BasicAuthError());
Copy link
Owner

Choose a reason for hiding this comment

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

can we also keep the status code asserts? We should make sure the response is formed correctly in the event of an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah absolutely those are the kind of tests that have made me stay in WIP

src/util.ts Outdated Show resolved Hide resolved
@gabrielcsapo
Copy link
Owner

I left a few comments, it will take time to go through this change more thoroughly.

@willstott101
Copy link
Contributor Author

willstott101 commented Jan 27, 2022

Sure, will address those a bit later today. Once using the library after this refactor... requiring throwing from authenticate feels a bit off. Do you think returning false would be better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants