1/* Copyright 2016 Software Freedom Conservancy Inc.
2 *
3 * This software is licensed under the GNU Lesser General Public License
4 * (version 2.1 or later).  See the COPYING file in this distribution.
5 */
6
7/**
8 * SASL's PLAIN authentication schema implemented as an {@link Authenticator}.
9 *
10 * See [[http://tools.ietf.org/html/rfc4616]]
11 */
12
13public class Geary.Smtp.PlainAuthenticator : Geary.Smtp.Authenticator {
14    private static uint8[] nul = { '\0' };
15
16    public PlainAuthenticator(Credentials credentials) {
17        base ("PLAIN", credentials);
18    }
19
20    public override Request initiate() {
21        return new Request(Command.AUTH, { "PLAIN" });
22    }
23
24    public override Memory.Buffer? challenge(int step, Response response) throws SmtpError {
25        // only a single challenge is issued in PLAIN
26        if (step > 0)
27            return null;
28
29        Memory.GrowableBuffer growable = new Memory.GrowableBuffer();
30        // skip the "authorize" field, which we don't support
31        growable.append(nul);
32        growable.append(credentials.user.data);
33        growable.append(nul);
34        growable.append((credentials.token ?? "").data);
35
36        // convert to Base64
37        return new Memory.StringBuffer(Base64.encode(growable.get_bytes().get_data()));
38    }
39}
40
41