Example #1
0
func TestPostAuth(t *testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	srv := &Server{
		runtime: runtime,
	}

	config := &auth.AuthConfig{
		Username: "******",
		Email:    "*****@*****.**",
	}

	authStr := auth.EncodeAuth(config)
	auth.SaveConfig(runtime.root, authStr, config.Email)

	r := httptest.NewRecorder()
	if err := getAuth(srv, API_VERSION, r, nil, nil); err != nil {
		t.Fatal(err)
	}

	authConfig := &auth.AuthConfig{}
	if err := json.Unmarshal(r.Body.Bytes(), authConfig); err != nil {
		t.Fatal(err)
	}

	if authConfig.Username != config.Username || authConfig.Email != config.Email {
		t.Errorf("The retrieve auth mismatch with the one set.")
	}
}
Example #2
0
// 'docker login': login / register a user to registry service.
func (cli *DockerCli) CmdLogin(args ...string) error {
	var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {
		char := make([]byte, 1)
		buffer := make([]byte, 64)
		var i = 0
		for i < len(buffer) {
			n, err := stdin.Read(char)
			if n > 0 {
				if char[0] == '\r' || char[0] == '\n' {
					stdout.Write([]byte{'\r', '\n'})
					break
				} else if char[0] == 127 || char[0] == '\b' {
					if i > 0 {
						if echo {
							stdout.Write([]byte{'\b', ' ', '\b'})
						}
						i--
					}
				} else if !unicode.IsSpace(rune(char[0])) &&
					!unicode.IsControl(rune(char[0])) {
					if echo {
						stdout.Write(char)
					}
					buffer[i] = char[0]
					i++
				}
			}
			if err != nil {
				if err != io.EOF {
					fmt.Fprintf(stdout, "Read error: %v\r\n", err)
				}
				break
			}
		}
		return string(buffer[:i])
	}
	var readAndEchoString = func(stdin io.Reader, stdout io.Writer) string {
		return readStringOnRawTerminal(stdin, stdout, true)
	}
	var readString = func(stdin io.Reader, stdout io.Writer) string {
		return readStringOnRawTerminal(stdin, stdout, false)
	}

	cmd := Subcmd("login", "[OPTIONS]", "Register or Login to the docker registry server")
	flUsername := cmd.String("u", "", "username")
	flPassword := cmd.String("p", "", "password")
	flEmail := cmd.String("e", "", "email")
	err := cmd.Parse(args)
	if err != nil {
		return nil
	}

	var oldState *term.State
	if *flUsername == "" || *flPassword == "" || *flEmail == "" {
		oldState, err = term.SetRawTerminal(cli.terminalFd)
		if err != nil {
			return err
		}
		defer term.RestoreTerminal(cli.terminalFd, oldState)
	}

	var (
		username string
		password string
		email    string
	)

	if *flUsername == "" {
		fmt.Fprintf(cli.out, "Username (%s): ", cli.authConfig.Username)
		username = readAndEchoString(cli.in, cli.out)
		if username == "" {
			username = cli.authConfig.Username
		}
	} else {
		username = *flUsername
	}
	if username != cli.authConfig.Username {
		if *flPassword == "" {
			fmt.Fprintf(cli.out, "Password: "******"" {
				return fmt.Errorf("Error : Password Required")
			}
		} else {
			password = *flPassword
		}

		if *flEmail == "" {
			fmt.Fprintf(cli.out, "Email (%s): ", cli.authConfig.Email)
			email = readAndEchoString(cli.in, cli.out)
			if email == "" {
				email = cli.authConfig.Email
			}
		} else {
			email = *flEmail
		}
	} else {
		password = cli.authConfig.Password
		email = cli.authConfig.Email
	}
	if oldState != nil {
		term.RestoreTerminal(cli.terminalFd, oldState)
	}
	cli.authConfig.Username = username
	cli.authConfig.Password = password
	cli.authConfig.Email = email

	body, statusCode, err := cli.call("POST", "/auth", cli.authConfig)
	if statusCode == 401 {
		cli.authConfig.Username = ""
		cli.authConfig.Password = ""
		cli.authConfig.Email = ""
		auth.SaveConfig(cli.authConfig)
		return err
	}
	if err != nil {
		return err
	}

	var out2 APIAuth
	err = json.Unmarshal(body, &out2)
	if err != nil {
		auth.LoadConfig(os.Getenv("HOME"))
		return err
	}
	auth.SaveConfig(cli.authConfig)
	if out2.Status != "" {
		fmt.Fprintf(cli.out, "%s\n", out2.Status)
	}
	return nil
}
Example #3
0
// 'docker login': login / register a user to registry service.
func (cli *DockerCli) CmdLogin(args ...string) error {
	cmd := Subcmd("login", "[OPTIONS] [SERVER]", "Register or Login to a docker registry server, if no server is specified \""+auth.IndexServerAddress()+"\" is the default.")

	var username, password, email string

	cmd.StringVar(&username, "u", "", "username")
	cmd.StringVar(&password, "p", "", "password")
	cmd.StringVar(&email, "e", "", "email")
	err := cmd.Parse(args)
	if err != nil {
		return nil
	}
	serverAddress := auth.IndexServerAddress()
	if len(cmd.Args()) > 0 {
		serverAddress, err = registry.ExpandAndVerifyRegistryUrl(cmd.Arg(0))
		if err != nil {
			return err
		}
		fmt.Fprintf(cli.out, "Login against server at %s\n", serverAddress)
	}

	promptDefault := func(prompt string, configDefault string) {
		if configDefault == "" {
			fmt.Fprintf(cli.out, "%s: ", prompt)
		} else {
			fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
		}
	}

	readInput := func(in io.Reader, out io.Writer) string {
		reader := bufio.NewReader(in)
		line, _, err := reader.ReadLine()
		if err != nil {
			fmt.Fprintln(out, err.Error())
			os.Exit(1)
		}
		return string(line)
	}

	cli.LoadConfigFile()
	authconfig, ok := cli.configFile.Configs[auth.IndexServerAddress()]
	if !ok {
		authconfig = auth.AuthConfig{}
	}

	if username == "" {
		promptDefault("Username", authconfig.Username)
		username = readInput(cli.in, cli.out)
		if username == "" {
			username = authconfig.Username
		}
	}
	if username != authconfig.Username {
		if password == "" {
			oldState, _ := term.SaveState(cli.terminalFd)
			fmt.Fprintf(cli.out, "Password: "******"\n")

			term.RestoreTerminal(cli.terminalFd, oldState)
			if password == "" {
				return fmt.Errorf("Error : Password Required")
			}
		}

		if email == "" {
			promptDefault("Email", authconfig.Email)
			email = readInput(cli.in, cli.out)
			if email == "" {
				email = authconfig.Email
			}
		}
	} else {
		password = authconfig.Password
		email = authconfig.Email
	}
	authconfig.Username = username
	authconfig.Password = password
	authconfig.Email = email
	authconfig.ServerAddress = serverAddress
	cli.configFile.Configs[serverAddress] = authconfig

	body, statusCode, err := cli.call("POST", "/auth", cli.configFile.Configs[serverAddress])
	if statusCode == 401 {
		delete(cli.configFile.Configs, serverAddress)
		auth.SaveConfig(cli.configFile)
		return err
	}
	if err != nil {
		return err
	}

	var out2 APIAuth
	err = json.Unmarshal(body, &out2)
	if err != nil {
		cli.configFile, _ = auth.LoadConfig(os.Getenv("HOME"))
		return err
	}
	auth.SaveConfig(cli.configFile)
	if out2.Status != "" {
		fmt.Fprintf(cli.out, "%s\n", out2.Status)
	}
	return nil
}
Example #4
0
// 'docker login': login / register a user to registry service.
func (cli *DockerCli) CmdLogin(args ...string) error {
	var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {
		char := make([]byte, 1)
		buffer := make([]byte, 64)
		var i = 0
		for i < len(buffer) {
			n, err := stdin.Read(char)
			if n > 0 {
				if char[0] == '\r' || char[0] == '\n' {
					stdout.Write([]byte{'\r', '\n'})
					break
				} else if char[0] == 127 || char[0] == '\b' {
					if i > 0 {
						if echo {
							stdout.Write([]byte{'\b', ' ', '\b'})
						}
						i--
					}
				} else if !unicode.IsSpace(rune(char[0])) &&
					!unicode.IsControl(rune(char[0])) {
					if echo {
						stdout.Write(char)
					}
					buffer[i] = char[0]
					i++
				}
			}
			if err != nil {
				if err != io.EOF {
					fmt.Fprintf(stdout, "Read error: %v\r\n", err)
				}
				break
			}
		}
		return string(buffer[:i])
	}
	var readAndEchoString = func(stdin io.Reader, stdout io.Writer) string {
		return readStringOnRawTerminal(stdin, stdout, true)
	}
	var readString = func(stdin io.Reader, stdout io.Writer) string {
		return readStringOnRawTerminal(stdin, stdout, false)
	}

	oldState, err := term.SetRawTerminal()
	if err != nil {
		return err
	}
	defer term.RestoreTerminal(oldState)

	cmd := Subcmd("login", "", "Register or Login to the docker registry server")
	if err := cmd.Parse(args); err != nil {
		return nil
	}

	var username string
	var password string
	var email string

	fmt.Print("Username (", cli.authConfig.Username, "): ")
	username = readAndEchoString(os.Stdin, os.Stdout)
	if username == "" {
		username = cli.authConfig.Username
	}
	if username != cli.authConfig.Username {
		fmt.Print("Password: "******"" {
			return fmt.Errorf("Error : Password Required")
		}

		fmt.Print("Email (", cli.authConfig.Email, "): ")
		email = readAndEchoString(os.Stdin, os.Stdout)
		if email == "" {
			email = cli.authConfig.Email
		}
	} else {
		email = cli.authConfig.Email
	}
	term.RestoreTerminal(oldState)

	cli.authConfig.Username = username
	cli.authConfig.Password = password
	cli.authConfig.Email = email

	body, _, err := cli.call("POST", "/auth", cli.authConfig)
	if err != nil {
		return err
	}

	var out2 APIAuth
	err = json.Unmarshal(body, &out2)
	if err != nil {
		auth.LoadConfig(os.Getenv("HOME"))
		return err
	}
	auth.SaveConfig(cli.authConfig)
	if out2.Status != "" {
		fmt.Print(out2.Status)
	}
	return nil
}