Browse code

auth with user agent

Nan Monnand Deng authored on 2013/08/02 16:30:45
Showing 3 changed files
... ...
@@ -87,7 +87,7 @@ func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Reque
87 87
 	if err != nil {
88 88
 		return err
89 89
 	}
90
-	status, err := auth.Login(authConfig)
90
+	status, err := auth.Login(authConfig, srv.HTTPRequestFactory())
91 91
 	if err != nil {
92 92
 		return err
93 93
 	}
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"encoding/json"
6 6
 	"errors"
7 7
 	"fmt"
8
+	"github.com/dotcloud/docker/utils"
8 9
 	"io/ioutil"
9 10
 	"net/http"
10 11
 	"os"
... ...
@@ -140,7 +141,7 @@ func SaveConfig(configFile *ConfigFile) error {
140 140
 }
141 141
 
142 142
 // try to register/login to the registry server
143
-func Login(authConfig *AuthConfig) (string, error) {
143
+func Login(authConfig *AuthConfig, factory *utils.HTTPRequestFactory) (string, error) {
144 144
 	client := &http.Client{}
145 145
 	reqStatusCode := 0
146 146
 	var status string
... ...
@@ -171,7 +172,7 @@ func Login(authConfig *AuthConfig) (string, error) {
171 171
 			"Please check your e-mail for a confirmation link.")
172 172
 	} else if reqStatusCode == 400 {
173 173
 		if string(reqBody) == "\"Username or email already exists\"" {
174
-			req, err := http.NewRequest("GET", IndexServerAddress()+"users/", nil)
174
+			req, err := factory.NewRequest("GET", IndexServerAddress()+"users/", nil)
175 175
 			req.SetBasicAuth(authConfig.Username, authConfig.Password)
176 176
 			resp, err := client.Do(req)
177 177
 			if err != nil {
... ...
@@ -1173,6 +1173,15 @@ func NewServer(flGraphPath string, autoRestart, enableCors bool, dns ListOpts) (
1173 1173
 	return srv, nil
1174 1174
 }
1175 1175
 
1176
+func (srv *Server) HTTPRequestFactory() *utils.HTTPRequestFactory {
1177
+	if srv.reqFactory == nil {
1178
+		ud := utils.NewHTTPUserAgentDecorator(srv.versionInfos()...)
1179
+		factory := utils.NewHTTPRequestFactory(ud)
1180
+		srv.reqFactory = factory
1181
+	}
1182
+	return srv.reqFactory
1183
+}
1184
+
1176 1185
 func (srv *Server) LogEvent(action, id string) {
1177 1186
 	now := time.Now().Unix()
1178 1187
 	jm := utils.JSONMessage{Status: action, ID: id, Time: now}