Ejemplo n.º 1
0
func AuthLogin(c *cli.Context, log logging.Logger, _ string) (int, error) {
	kodingURL, err := url.Parse(c.String("baseurl"))
	if err != nil {
		return 1, fmt.Errorf("%q is not a valid URL value: %s\n", c.String("koding"), err)
	}

	k, ok := configstore.List()[config.ID(kodingURL.String())]
	if !ok {
		k = &config.Konfig{
			Endpoints: &config.Endpoints{
				Koding: config.NewEndpointURL(kodingURL),
			},
		}
	}

	if err := configstore.Use(k); err != nil {
		return 1, err
	}

	// We create here a kloud client instead of using kloud.DefaultClient
	// in order to handle first-time login attempts where configuration
	// for kloud does not yet exist.
	kloudClient := &kloud.Client{
		Transport: &kloud.KiteTransport{
			Konfig: k,
			Log:    log,
		},
	}

	testKloudHook(kloudClient)

	authClient := &auth.Client{
		Kloud: kloudClient,
	}

	teamClient := &team.Client{
		Kloud: kloudClient,
	}

	// If we already own a valid kite.key, it means we were already
	// authenticated and we just call kloud using kite.key authentication.
	err = kloudClient.Transport.(stack.Validator).Valid()

	log.Debug("auth: transport test: %s", err)

	opts := &auth.LoginOptions{
		Team: c.String("team"),
	}

	if err != nil {
		opts.Username, err = helper.Ask("Username [%s]: ", config.CurrentUser.Username)
		if err != nil {
			return 1, err
		}

		if opts.Username == "" {
			opts.Username = config.CurrentUser.Username
		}

		for {
			opts.Password, err = helper.AskSecret("Password [***]: ")
			if err != nil {
				return 1, err
			}
			if opts.Password != "" {
				break
			}
		}
	}

	fmt.Fprintln(os.Stderr, "Logging to", kodingURL, "...")

	resp, err := authClient.Login(opts)
	if err != nil {
		return 1, fmt.Errorf("error logging into your Koding account: %v", err)
	}

	if resp.KiteKey != "" {
		k.KiteKey = resp.KiteKey
		k.Endpoints = resp.Metadata.Endpoints

		if err := configstore.Use(k); err != nil {
			return 1, err
		}
	}

	teamClient.Use(&team.Team{Name: resp.GroupName})

	if c.Bool("json") {
		enc := json.NewEncoder(os.Stdout)
		enc.SetIndent("", "\t")
		enc.Encode(resp)
	} else {
		fmt.Fprintln(os.Stdout, "Successfully logged in to the following team:", resp.GroupName)
	}

	return 0, nil
}
Ejemplo n.º 2
0
func checkAndAskRequiredFields(r *RegisterRequest) (*RegisterRequest, error) {
	var err error

	if r.Username == "" {
		r.Username, err = helper.Ask("Username : "******"" {
		r.FirstName, err = helper.Ask("FirstName : ")
		if err != nil {
			return nil, err
		}
	}
	if r.LastName == "" {
		r.LastName, err = helper.Ask("LastName : ")
		if err != nil {
			return nil, err
		}
	}
	if r.Password == "" {
		r.Password, err = helper.AskSecret("Password [***]: ")
		if err != nil {
			return nil, err
		}
	}
	if r.PasswordConfirm == "" {
		r.PasswordConfirm, err = helper.AskSecret("Confirm Password [***]: ")
		if err != nil {
			return nil, err
		}
		if r.PasswordConfirm != r.Password {
			return nil, fmt.Errorf("Different Password/ConfirmPassword ")
		}
	}
	if r.Email == "" {
		r.Email, err = helper.Ask("Email []: ")
		if err != nil {
			return nil, err
		}
	}
	if r.Slug == "" {
		r.Slug, err = helper.Ask("Team Name []: ")
		if err != nil {
			return nil, err
		}
	}
	if r.CompanyName == "" {
		r.CompanyName, err = helper.Ask("Company Name []: ")
		if err != nil {
			return nil, err
		}
	}
	if r.AlreadyMember == falseValue {
		r.AlreadyMember, err = helper.Ask("Are you already koding member? [%s or %s] : ", trueValue, falseValue)
		if err != nil {
			return nil, err
		}
		if r.AlreadyMember != trueValue && r.AlreadyMember != falseValue {
			return nil, fmt.Errorf("Valid input [%s or %s]", trueValue, falseValue)
		}
	}

	return r, nil
}