-
Notifications
You must be signed in to change notification settings - Fork 17
/
session.go
90 lines (84 loc) · 2.49 KB
/
session.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
package epp
import (
"bytes"
"encoding/xml"
)
// Login initializes an authenticated EPP session.
// https://tools.ietf.org/html/rfc5730#section-2.9.1.1
func (c *Conn) Login(user, password, newPassword string) error {
err := c.writeLogin(user, password, newPassword)
if err != nil {
return err
}
res, err := c.readResponse()
if err != nil {
return err
}
// We always have a .Result in our non-pointer, but it might be meaningless.
// We might not have read anything. We think that the worst case is we
// have the same zero values we'd get without the assignment-even-in-error-case.
c.m.Lock()
c.LoginResult = res.Result
c.m.Unlock()
return err
}
func (c *Conn) writeLogin(user, password, newPassword string) error {
ver, lang := "1.0", "en"
if len(c.Greeting.Versions) > 0 {
ver = c.Greeting.Versions[0]
}
if len(c.Greeting.Languages) > 0 {
lang = c.Greeting.Languages[0]
}
x, err := encodeLogin(user, password, newPassword, ver, lang, c.Greeting.Objects, c.Greeting.Extensions)
if err != nil {
return err
}
return c.writeRequest(x)
}
func encodeLogin(user, password, newPassword, version, language string, objects, extensions []string) ([]byte, error) {
buf := bytes.NewBufferString(xmlCommandPrefix)
buf.WriteString(`<login><clID>`)
xml.EscapeText(buf, []byte(user))
buf.WriteString(`</clID><pw>`)
xml.EscapeText(buf, []byte(password))
if len(newPassword) > 0 {
buf.WriteString(`</pw><newPW>`)
xml.EscapeText(buf, []byte(newPassword))
buf.WriteString(`</newPW><options><version>`)
} else {
buf.WriteString(`</pw><options><version>`)
}
xml.EscapeText(buf, []byte(version))
buf.WriteString(`</version><lang>`)
xml.EscapeText(buf, []byte(language))
buf.WriteString(`</lang></options><svcs>`)
for _, o := range objects {
buf.WriteString(`<objURI>`)
xml.EscapeText(buf, []byte(o))
buf.WriteString(`</objURI>`)
}
if len(extensions) > 0 {
buf.WriteString(`<svcExtension>`)
for _, o := range extensions {
buf.WriteString(`<extURI>`)
xml.EscapeText(buf, []byte(o))
buf.WriteString(`</extURI>`)
}
buf.WriteString(`</svcExtension>`)
}
buf.WriteString(`</svcs></login>`)
buf.WriteString(xmlCommandSuffix)
return buf.Bytes(), nil
}
// Logout sends a <logout> command to terminate an EPP session.
// https://tools.ietf.org/html/rfc5730#section-2.9.1.2
func (c *Conn) Logout() error {
err := c.writeRequest(xmlLogout)
if err != nil {
return err
}
_, err = c.readResponse()
return err
}
var xmlLogout = []byte(xmlCommandPrefix + `<logout/>` + xmlCommandSuffix)