func CreatePassword()

in dex/password.go [19:58]


func CreatePassword(email string, password string) error {
	conn, err := newGrpcConnection()
	if err != nil {
		return getApiClientError(err)
	}
	defer conn.Close()

	client := api.NewDexClient(conn)

	hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		err = fmt.Errorf("failed to bcrypt password: %s", err)
		return getError(err)
	}

	userId := base64.StdEncoding.EncodeToString([]byte(email))

	p := &api.Password{
		Email:    email,
		Hash:     hash,
		Username: email,
		UserId:   userId,
	}

	req := &api.CreatePasswordReq{
		Password: p,
	}

	if resp, err := client.CreatePassword(context.TODO(), req); err != nil || (resp != nil && resp.AlreadyExists) {
		if resp != nil && resp.AlreadyExists {
			err = fmt.Errorf("password for %s already exists", email)
			return getError(err)
		}

		err = fmt.Errorf("failed to create password %s", err)
		return getError(err)
	}

	return nil
}