Skip to content

Commit 2ba65c6

Browse files
committed
Add generic oauth support, rather than just google
1 parent 7dc5b69 commit 2ba65c6

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
google_auth_proxy
1+
oauth_proxy
22
=================
33

4+
Note: this is a fork of bit.ly's google_oauth_proxy project that works for any oauth provider (where any has actually been tested on Github and Google, YMMV)
45

5-
A reverse proxy that provides authentication using Google OAuth2 to validate
6+
A reverse proxy that provides authentication using an oauth server to validate
67
individual accounts, or a whole google apps domain.
78

8-
[![Build Status](https://secure.travis-ci.org/bitly/google_auth_proxy.png?branch=master)](http://travis-ci.org/bitly/google_auth_proxy)
9-
10-
119
## Architecture
1210

1311
```
@@ -52,6 +50,10 @@ Usage of ./google_auth_proxy:
5250
-redirect-url="": the OAuth Redirect URL. ie: "https://internalapp.yourcompany.com/oauth2/callback"
5351
-upstream=[]: the http url(s) of the upstream endpoint. If multiple, routing is based on path
5452
-version=false: print version string
53+
-login-url: the OAuth Login URL
54+
-redemption-url: the OAuth code redemption URL
55+
-user-info-url: the OAuth user info URL
56+
5557
```
5658

5759

@@ -84,16 +86,10 @@ server {
8486
}
8587
```
8688

87-
The command line to run `google_auth_proxy` would look like this:
89+
An example commandline that works with github is:
8890

8991
```bash
90-
./google_auth_proxy \
91-
--redirect-url="https://internal.yourcompany.com/oauth2/callback" \
92-
--google-apps-domain="yourcompany.com" \
93-
--upstream=http://127.0.0.1:8080/ \
94-
--cookie-secret=... \
95-
--client-id=... \
96-
--client-secret=...
92+
/oauth_proxy --client-id="f4dddfabbebe5ba" --client-secret="ecb0561717bbf29956f" --upstream="http://localhost:8080/" --cookie-secret="secretsecret" --login-url="https://github.com/login/oauth/authorize" --redirect-url="http://localhost:4180/oauth2/callback/" --redemption-url="https://github.com/login/oauth/access_token" --user-info-url="https://api.github.com/user"
9793
```
9894

9995
## Environment variables

main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ var (
1717
showVersion = flag.Bool("version", false, "print version string")
1818
httpAddr = flag.String("http-address", "127.0.0.1:4180", "<addr>:<port> to listen on for HTTP clients")
1919
redirectUrl = flag.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"")
20-
clientID = flag.String("client-id", "", "the Google OAuth Client ID: ie: \"123456.apps.googleusercontent.com\"")
20+
clientID = flag.String("client-id", "", "the Oauth Client ID: ie: \"123456.apps.googleusercontent.com\"")
2121
clientSecret = flag.String("client-secret", "", "the OAuth Client Secret")
22+
loginUrl = flag.String("login-url", "", "the OAuth Login URL")
23+
redemptionUrl = flag.String("redemption-url", "", "the OAuth code redemption URL")
24+
userInfoUrl = flag.String("user-info-url", "", "the OAuth user info URL")
2225
passBasicAuth = flag.Bool("pass-basic-auth", true, "pass HTTP Basic Auth information to upstream")
2326
htpasswdFile = flag.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
2427
cookieSecret = flag.String("cookie-secret", "", "the seed string for secure cookies")
@@ -79,7 +82,7 @@ func main() {
7982
}
8083

8184
validator := NewValidator(*googleAppsDomain, *authenticatedEmailsFile)
82-
oauthproxy := NewOauthProxy(upstreamUrls, *clientID, *clientSecret, validator)
85+
oauthproxy := NewOauthProxy(upstreamUrls, *clientID, *clientSecret, *loginUrl, *redemptionUrl, *userInfoUrl, validator)
8386
oauthproxy.SetRedirectUrl(redirectUrl)
8487
if *googleAppsDomain != "" && *authenticatedEmailsFile == "" {
8588
oauthproxy.SignInMessage = fmt.Sprintf("using a %s email address", *googleAppsDomain)

oauthproxy.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ type OauthProxy struct {
3636
serveMux *http.ServeMux
3737
}
3838

39-
func NewOauthProxy(proxyUrls []*url.URL, clientID string, clientSecret string, validator func(string) bool) *OauthProxy {
40-
login, _ := url.Parse("https://accounts.google.com/o/oauth2/auth")
41-
redeem, _ := url.Parse("https://accounts.google.com/o/oauth2/token")
42-
info, _ := url.Parse("https://www.googleapis.com/oauth2/v2/userinfo")
39+
func NewOauthProxy(proxyUrls []*url.URL, clientID string, clientSecret string, oauthLoginUrl string, oauthRedemptionUrl string, oauthUserInfoUrl string, validator func(string) bool) *OauthProxy {
40+
login, _ := url.Parse(oauthLoginUrl)
41+
redeem, _ := url.Parse(oauthRedemptionUrl)
42+
info, _ := url.Parse(oauthUserInfoUrl)
4343
serveMux := http.NewServeMux()
4444
for _, u := range proxyUrls {
4545
path := u.Path
@@ -54,7 +54,7 @@ func NewOauthProxy(proxyUrls []*url.URL, clientID string, clientSecret string, v
5454

5555
clientID: clientID,
5656
clientSecret: clientSecret,
57-
oauthScope: "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
57+
oauthScope: "",
5858
oauthRedemptionUrl: redeem,
5959
oauthLoginUrl: login,
6060
oauthUserInfoUrl: info,
@@ -90,8 +90,9 @@ func apiRequest(req *http.Request) (*simplejson.Json, error) {
9090
}
9191
if resp.StatusCode != 200 {
9292
log.Printf("got response code %d - %s", resp.StatusCode, body)
93-
return nil, errors.New("api request returned 200 status code")
93+
return nil, errors.New("api request returned error code")
9494
}
95+
log.Printf("got body %s", string(body))
9596
data, err := simplejson.NewJson(body)
9697
if err != nil {
9798
return nil, err
@@ -106,8 +107,8 @@ func (p *OauthProxy) redeemCode(code string) (string, error) {
106107
params.Add("client_secret", p.clientSecret)
107108
params.Add("code", code)
108109
params.Add("grant_type", "authorization_code")
109-
log.Printf("body is %s", params.Encode())
110110
req, err := http.NewRequest("POST", p.oauthRedemptionUrl.String(), bytes.NewBufferString(params.Encode()))
111+
req.Header.Set("Accept", "application/json")
111112
if err != nil {
112113
log.Printf("failed building request %s", err.Error())
113114
return "", err

templates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func getTemplates() *template.Template {
1212
<head><title>Sign In</title></head>
1313
<body>
1414
<form method="GET" action="/oauth2/start">
15-
<button type="submit">Sign In w/ Google</button>
15+
<button type="submit">Sign In</button>
1616
{{.SignInMessage}}
1717
</form>
1818
{{ if .Htpasswd }}

0 commit comments

Comments
 (0)