forked from containers/podman
/
auth.go
70 lines (62 loc) · 2.18 KB
/
auth.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package compat
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/containers/common/pkg/auth"
DockerClient "github.com/containers/image/v5/docker"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v4/libpod"
"github.com/containers/podman/v4/pkg/api/handlers/utils"
api "github.com/containers/podman/v4/pkg/api/types"
"github.com/containers/podman/v4/pkg/domain/entities"
docker "github.com/docker/docker/api/types"
)
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, http.StatusInternalServerError, fmt.Errorf("failed to parse request: %w", err))
return
}
skipTLS := types.NewOptionalBool(false)
if strings.HasPrefix(authConfig.ServerAddress, "https://localhost/") || strings.HasPrefix(authConfig.ServerAddress, "https://localhost:") || strings.HasPrefix(authConfig.ServerAddress, "localhost:") {
// support for local testing
skipTLS = types.NewOptionalBool(true)
}
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
sysCtx := runtime.SystemContext()
sysCtx.DockerInsecureSkipTLSVerify = skipTLS
loginOpts := &auth.LoginOptions{
Username: authConfig.Username,
Password: authConfig.Password,
Stdout: io.Discard,
NoWriteBack: true, // to prevent credentials to be written on disk
}
if err := auth.Login(r.Context(), sysCtx, loginOpts, []string{authConfig.ServerAddress}); err == nil {
utils.WriteResponse(w, http.StatusOK, entities.AuthReport{
IdentityToken: "",
Status: "Login Succeeded",
})
} else {
var msg string
// Return a 401 in case the credentials are wrong. The string
// compare is needed because `auth.Login` turns
// `ErrUnauthorizedForCredentials` into something friendly to
// the human eyes.
var unauthErr DockerClient.ErrUnauthorizedForCredentials
if errors.As(err, &unauthErr) {
msg = "401 Unauthorized"
} else {
msg = err.Error()
}
utils.WriteResponse(w, http.StatusInternalServerError, struct {
Message string `json:"message"`
}{
Message: "login attempt to " + authConfig.ServerAddress + " failed with status: " + msg,
})
}
}