-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
158 lines (143 loc) · 3.64 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package soap
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"os"
"strconv"
"golang.org/x/crypto/pkcs12"
)
// LoginIntfc is an interface for unified login structs
type LoginIntfc interface {
// Login returns sid (session id) in case of successfull login.
// SID is the first argument in the most of WSDL methods.
Login(SoapWebService) (*int, error)
}
type loginUserPassword struct {
username string
password string
policy string
}
// NewLoginUserPassword is a built-in function to create an instance of
// SoapLogin compatible struct. With authorization by CA SDM username, password, and policy.
func NewLoginUserPassword(username, password, policy string) *loginUserPassword {
return &loginUserPassword{
username: username,
password: password,
policy: policy,
}
}
func (l *loginUserPassword) Login(ws SoapWebService) (*int, error) {
if l.policy == "" {
resp, err := ws.Login(&Login{
Username: &l.username,
Password: &l.password,
})
if err != nil {
return nil, err
}
return resp.LoginReturn, nil
}
resp, err := ws.LoginService(&LoginService{
Username: &l.username,
Password: &l.password,
Policy: &l.policy,
})
if err != nil {
return nil, err
}
return resp.LoginServiceReturn, nil
}
type loginManaged struct {
policy string
cert string
}
// NewLoginManaged is a built-in function to create an instance of
// SoapLogin compatible struct. With authorization by CA SDM policy and certificate.
//
// Pre-requirement steps:
// 1. Create new Policy under Admin Tab > SOAP > Policies;
// 2. Specify Proxy Contact
// 3. Run `pdm_pki -p <POLICY_NAME> -f`
// 4. Distribute newly created `<POLICY_NAME>.p12` file
func NewLoginManaged(policy, cert string) *loginManaged {
return &loginManaged{
policy: policy,
cert: cert,
}
}
func (l *loginManaged) Login(ws SoapWebService) (*int, error) {
sign, err := l.readCert()
if err != nil {
return nil, err
}
resp, err := ws.LoginServiceManaged(&LoginServiceManaged{
Policy: &l.policy,
Encrypted_policy: sign,
})
if err != nil {
return nil, err
}
sid, err := strconv.Atoi(*resp.LoginServiceManagedReturn)
if err != nil {
return nil, err
}
return &sid, nil
}
func (l *loginManaged) readCert() (*string, error) {
data, err := os.ReadFile(l.cert)
if err != nil {
return nil, err
}
pk, _, err := pkcs12.Decode(data, l.policy)
if err != nil {
return nil, err
}
rsaPK := pk.(*rsa.PrivateKey)
h := crypto.Hash.New(crypto.SHA1)
h.Write([]byte(l.policy))
hashed := h.Sum(nil)
var signature []byte
signature, err = rsa.SignPKCS1v15(rand.Reader, rsaPK, crypto.SHA1, hashed)
if err != nil {
return nil, err
}
return StringPtr(base64.StdEncoding.EncodeToString(signature)), nil
}
type loginArtifact struct {
userid string
artifact string
}
// NewLoginArtifact is a built-in function to create an instance of
// SoapLogin compatible struct. With authorization by CA SDM userid and EEM artifact.
//
// *NOT IMPLEMENTED*
func NewLoginArtifact(userid, artifact string) *loginArtifact {
return &loginArtifact{
userid: userid,
artifact: artifact,
}
}
// Not Implemented
// I dont have EEM instance, so... pull requests are very appreciated u know
// Login with EEM artifact
func (l *loginArtifact) Login(ws SoapWebService) (*int, error) {
if l.artifact == "" {
art, err := ws.GetArtifact(&GetArtifact{
// I dont have EEM instance so this is not implemented yet
})
if err != nil {
return nil, err
}
l.artifact = *art.GetArtifactReturn
}
resp, err := ws.LoginWithArtifact(&LoginWithArtifact{
Userid: &l.userid,
Artifact: &l.artifact,
})
if err != nil {
return nil, err
}
return resp.LoginWithArtifactReturn, nil
}