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
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion github/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package github

import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/dghubble/gologin"
oauth2Login "github.com/dghubble/gologin/oauth2"
Expand Down Expand Up @@ -57,8 +60,14 @@ func githubHandler(config *oauth2.Config, success, failure http.Handler) http.Ha
failure.ServeHTTP(w, req.WithContext(ctx))
return
}

httpClient := config.Client(ctx, token)
githubClient := github.NewClient(httpClient)
githubClient, err := githubClientFromAuthURL(config.Endpoint.AuthURL, httpClient)
if err != nil {
ctx = gologin.WithError(ctx, fmt.Errorf("github: error creating Client: %v", err))
failure.ServeHTTP(w, req.WithContext(ctx))
return
}
user, resp, err := githubClient.Users.Get(ctx, "")
err = validateResponse(user, resp, err)
if err != nil {
Expand All @@ -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) {
beyang marked this conversation as resolved.
Show resolved Hide resolved
client := github.NewClient(httpClient)
if !strings.HasPrefix(authURL, "https://github.com/") {
// convert authURL to GHE baseURL https://<mycompany>.github.jparrowsec.cn/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

baseURL, err := url.Parse(authURL)
if err != nil {
return nil, fmt.Errorf("github: error parsing Endoint.AuthURL: %s", authURL)
}
baseURL.Path = "/api/v3/"

client.BaseURL = baseURL
client.UploadURL = baseURL
}
return client, nil
}
21 changes: 21 additions & 0 deletions github/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestGithubHandler(t *testing.T) {
ctx = oauth2Login.WithToken(ctx, anyToken)

config := &oauth2.Config{}
config.Endpoint.AuthURL = "https://github.com/login/oauth/authorize"
success := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
githubUser, err := UserFromContext(ctx)
Expand Down Expand Up @@ -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 {
   ...
}

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: "https://github.com/login/oauth/authorize", expClientBaseURL: "https://api.github.com/"},
{authURL: "https://github.mycompany.com/login/oauth/authorize", expClientBaseURL: "https://github.mycompany.com/api/v3/"},
{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)

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)

t.Errorf("For authorization URL %q, expected client URL %q, but got %q", test.authURL, want, got)
}
}
}