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

github: support enterprise instances #33

Closed
wants to merge 3 commits into from

Conversation

beyang
Copy link
Contributor

@beyang beyang commented Nov 10, 2018

Thanks for making a wonderful library!

This PR adds support for GitHub Enterprise instances. It looks at the AuthURL endpoint to infer whether the GitHub instance is github.com or an enterprise instance.

Copy link
Owner

@dghubble dghubble left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I think this could be a reasonable feature to offer and left some comments.

github/login.go Outdated
@@ -83,3 +92,19 @@ func validateResponse(user *github.User, resp *github.Response, err error) error
}
return nil
}

func githubClientFromAuthURL(authURL string, httpClient *http.Client) (*github.Client, error) {
if strings.HasPrefix(authURL, "https://github.com/") {
Copy link
Owner

Choose a reason for hiding this comment

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

It worries me some we can't have the regular Github Client as the default (else) behavior. I see why you've got it this way since GithubEnterprise auth url's don't look likely to have any structure we could detect and leverage.

I think everyone using gologin with Github today should have an Endpoint.AuthURL starting with https://github.com (Github forbits http), but there may be some cases someone proxies access through another endpoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about adding a func EnterpriseCallbackHandler(config *oauth2.Config, success, failure http.Handler) method that is the same as CallbackHandler, but sets an enterprise API root URL?

Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather not. Exposing the function would make Github Enterprise a first-class use case. Then if Github's enterprise product were to make a breaking change, I'd be expected to accomodate that in gologin.

As is, this PR would just let Github login automatically best-effort allow GHE (undocumented). If it becomes problematic, it can be reverted.

Copy link
Owner

Choose a reason for hiding this comment

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

Taking a step back, if a users wants a handler for GHE, it would be very much in the gologin style to just add convenience functions that chain gologin/oauth2 with a EnterpriseCallbackHandler. And that handler can go in their code or here.

It really comes down to whether GHE should be in-scope for gologin or left to users.

Copy link
Owner

Choose a reason for hiding this comment

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

Alright, I think your EnterpriseCallbackHandler is a reasonable approach. It eliminates the risk this will break anyone using regular Github login and it will be intuitive - its clear and isolated and avoids any magic of having GHE handling in the Github handler. And gologin is somewhat insulated from GHE making drastic breakages since the go-github library would have to deal with them first.

Ignore my "I'd rather not" from earlier.

github/login.go Outdated Show resolved Hide resolved
github/login.go Outdated Show resolved Hide resolved
github/login.go Outdated Show resolved Hide resolved
github/login.go Outdated Show resolved Hide resolved
github/login.go Outdated Show resolved Hide resolved
@dghubble
Copy link
Owner

Rebasing should solve the go get dependency failures.

@beyang
Copy link
Contributor Author

beyang commented Nov 18, 2018

Addressed comments, PTAL

authURL string
expClientBaseURL string
}{
{authURL: "https://github.com/login/oauth/authorize/", expClientBaseURL: "https://api.github.com/"},
Copy link
Owner

Choose a reason for hiding this comment

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

You don't have to repeat the field name if you don't want.

{authURL: "http://github.mycompany.com/login/oauth/authorize", expClientBaseURL: "http://github.mycompany.com/api/v3/"},
} {
client, err := githubClientFromAuthURL(test.authURL, nil)
if err != nil {
Copy link
Owner

Choose a reason for hiding this comment

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

assert.NotNil(err)

@@ -108,3 +109,23 @@ func TestValidateResponse(t *testing.T) {
assert.Equal(t, ErrUnableToGetGithubUser, validateResponse(validUser, invalidResponse, nil))
assert.Equal(t, ErrUnableToGetGithubUser, validateResponse(&github.User{}, validResponse, nil))
}

func Test_githubClientFromAuthURL(t *testing.T) {
for _, test := range []struct {
Copy link
Owner

Choose a reason for hiding this comment

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

While this is valid, can you define cases separately? (kinda a nitpick, but it matches other libraries)

cases := []struct{
    ...
}{
    ...
}
for _, c := range cases {
   ...
}

github/login.go Outdated
func githubClientFromAuthURL(authURL string, httpClient *http.Client) (*github.Client, error) {
client := github.NewClient(httpClient)
if !strings.HasPrefix(authURL, "https://github.com/") {
// convert authURL to GHE baseURL https://<mycompany>.git.luolix.top/api/v3/
Copy link
Owner

Choose a reason for hiding this comment

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

Example isn't right, GHE wouldn't be hosted under github.com

if err != nil {
t.Fatal(err)
}
if got, want := client.BaseURL.String(), test.expClientBaseURL; got != want {
Copy link
Owner

Choose a reason for hiding this comment

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

gologin already makes use of testify assert, which you can use. assert.Equal(t, client.BaseURL.String(), c.expectedBaseURL)

@beyang
Copy link
Contributor Author

beyang commented Nov 18, 2018

Comments addressed, PTAL. Given the addition of EnterpriseCallbackHandler, renamed githubClientFromAuthURL to enterpriseGithubClientFromAuthURL and updated the test. Also added a test for EnterpriseCallbackHandler.

@dghubble
Copy link
Owner

Squashed and rebased as e2bffa0 btw. Thanks for contributing!

@dghubble dghubble closed this Nov 20, 2018
daxmc99 added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this pull request May 13, 2022
This is the actual version we use because we had a replace directive.
From PR dghubble/gologin#33
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