@@ -1,6 +1,7 @@
1package main
1package main
4 "github.com/bitly/oauth2_proxy/providers"
5 "github.com/bitly/oauth2_proxy/providers"
5 "github.com/bmizerany/assert"
6 "github.com/bmizerany/assert"
6 "io/ioutil"
7 "io/ioutil"
@@ -88,6 +89,101 @@ func TestRobotsTxt(t *testing.T) {
88 assert.Equal(t, "User-agent: *\nDisallow: /", rw.Body.String())
89 assert.Equal(t, "User-agent: *\nDisallow: /", rw.Body.String())
92+func TestBasicAuthPassword(t *testing.T) {
93+ provider_server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
99+ payload = `{"access_token": "my_auth_token"}`
101+ payload = r.Header.Get("Authorization")
103+ payload = "No Authorization header found."
107+ w.Write([]byte(payload))
109+ opts := NewOptions()
110+ opts.Upstreams = append(opts.Upstreams, provider_server.URL)
111+ // The CookieSecret must be 32 bytes in order to create the AES
113+ opts.CookieSecret = "xyzzyplughxyzzyplughxyzzyplughxp"
114+ opts.ClientID = "bazquux"
115+ opts.ClientSecret = "foobar"
116+ opts.CookieSecure = false
117+ opts.PassBasicAuth = true
118+ opts.BasicAuthPassword = "This is a secure password"
121+ provider_url, _ := url.Parse(provider_server.URL)
122+ const email_address = "michael.bland@gsa.gov"
123+ const user_name = "michael.bland"
125+ opts.provider = &TestProvider{
126+ ProviderData: &providers.ProviderData{
127+ ProviderName: "Test Provider",
130+ Host: provider_url.Host,
131+ Path: "/oauth/authorize",
133+ RedeemUrl: &url.URL{
135+ Host: provider_url.Host,
136+ Path: "/oauth/token",
138+ ProfileUrl: &url.URL{
140+ Host: provider_url.Host,
141+ Path: "/api/v1/profile",
143+ Scope: "profile.email",
145+ EmailAddress: email_address,
148+ proxy := NewOauthProxy(opts, func(email string) bool {
149+ return email == email_address
152+ rw := httptest.NewRecorder()
153+ req, _ := http.NewRequest("GET", "/oauth2/callback?code=callback_code",
154+ strings.NewReader(""))
155+ proxy.ServeHTTP(rw, req)
156+ cookie := rw.HeaderMap["Set-Cookie"][0]
158+ cookieName := proxy.CookieName
160+ key_prefix := cookieName + "="
162+ for _, field := range strings.Split(cookie, "; ") {
163+ value = strings.TrimPrefix(field, key_prefix)
171+ req, _ = http.NewRequest("GET", "/", strings.NewReader(""))
172+ req.AddCookie(&http.Cookie{
176+ Expires: time.Now().Add(time.Duration(24)),
180+ rw = httptest.NewRecorder()
181+ proxy.ServeHTTP(rw, req)
182+ expectedHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(user_name+":"+opts.BasicAuthPassword))
183+ assert.Equal(t, expectedHeader, rw.Body.String())
184+ provider_server.Close()
91type TestProvider struct {
187type TestProvider struct {
92 *providers.ProviderData
188 *providers.ProviderData
93 EmailAddress string
189 EmailAddress string