Skip to content

Commit 1f6471f

Browse files
schmitzhermesrojer
authored andcommitted
Feature: GHE Support (#151)
* Parametrize GitHub host, for use with GHE
1 parent 3c31d7a commit 1f6471f

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html itemscope itemtype="http://schema.org/Article">
22
<body>
3-
<button type="button" onclick="location.href='https://github.com/login/oauth/authorize?scope=user:email&client_id={{.ClientId}}'">Login with GitHub</button>
4-
<button type="button" onclick="location.href='https://github.com/settings/applications'">Revoke access</button>
3+
<button type="button" onclick="location.href='{{.GithubWebUri}}/login/oauth/authorize?scope=user:email&client_id={{.ClientId}}'">Login with GitHub</button>
4+
<button type="button" onclick="location.href='{{.GithubWebUri}}/settings/applications'">Revoke access</button>
55
</body>
66
</html>

auth_server/authn/github_auth.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type GitHubAuthConfig struct {
3939
TokenDB string `yaml:"token_db,omitempty"`
4040
HTTPTimeout time.Duration `yaml:"http_timeout,omitempty"`
4141
RevalidateAfter time.Duration `yaml:"revalidate_after,omitempty"`
42+
GithubWebUri string `yaml:"github_web_uri,omitempty"`
43+
GithubApiUri string `yaml:"github_api_uri,omitempty"`
4244
}
4345

4446
type GitHubAuthRequest struct {
@@ -90,13 +92,30 @@ func (gha *GitHubAuth) DoGitHubAuth(rw http.ResponseWriter, req *http.Request) {
9092
}
9193
}
9294

95+
func (gha *GitHubAuth) getGithubApiUri() string {
96+
if gha.config.GithubApiUri != "" {
97+
return gha.config.GithubApiUri
98+
} else {
99+
return "https://api.github.com"
100+
}
101+
}
102+
103+
func (gha *GitHubAuth) getGithubWebUri() string {
104+
if gha.config.GithubWebUri != "" {
105+
return gha.config.GithubWebUri
106+
} else {
107+
return "https://github.com"
108+
}
109+
}
110+
93111
func (gha *GitHubAuth) doGitHubAuthCreateToken(rw http.ResponseWriter, code string) {
94112
data := url.Values{
95113
"code": []string{string(code)},
96114
"client_id": []string{gha.config.ClientId},
97115
"client_secret": []string{gha.config.ClientSecret},
98116
}
99-
req, err := http.NewRequest("POST", "https://github.com/login/oauth/access_token", bytes.NewBufferString(data.Encode()))
117+
118+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/login/oauth/access_token", gha.getGithubWebUri()), bytes.NewBufferString(data.Encode()))
100119
if err != nil {
101120
http.Error(rw, fmt.Sprintf("Error creating request to GitHub auth backend: %s", err), http.StatusServiceUnavailable)
102121
return
@@ -150,7 +169,7 @@ func (gha *GitHubAuth) doGitHubAuthCreateToken(rw http.ResponseWriter, code stri
150169
}
151170

152171
func (gha *GitHubAuth) validateAccessToken(token string) (user string, err error) {
153-
req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
172+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/user", gha.getGithubApiUri()), nil)
154173
if err != nil {
155174
err = fmt.Errorf("could not create request to get information for token %s: %s", token, err)
156175
return
@@ -187,7 +206,7 @@ func (gha *GitHubAuth) checkOrganization(token, user string) (err error) {
187206
if gha.config.Organization == "" {
188207
return nil
189208
}
190-
url := fmt.Sprintf("https://api.github.com/orgs/%s/members/%s", gha.config.Organization, user)
209+
url := fmt.Sprintf("%s/orgs/%s/members/%s", gha.getGithubApiUri(), gha.config.Organization, user)
191210
req, err := http.NewRequest("GET", url, nil)
192211
if err != nil {
193212
err = fmt.Errorf("could not create request to get organization membership: %s", err)

examples/reference.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ github_auth:
8787
http_timeout: "10s"
8888
# How long to wait before revalidating the GitHub token. Optional.
8989
revalidate_after: "1h"
90+
# The Github Web URI in case you are using Github Enterprise.
91+
# Includes the protocol, without trailing slash. Optional - defaults to: https://github.com
92+
github_web_uri: "https://github.acme.com"
93+
# The Github API URI in case you are using Github Enterprise.
94+
# Includes the protocol, without trailing slash. - defaults to: https://api.github.com
95+
github_api_uri: "https://github.acme.com/api/v3"
9096

9197
# LDAP authentication.
9298
# Authentication is performed by first binding to the server, looking up the user entry

0 commit comments

Comments
 (0)