summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/compat/auth.go
blob: e914301f468a65aea2d8f8c0db5b36f9a34d1969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package compat

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	DockerClient "github.com/containers/image/v5/docker"
	"github.com/containers/image/v5/types"
	"github.com/containers/podman/v3/pkg/api/handlers/utils"
	"github.com/containers/podman/v3/pkg/domain/entities"
	"github.com/containers/podman/v3/pkg/registries"
	docker "github.com/docker/docker/api/types"
	"github.com/pkg/errors"
)

func Auth(w http.ResponseWriter, r *http.Request) {
	var authConfig docker.AuthConfig
	err := json.NewDecoder(r.Body).Decode(&authConfig)
	if err != nil {
		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrapf(err, "failed to parse request"))
		return
	}

	skipTLS := types.NewOptionalBool(false)
	if strings.HasPrefix(authConfig.ServerAddress, "http://localhost/") || strings.HasPrefix(authConfig.ServerAddress, "http://localhost:") {
		// support for local testing
		skipTLS = types.NewOptionalBool(true)
	}

	fmt.Println("Authenticating with existing credentials...")
	sysCtx := types.SystemContext{
		AuthFilePath:                "",
		DockerCertPath:              "",
		DockerInsecureSkipTLSVerify: skipTLS,
		SystemRegistriesConfPath:    registries.SystemRegistriesConfPath(),
	}
	if err := DockerClient.CheckAuth(context.Background(), &sysCtx, authConfig.Username, authConfig.Password, authConfig.ServerAddress); err == nil {
		utils.WriteResponse(w, http.StatusOK, entities.AuthReport{
			IdentityToken: "",
			Status:        "Login Succeeded",
		})
	} else {
		utils.WriteResponse(w, http.StatusBadRequest, entities.AuthReport{
			IdentityToken: "",
			Status:        "login attempt to " + authConfig.ServerAddress + " failed with status: " + err.Error(),
		})
	}
}