@@ -3,7 +3,6 @@ package auth
33import (
44 "encoding/base64"
55 "encoding/json"
6- "errors"
76 "fmt"
87 "io/ioutil"
98 "net/http"
@@ -16,7 +15,7 @@ import (
1615const CONFIGFILE = ".dockercfg"
1716
1817// the registry server we want to login against
19- const REGISTRY_SERVER = "https://registry .docker.io"
18+ const INDEX_SERVER = "https://index .docker.io"
2019
2120type AuthConfig struct {
2221 Username string `json:"username"`
@@ -76,6 +75,9 @@ func LoadConfig(rootPath string) (*AuthConfig, error) {
7675 return nil , err
7776 }
7877 arr := strings .Split (string (b ), "\n " )
78+ if len (arr ) < 2 {
79+ return nil , fmt .Errorf ("The Auth config file is empty" )
80+ }
7981 origAuth := strings .Split (arr [0 ], " = " )
8082 origEmail := strings .Split (arr [1 ], " = " )
8183 authConfig , err := DecodeAuth (origAuth [1 ])
@@ -89,9 +91,14 @@ func LoadConfig(rootPath string) (*AuthConfig, error) {
8991
9092// save the auth config
9193func saveConfig (rootPath , authStr string , email string ) error {
94+ confFile := path .Join (rootPath , CONFIGFILE )
95+ if len (email ) == 0 {
96+ os .Remove (confFile )
97+ return nil
98+ }
9299 lines := "auth = " + authStr + "\n " + "email = " + email + "\n "
93100 b := []byte (lines )
94- err := ioutil .WriteFile (path . Join ( rootPath , CONFIGFILE ) , b , 0600 )
101+ err := ioutil .WriteFile (confFile , b , 0600 )
95102 if err != nil {
96103 return err
97104 }
@@ -101,40 +108,38 @@ func saveConfig(rootPath, authStr string, email string) error {
101108// try to register/login to the registry server
102109func Login (authConfig * AuthConfig ) (string , error ) {
103110 storeConfig := false
111+ client := & http.Client {}
104112 reqStatusCode := 0
105113 var status string
106- var errMsg string
107114 var reqBody []byte
108115 jsonBody , err := json .Marshal (authConfig )
109116 if err != nil {
110- errMsg = fmt .Sprintf ("Config Error: %s" , err )
111- return "" , errors .New (errMsg )
117+ return "" , fmt .Errorf ("Config Error: %s" , err )
112118 }
113119
114120 // using `bytes.NewReader(jsonBody)` here causes the server to respond with a 411 status.
115121 b := strings .NewReader (string (jsonBody ))
116- req1 , err := http .Post (REGISTRY_SERVER + "/v1/users" , "application/json; charset=utf-8" , b )
122+ req1 , err := http .Post (INDEX_SERVER + "/v1/users/ " , "application/json; charset=utf-8" , b )
117123 if err != nil {
118- errMsg = fmt .Sprintf ("Server Error: %s" , err )
119- return "" , errors .New (errMsg )
124+ return "" , fmt .Errorf ("Server Error: %s" , err )
120125 }
121-
122126 reqStatusCode = req1 .StatusCode
123127 defer req1 .Body .Close ()
124128 reqBody , err = ioutil .ReadAll (req1 .Body )
125129 if err != nil {
126- errMsg = fmt .Sprintf ("Server Error: [%#v] %s" , reqStatusCode , err )
127- return "" , errors .New (errMsg )
130+ return "" , fmt .Errorf ("Server Error: [%#v] %s" , reqStatusCode , err )
128131 }
129132
130133 if reqStatusCode == 201 {
131- status = "Account Created\n "
134+ status = "Account created. Please use the confirmation link we sent" +
135+ " to your e-mail to activate it.\n "
132136 storeConfig = true
137+ } else if reqStatusCode == 403 {
138+ return "" , fmt .Errorf ("Login: Your account hasn't been activated. " +
139+ "Please check your e-mail for a confirmation link." )
133140 } else if reqStatusCode == 400 {
134- // FIXME: This should be 'exists', not 'exist'. Need to change on the server first.
135- if string (reqBody ) == "Username or email already exist" {
136- client := & http.Client {}
137- req , err := http .NewRequest ("GET" , REGISTRY_SERVER + "/v1/users" , nil )
141+ if string (reqBody ) == "\" Username or email already exists\" " {
142+ req , err := http .NewRequest ("GET" , INDEX_SERVER + "/v1/users/" , nil )
138143 req .SetBasicAuth (authConfig .Username , authConfig .Password )
139144 resp , err := client .Do (req )
140145 if err != nil {
@@ -148,17 +153,18 @@ func Login(authConfig *AuthConfig) (string, error) {
148153 if resp .StatusCode == 200 {
149154 status = "Login Succeeded\n "
150155 storeConfig = true
156+ } else if resp .StatusCode == 401 {
157+ saveConfig (authConfig .rootPath , "" , "" )
158+ return "" , fmt .Errorf ("Wrong login/password, please try again" )
151159 } else {
152- status = fmt .Sprintf ("Login: %s" , body )
153- return "" , errors . New ( status )
160+ return "" , fmt .Errorf ("Login: %s (Code: %d; Headers: %s) " , body ,
161+ resp . StatusCode , resp . Header )
154162 }
155163 } else {
156- status = fmt .Sprintf ("Registration: %s" , reqBody )
157- return "" , errors .New (status )
164+ return "" , fmt .Errorf ("Registration: %s" , reqBody )
158165 }
159166 } else {
160- status = fmt .Sprintf ("[%s] : %s" , reqStatusCode , reqBody )
161- return "" , errors .New (status )
167+ return "" , fmt .Errorf ("Unexpected status code [%d] : %s" , reqStatusCode , reqBody )
162168 }
163169 if storeConfig {
164170 authStr := EncodeAuth (authConfig )
0 commit comments