|
@@ -48,7 +48,7 @@ func IndexServerAddress() string {
|
|
}
|
|
}
|
|
|
|
|
|
// create a base64 encoded auth string to store in config
|
|
// create a base64 encoded auth string to store in config
|
|
-func EncodeAuth(authConfig *AuthConfig) string {
|
|
|
|
|
|
+func encodeAuth(authConfig *AuthConfig) string {
|
|
authStr := authConfig.Username + ":" + authConfig.Password
|
|
authStr := authConfig.Username + ":" + authConfig.Password
|
|
msg := []byte(authStr)
|
|
msg := []byte(authStr)
|
|
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(msg)))
|
|
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(msg)))
|
|
@@ -57,7 +57,7 @@ func EncodeAuth(authConfig *AuthConfig) string {
|
|
}
|
|
}
|
|
|
|
|
|
// decode the auth string
|
|
// decode the auth string
|
|
-func DecodeAuth(authStr string) (*AuthConfig, error) {
|
|
|
|
|
|
+func decodeAuth(authStr string) (*AuthConfig, error) {
|
|
decLen := base64.StdEncoding.DecodedLen(len(authStr))
|
|
decLen := base64.StdEncoding.DecodedLen(len(authStr))
|
|
decoded := make([]byte, decLen)
|
|
decoded := make([]byte, decLen)
|
|
authByte := []byte(authStr)
|
|
authByte := []byte(authStr)
|
|
@@ -82,7 +82,7 @@ func DecodeAuth(authStr string) (*AuthConfig, error) {
|
|
func LoadConfig(rootPath string) (*AuthConfig, error) {
|
|
func LoadConfig(rootPath string) (*AuthConfig, error) {
|
|
confFile := path.Join(rootPath, CONFIGFILE)
|
|
confFile := path.Join(rootPath, CONFIGFILE)
|
|
if _, err := os.Stat(confFile); err != nil {
|
|
if _, err := os.Stat(confFile); err != nil {
|
|
- return nil, ErrConfigFileMissing
|
|
|
|
|
|
+ return &AuthConfig{rootPath:rootPath}, ErrConfigFileMissing
|
|
}
|
|
}
|
|
b, err := ioutil.ReadFile(confFile)
|
|
b, err := ioutil.ReadFile(confFile)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -94,7 +94,7 @@ func LoadConfig(rootPath string) (*AuthConfig, error) {
|
|
}
|
|
}
|
|
origAuth := strings.Split(arr[0], " = ")
|
|
origAuth := strings.Split(arr[0], " = ")
|
|
origEmail := strings.Split(arr[1], " = ")
|
|
origEmail := strings.Split(arr[1], " = ")
|
|
- authConfig, err := DecodeAuth(origAuth[1])
|
|
|
|
|
|
+ authConfig, err := decodeAuth(origAuth[1])
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -104,13 +104,13 @@ func LoadConfig(rootPath string) (*AuthConfig, error) {
|
|
}
|
|
}
|
|
|
|
|
|
// save the auth config
|
|
// save the auth config
|
|
-func SaveConfig(rootPath, authStr string, email string) error {
|
|
|
|
- confFile := path.Join(rootPath, CONFIGFILE)
|
|
|
|
- if len(email) == 0 {
|
|
|
|
|
|
+func SaveConfig(authConfig *AuthConfig) error {
|
|
|
|
+ confFile := path.Join(authConfig.rootPath, CONFIGFILE)
|
|
|
|
+ if len(authConfig.Email) == 0 {
|
|
os.Remove(confFile)
|
|
os.Remove(confFile)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
- lines := "auth = " + authStr + "\n" + "email = " + email + "\n"
|
|
|
|
|
|
+ lines := "auth = " + encodeAuth(authConfig) + "\n" + "email = " + authConfig.Email + "\n"
|
|
b := []byte(lines)
|
|
b := []byte(lines)
|
|
err := ioutil.WriteFile(confFile, b, 0600)
|
|
err := ioutil.WriteFile(confFile, b, 0600)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -121,7 +121,6 @@ func SaveConfig(rootPath, authStr string, email string) error {
|
|
|
|
|
|
// try to register/login to the registry server
|
|
// try to register/login to the registry server
|
|
func Login(authConfig *AuthConfig) (string, error) {
|
|
func Login(authConfig *AuthConfig) (string, error) {
|
|
- storeConfig := false
|
|
|
|
client := &http.Client{}
|
|
client := &http.Client{}
|
|
reqStatusCode := 0
|
|
reqStatusCode := 0
|
|
var status string
|
|
var status string
|
|
@@ -147,7 +146,6 @@ func Login(authConfig *AuthConfig) (string, error) {
|
|
if reqStatusCode == 201 {
|
|
if reqStatusCode == 201 {
|
|
status = "Account created. Please use the confirmation link we sent" +
|
|
status = "Account created. Please use the confirmation link we sent" +
|
|
" to your e-mail to activate it.\n"
|
|
" to your e-mail to activate it.\n"
|
|
- storeConfig = true
|
|
|
|
} else if reqStatusCode == 403 {
|
|
} else if reqStatusCode == 403 {
|
|
return "", fmt.Errorf("Login: Your account hasn't been activated. " +
|
|
return "", fmt.Errorf("Login: Your account hasn't been activated. " +
|
|
"Please check your e-mail for a confirmation link.")
|
|
"Please check your e-mail for a confirmation link.")
|
|
@@ -166,11 +164,7 @@ func Login(authConfig *AuthConfig) (string, error) {
|
|
}
|
|
}
|
|
if resp.StatusCode == 200 {
|
|
if resp.StatusCode == 200 {
|
|
status = "Login Succeeded\n"
|
|
status = "Login Succeeded\n"
|
|
- storeConfig = true
|
|
|
|
} else if resp.StatusCode == 401 {
|
|
} else if resp.StatusCode == 401 {
|
|
- if err := SaveConfig(authConfig.rootPath, "", ""); err != nil {
|
|
|
|
- return "", err
|
|
|
|
- }
|
|
|
|
return "", fmt.Errorf("Wrong login/password, please try again")
|
|
return "", fmt.Errorf("Wrong login/password, please try again")
|
|
} else {
|
|
} else {
|
|
return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body,
|
|
return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body,
|
|
@@ -182,11 +176,5 @@ func Login(authConfig *AuthConfig) (string, error) {
|
|
} else {
|
|
} else {
|
|
return "", fmt.Errorf("Unexpected status code [%d] : %s", reqStatusCode, reqBody)
|
|
return "", fmt.Errorf("Unexpected status code [%d] : %s", reqStatusCode, reqBody)
|
|
}
|
|
}
|
|
- if storeConfig {
|
|
|
|
- authStr := EncodeAuth(authConfig)
|
|
|
|
- if err := SaveConfig(authConfig.rootPath, authStr, authConfig.Email); err != nil {
|
|
|
|
- return "", err
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
return status, nil
|
|
return status, nil
|
|
}
|
|
}
|