forked from aquasecurity/fanal
/
token.go
46 lines (39 loc) · 1.18 KB
/
token.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
package image
import (
"context"
"github.com/containers/image/v5/pkg/docker/config"
imageTypes "github.com/containers/image/v5/types"
"github.com/goodwithtech/deckoder/types"
)
var (
registries []Registry
)
type Registry interface {
CheckOptions(domain string, option types.DockerOption) error
GetCredential(ctx context.Context) (string, string, error)
}
func RegisterRegistry(registry Registry) {
registries = append(registries, registry)
}
func GetToken(ctx context.Context, domain string, opt types.DockerOption) (auth *imageTypes.DockerAuthConfig) {
if opt.UserName != "" || opt.Password != "" {
return &imageTypes.DockerAuthConfig{Username: opt.UserName, Password: opt.Password}
}
if config, _ := config.GetCredentials(nil, domain); config.Username != "" {
return &config
}
// check registry which particular to get credential
for _, registry := range registries {
err := registry.CheckOptions(domain, opt)
if err != nil {
continue
}
username, password, err := registry.GetCredential(ctx)
if err != nil {
// only skip check registry if error occurred
break
}
return &imageTypes.DockerAuthConfig{Username: username, Password: password}
}
return nil
}