-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials_windows.go
126 lines (108 loc) · 2.74 KB
/
credentials_windows.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//go:build windows
// +build windows
package credentials
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/danieljoos/wincred"
)
var (
ErrorDuplicateItem = errors.New("Secret already existed")
ErrorItemNotFound = wincred.ErrElementNotFound
)
func init() {
Current = &WindowsProvider{}
}
type WindowsProvider struct {
domain *Domain
}
func (p *WindowsProvider) ErrorWrap(url string, err error) error {
if err == nil {
return nil
}
if errors.Is(err, wincred.ErrElementNotFound) {
return fmt.Errorf("%w: %s: %v", ErrNotFound, url, err)
}
return fmt.Errorf("%w: %s", err, url)
}
func (p *WindowsProvider) SetDomain(domain *Domain) {
p.domain = domain
}
func (p *WindowsProvider) IsConfigured() bool {
return p.domain != nil
}
func (p *WindowsProvider) getTargetName(url string) string {
return p.domain.Service + " (" + url + ")"
}
func (p *WindowsProvider) Create(url, name, secret string) error {
_, existing, err := p.Retrieve(url)
if err != nil && !errors.Is(err, ErrNotFound) {
p.ErrorWrap(url, err)
}
if existing != "" {
return p.ErrorWrap(url, ErrDuplicate)
}
g := wincred.NewGenericCredential(p.getTargetName(url))
g.UserName = name
g.CredentialBlob = []byte(secret)
g.Persist = wincred.PersistLocalMachine
g.Attributes = []wincred.CredentialAttribute{
{
Keyword: "url",
Value: []byte(url),
},
{
Keyword: "access-group",
Value: []byte(p.domain.AccessGroup),
},
}
return g.Write()
}
func (p *WindowsProvider) matchAttributes(url string, g *wincred.GenericCredential) bool {
matchAccessGroup, matchUrl := false, false
for _, attr := range g.Attributes {
if strings.Compare(attr.Keyword, "url") == 0 &&
bytes.Equal(attr.Value, []byte(url)) {
matchUrl = true
}
if strings.Compare(attr.Keyword, "access-group") == 0 &&
bytes.Equal(attr.Value, []byte(p.domain.AccessGroup)) {
matchAccessGroup = true
}
}
return matchUrl && matchAccessGroup
}
func (p *WindowsProvider) Retrieve(url string) (string, string, error) {
g, err := wincred.GetGenericCredential(p.getTargetName(url))
if err != nil {
return "", "", p.ErrorWrap(url, err)
}
if g == nil {
return "", "", p.ErrorWrap(url, ErrNotFound)
}
if !(p.matchAttributes(url, g)) {
return "", "", p.ErrorWrap(url, ErrNotFound)
}
return g.UserName, string(g.CredentialBlob), nil
}
func (p *WindowsProvider) Update(url, name, secret string) error {
if err := p.Delete(url); err != nil {
return err
}
return p.Create(url, name, secret)
}
func (p *WindowsProvider) Delete(url string) error {
g, err := wincred.GetGenericCredential(p.getTargetName(url))
if err != nil && !errors.Is(err, ErrNotFound) {
return p.ErrorWrap(url, err)
}
if g == nil {
return nil
}
if !(p.matchAttributes(url, g)) {
return nil
}
return g.Delete()
}