@@ -25,19 +25,15 @@ var (
2525)
2626
2727type AuthConfig struct {
28- Username string `json:"username"`
29- Password string `json:"password"`
28+ Username string `json:"username,omitempty"`
29+ Password string `json:"password,omitempty"`
30+ Auth string `json:"auth"`
3031 Email string `json:"email"`
31- rootPath string
3232}
3333
34- func NewAuthConfig (username , password , email , rootPath string ) * AuthConfig {
35- return & AuthConfig {
36- Username : username ,
37- Password : password ,
38- Email : email ,
39- rootPath : rootPath ,
40- }
34+ type ConfigFile struct {
35+ Configs map [string ]AuthConfig `json:"configs,omitempty"`
36+ rootPath string
4137}
4238
4339func IndexServerAddress () string {
@@ -54,70 +50,91 @@ func encodeAuth(authConfig *AuthConfig) string {
5450}
5551
5652// decode the auth string
57- func decodeAuth (authStr string ) (* AuthConfig , error ) {
53+ func decodeAuth (authStr string ) (string , string , error ) {
5854 decLen := base64 .StdEncoding .DecodedLen (len (authStr ))
5955 decoded := make ([]byte , decLen )
6056 authByte := []byte (authStr )
6157 n , err := base64 .StdEncoding .Decode (decoded , authByte )
6258 if err != nil {
63- return nil , err
59+ return "" , "" , err
6460 }
6561 if n > decLen {
66- return nil , fmt .Errorf ("Something went wrong decoding auth config" )
62+ return "" , "" , fmt .Errorf ("Something went wrong decoding auth config" )
6763 }
6864 arr := strings .Split (string (decoded ), ":" )
6965 if len (arr ) != 2 {
70- return nil , fmt .Errorf ("Invalid auth configuration file" )
66+ return "" , "" , fmt .Errorf ("Invalid auth configuration file" )
7167 }
7268 password := strings .Trim (arr [1 ], "\x00 " )
73- return & AuthConfig { Username : arr [0 ], Password : password } , nil
69+ return arr [0 ], password , nil
7470}
7571
7672// load up the auth config information and return values
7773// FIXME: use the internal golang config parser
78- func LoadConfig (rootPath string ) (* AuthConfig , error ) {
74+ func LoadConfig (rootPath string ) (* ConfigFile , error ) {
75+ configFile := ConfigFile {Configs : make (map [string ]AuthConfig ), rootPath : rootPath }
7976 confFile := path .Join (rootPath , CONFIGFILE )
8077 if _ , err := os .Stat (confFile ); err != nil {
81- return & AuthConfig { rootPath : rootPath } , ErrConfigFileMissing
78+ return & configFile , ErrConfigFileMissing
8279 }
8380 b , err := ioutil .ReadFile (confFile )
8481 if err != nil {
8582 return nil , err
8683 }
87- arr := strings .Split (string (b ), "\n " )
88- if len (arr ) < 2 {
89- return nil , fmt .Errorf ("The Auth config file is empty" )
90- }
91- origAuth := strings .Split (arr [0 ], " = " )
92- origEmail := strings .Split (arr [1 ], " = " )
93- authConfig , err := decodeAuth (origAuth [1 ])
94- if err != nil {
95- return nil , err
84+
85+ if err := json .Unmarshal (b , & configFile .Configs ); err != nil {
86+ arr := strings .Split (string (b ), "\n " )
87+ if len (arr ) < 2 {
88+ return nil , fmt .Errorf ("The Auth config file is empty" )
89+ }
90+ authConfig := AuthConfig {}
91+ origAuth := strings .Split (arr [0 ], " = " )
92+ authConfig .Username , authConfig .Password , err = decodeAuth (origAuth [1 ])
93+ if err != nil {
94+ return nil , err
95+ }
96+ origEmail := strings .Split (arr [1 ], " = " )
97+ authConfig .Email = origEmail [1 ]
98+ configFile .Configs [IndexServerAddress ()] = authConfig
99+ } else {
100+ for k , authConfig := range configFile .Configs {
101+ authConfig .Username , authConfig .Password , err = decodeAuth (authConfig .Auth )
102+ if err != nil {
103+ return nil , err
104+ }
105+ configFile .Configs [k ] = authConfig
106+ }
96107 }
97- authConfig .Email = origEmail [1 ]
98- authConfig .rootPath = rootPath
99- return authConfig , nil
108+ return & configFile , nil
100109}
101110
102111// save the auth config
103- func SaveConfig (authConfig * AuthConfig ) error {
104- confFile := path .Join (authConfig .rootPath , CONFIGFILE )
105- if len (authConfig . Email ) == 0 {
112+ func SaveConfig (configFile * ConfigFile ) error {
113+ confFile := path .Join (configFile .rootPath , CONFIGFILE )
114+ if len (configFile . Configs ) == 0 {
106115 os .Remove (confFile )
107116 return nil
108117 }
109- lines := "auth = " + encodeAuth (authConfig ) + "\n " + "email = " + authConfig .Email + "\n "
110- b := []byte (lines )
111- err := ioutil .WriteFile (confFile , b , 0600 )
118+ for k , authConfig := range configFile .Configs {
119+ authConfig .Auth = encodeAuth (& authConfig )
120+ authConfig .Username = ""
121+ authConfig .Password = ""
122+ configFile .Configs [k ] = authConfig
123+ }
124+
125+ b , err := json .Marshal (configFile .Configs )
126+ if err != nil {
127+ return err
128+ }
129+ err = ioutil .WriteFile (confFile , b , 0600 )
112130 if err != nil {
113131 return err
114132 }
115133 return nil
116134}
117135
118136// try to register/login to the registry server
119- func Login (authConfig * AuthConfig , store bool ) (string , error ) {
120- storeConfig := false
137+ func Login (authConfig * AuthConfig ) (string , error ) {
121138 client := & http.Client {}
122139 reqStatusCode := 0
123140 var status string
@@ -143,7 +160,6 @@ func Login(authConfig *AuthConfig, store bool) (string, error) {
143160 if reqStatusCode == 201 {
144161 status = "Account created. Please use the confirmation link we sent" +
145162 " to your e-mail to activate it."
146- storeConfig = true
147163 } else if reqStatusCode == 403 {
148164 return "" , fmt .Errorf ("Login: Your account hasn't been activated. " +
149165 "Please check your e-mail for a confirmation link." )
@@ -162,14 +178,7 @@ func Login(authConfig *AuthConfig, store bool) (string, error) {
162178 }
163179 if resp .StatusCode == 200 {
164180 status = "Login Succeeded"
165- storeConfig = true
166181 } else if resp .StatusCode == 401 {
167- if store {
168- authConfig .Email = ""
169- if err := SaveConfig (authConfig ); err != nil {
170- return "" , err
171- }
172- }
173182 return "" , fmt .Errorf ("Wrong login/password, please try again" )
174183 } else {
175184 return "" , fmt .Errorf ("Login: %s (Code: %d; Headers: %s)" , body ,
@@ -181,10 +190,5 @@ func Login(authConfig *AuthConfig, store bool) (string, error) {
181190 } else {
182191 return "" , fmt .Errorf ("Unexpected status code [%d] : %s" , reqStatusCode , reqBody )
183192 }
184- if storeConfig && store {
185- if err := SaveConfig (authConfig ); err != nil {
186- return "" , err
187- }
188- }
189193 return status , nil
190194}
0 commit comments