コード例 #1
0
ファイル: login.go プロジェクト: yepengxj/datahub-notebook
func Login(login bool, args []string) (err error) {
	fmt.Printf("login: "******"\r\n")
	fmt.Printf("password: "******"[%s]:[%s]\n", string(loginName), string(pass))

	User.userName = string(loginName)
	//User.password = string(pass)
	User.password = fmt.Sprintf("%x", md5.Sum(pass))

	User.b64 = base64.StdEncoding.EncodeToString([]byte(User.userName + ":" + User.password))
	//fmt.Printf("%s\n%s:%s\n", User.b64, User.userName, User.password)

	//req.Header.Set("Authorization", "Basic "+os.Getenv("DAEMON_USER_AUTH_INFO"))
	userJson := UserForJson{Username: User.userName}
	jsondata, _ := json.Marshal(userJson)

	resp, err := commToDaemon("get", "/users/auth", jsondata) //users/auth
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	//fmt.Println("login return", resp.StatusCode)
	if resp.StatusCode == 200 {
		Logged = true
		if login {
			fmt.Println("login success.")
		}
		return
	} else {
		if resp.StatusCode == 401 && login {
			fmt.Println("login failed.")
		}
		return fmt.Errorf("ERROR %d: login failed.", resp.StatusCode)
	}
	/*
		body, _ := ioutil.ReadAll(resp.Body)
		if resp.StatusCode != 200 {
			//fmt.Println(resp.StatusCode, ShowMsgResp(body, false))
			fmt.Println(resp.StatusCode)
		}

		if resp.StatusCode == 401 {
			return fmt.Errorf(string(body))
		}
		return fmt.Errorf("ERROR %d: login failed.", resp.StatusCode)
	*/
}
コード例 #2
0
ファイル: login.go プロジェクト: asiainfoLDP/datahub
func Login(login bool, args []string) (err error) {

	f := mflag.NewFlagSet("datahub login", mflag.ContinueOnError)
	f.Usage = loginUsage
	if err := f.Parse(args); err != nil {
		//fmt.Println(err)
		return err
	}
	if len(args) == 0 {
		fmt.Println("Please login")
		loginUsage()
		return errors.New(ErrMsgArgument)
	}

	var prefix string
	if p, ok := ServerPrefix[args[0]]; ok {
		prefix = p
	} else {
		fmt.Println(ErrMsgArgument)
		loginUsage()
		return errors.New(ErrMsgArgument)
	}

	fmt.Printf("login as: ")
	reader := bufio.NewReader(os.Stdin)
	//loginName, _ := reader.ReadString('\n')
	loginName, _ := reader.ReadBytes('\n')

	loginName = append([]byte(prefix), bytes.TrimRight(loginName, "\r\n")...)

	fmt.Printf("password: "******"[%s]:[%s]\n", string(loginName), string(pass))

	User.userName = string(loginName)
	//User.password = string(pass)
	User.password = fmt.Sprintf("%x", md5.Sum(pass))

	User.b64 = base64.StdEncoding.EncodeToString([]byte(User.userName + ":" + User.password))
	//fmt.Printf("%s\n%s:%s\n", User.b64, User.userName, User.password)

	//req.Header.Set("Authorization", "Basic "+os.Getenv("DAEMON_USER_AUTH_INFO"))
	userJson := UserForJson{Username: User.userName}
	jsondata, _ := json.Marshal(userJson)

	resp, err := commToDaemon("get", "/users/auth", jsondata) //users/auth
	if err != nil {
		fmt.Println(err)
		return err
	}
	defer resp.Body.Close()
	//fmt.Println("login return", resp.StatusCode)
	if resp.StatusCode == http.StatusOK {
		Logged = true
		if login {
			fmt.Println("DataHub : login success.")
		}
		return
	} else if resp.StatusCode == http.StatusForbidden {

		result := &ds.Result{}
		objloginerr := &Loginerr{}
		result.Data = objloginerr
		body, _ := ioutil.ReadAll(resp.Body)
		if err = json.Unmarshal(body, result); err != nil {
			fmt.Println("Error :", err)
			return err
		} else {
			retrytimes, _ := strconv.Atoi(objloginerr.Retrytimes)
			leftchance := 5 - retrytimes
			switch leftchance {
			case 0:
				fmt.Printf("%s\nno chance left.\n", result.Msg)
			case 1:
				fmt.Printf("%s\n1 chance left.\n", result.Msg)
			default:
				fmt.Printf("%s\n%v chances left.\n", result.Msg, leftchance)
			}
			fmt.Println(ErrLoginFailed)
			return errors.New(ErrLoginFailed)
		}
	} else {

		fmt.Println(ErrLoginFailed)
		if /*resp.StatusCode == 401 &&*/ login {
			body, _ := ioutil.ReadAll(resp.Body)
			fmt.Println(string(body))
		}
		return errors.New(ErrLoginFailed)
	}
	/*
		body, _ := ioutil.ReadAll(resp.Body)
		if resp.StatusCode != 200 {
			//fmt.Println(resp.StatusCode, ShowMsgResp(body, false))
			fmt.Println(resp.StatusCode)
		}

		if resp.StatusCode == 401 {
			return fmt.Errorf(string(body))
		}
		return fmt.Errorf("ERROR %d: login failed.", resp.StatusCode)
	*/
}