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

add header basic authorization #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Usage of ./gooauth_proxy:
-client-secret="": the OAuth Client Secret
-cookie-domain="": an optional cookie domain to force cookies to
-cookie-secret="": the seed string for secure cookies
-header-basic-auth=false: use Authorization: Basic header for authorization (see RFC 2617 section 2)
-google-apps-domain="": authenticate against the given google apps domain
-http-address="127.0.0.1:4180": <addr>:<port> to listen on for HTTP clients
-pass-basic-auth=true: pass HTTP Basic Auth information to upstream
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
redirectUrl = flag.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"")
clientID = flag.String("client-id", "", "the Oauth Client ID: ie: \"123456.apps.googleusercontent.com\"")
clientSecret = flag.String("client-secret", "", "the OAuth Client Secret")
headerBasicAuth = flag.Bool("header-basic-auth", false, "use Authorization: Basic header for authorization")
loginUrl = flag.String("login-url", "", "the OAuth Login URL")
redemptionUrl = flag.String("redemption-url", "", "the OAuth code redemption URL")
cookieSecret = flag.String("cookie-secret", "", "the seed string for secure cookies")
Expand Down Expand Up @@ -79,7 +80,7 @@ func main() {
}

validator := NewCommandValidator(*userVerificationCommand)
oauthproxy := NewOauthProxy(upstreamUrls, *clientID, *clientSecret, *loginUrl, *redemptionUrl, *oauthScope, validator)
oauthproxy := NewOauthProxy(upstreamUrls, *clientID, *clientSecret, *headerBasicAuth, *loginUrl, *redemptionUrl, *oauthScope, validator)
oauthproxy.SetRedirectUrl(redirectUrl)

listener, err := net.Listen("tcp", *httpAddr)
Expand Down
9 changes: 8 additions & 1 deletion oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"github.com/bitly/go-simplejson"
Expand Down Expand Up @@ -29,11 +30,12 @@ type OauthProxy struct {
oauthScope string
clientID string
clientSecret string
headerBasicAuth bool
SignInMessage string
serveMux *http.ServeMux
}

func NewOauthProxy(proxyUrls []*url.URL, clientID string, clientSecret string, oauthLoginUrl string, oauthRedemptionUrl string, oauthScope string, validator func(string) bool) *OauthProxy {
func NewOauthProxy(proxyUrls []*url.URL, clientID string, clientSecret string, headerBasicAuth bool, oauthLoginUrl string, oauthRedemptionUrl string, oauthScope string, validator func(string) bool) *OauthProxy {
login, _ := url.Parse(oauthLoginUrl)
redeem, _ := url.Parse(oauthRedemptionUrl)
serveMux := http.NewServeMux()
Expand All @@ -50,6 +52,7 @@ func NewOauthProxy(proxyUrls []*url.URL, clientID string, clientSecret string, o

clientID: clientID,
clientSecret: clientSecret,
headerBasicAuth: headerBasicAuth,
oauthScope: oauthScope,
oauthRedemptionUrl: redeem,
oauthLoginUrl: login,
Expand Down Expand Up @@ -104,6 +107,10 @@ func (p *OauthProxy) redeemCode(code string) (string, error) {
params.Add("grant_type", "authorization_code")
req, err := http.NewRequest("POST", p.oauthRedemptionUrl.String(), bytes.NewBufferString(params.Encode()))
req.Header.Set("Accept", "application/json")
if p.headerBasicAuth {
token := []byte(fmt.Sprintf("%s:%s", p.clientID, p.clientSecret))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(token)))
}
if err != nil {
log.Printf("failed building request %s", err.Error())
return "", err
Expand Down