|
31 | 31 | package scram |
32 | 32 |
|
33 | 33 | import ( |
34 | | - "bytes" |
35 | 34 | "errors" |
36 | 35 |
|
37 | 36 | xdg "github.com/xdg-go/scram" |
@@ -59,8 +58,6 @@ import ( |
59 | 58 | // } |
60 | 59 | // |
61 | 60 | type Client struct { |
62 | | - out bytes.Buffer |
63 | | - err error |
64 | 61 | conv *xdg.ClientConversation |
65 | 62 | } |
66 | 63 |
|
@@ -97,48 +94,38 @@ func NewMethod(methodString string) (*Method, error) { |
97 | 94 | // |
98 | 95 | // method, _ := scram.NewMethod("SCRAM-SHA-1") |
99 | 96 | // |
100 | | -// client := scram.NewClient(method, user, pass) |
| 97 | +// client, _ := scram.NewClient(method, user, pass) |
101 | 98 | // |
102 | | -func NewClient(method *Method, user, pass string) *Client { |
103 | | - var client *xdg.Client |
104 | | - var err error |
| 99 | +func NewClient(method *Method, user, pass string) (client *Client, err error) { |
| 100 | + var internalClient *xdg.Client |
105 | 101 |
|
106 | 102 | switch method.method { |
107 | 103 | case ScramSha1: |
108 | | - client, err = xdg.SHA1.NewClient(user, pass, "") |
| 104 | + internalClient, err = xdg.SHA1.NewClient(user, pass, "") |
109 | 105 | case ScramSha256: |
110 | | - client, err = xdg.SHA256.NewClient(user, pass, "") |
| 106 | + internalClient, err = xdg.SHA256.NewClient(user, pass, "") |
111 | 107 | } |
112 | 108 |
|
113 | | - c := &Client{ |
114 | | - conv: client.NewConversation(), |
115 | | - err: err, |
| 109 | + client = &Client{ |
| 110 | + conv: internalClient.NewConversation(), |
116 | 111 | } |
117 | | - c.out.Grow(256) |
118 | | - return c |
| 112 | + return |
119 | 113 | } |
120 | 114 |
|
121 | | -// Out returns the data to be sent to the server in the current step. |
122 | | -func (c *Client) Out() []byte { |
123 | | - if c.out.Len() == 0 { |
124 | | - return []byte{} |
125 | | - } |
126 | | - return c.out.Bytes() |
127 | | -} |
128 | | - |
129 | | -// Err returns the error that occurred, or nil if there were no errors. |
130 | | -func (c *Client) Err() error { |
131 | | - return c.err |
| 115 | +// Implement saslStepper (auth.go) |
| 116 | +type saslStepper interface { |
| 117 | + Step(serverData []byte) (clientData []byte, done bool, err error) |
| 118 | + Close() |
132 | 119 | } |
133 | 120 |
|
134 | | -// Step processes the incoming data from the server and makes the |
135 | | -// next round of data for the server available via Client.Out. |
136 | | -// Step returns false if there are no errors and more data is |
137 | | -// still expected. |
138 | | -func (c *Client) Step(in []byte) bool { |
| 121 | +// Step progresses the underlying SASL SCRAM process |
| 122 | +func (c *Client) Step(serverData []byte) (clientData []byte, done bool, err error) { |
139 | 123 | var resp string |
140 | | - c.out.Reset() |
141 | | - resp, c.err = c.conv.Step(string(in)) |
142 | | - _, c.err = c.out.Write([]byte(resp)) |
143 | | - return c.conv.Valid() || c.err != nil |
| 124 | + resp, err = c.conv.Step(string(serverData)) |
| 125 | + clientData = []byte(resp) |
| 126 | + done = c.conv.Done() |
| 127 | + return |
144 | 128 | } |
| 129 | + |
| 130 | +// Close is a no opp to fit the saslStepper interface |
| 131 | +func (c *Client) Close() {} |
0 commit comments