summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/modules/LDAPListenerBase.jsm
blob: 486dcaffbeac5d1ac5644629e5203cf5e5604c8e (plain)
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
/* -*- Mode: JavaScript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = ["LDAPListenerBase"];

/**
 * @implements {nsILDAPMessageListener}
 */
class LDAPListenerBase {
  /**
   * @see nsILDAPMessageListener
   */
  async onLDAPInit() {
    let outPassword = {};
    if (this._directory.authDn && this._directory.saslMechanism != "GSSAPI") {
      // If authDn is set, we're expected to use it to get a password.
      let bundle = Services.strings.createBundle(
        "chrome://mozldap/locale/ldap.properties"
      );

      let authPrompt = Services.ww.getNewAuthPrompter(
        Services.wm.getMostRecentWindow(null)
      );
      await authPrompt.asyncPromptPassword(
        bundle.GetStringFromName("authPromptTitle"),
        bundle.formatStringFromName("authPromptText", [
          this._directory.lDAPURL.host,
        ]),
        this._directory.lDAPURL.spec,
        Ci.nsIAuthPrompt.SAVE_PASSWORD_PERMANENTLY,
        outPassword
      );
    }
    this._operation.init(this._connection, this, null);

    if (this._directory.saslMechanism != "GSSAPI") {
      this._operation.simpleBind(outPassword.value);
      return;
    }

    // Handle GSSAPI now.
    this._operation.saslBind(
      `ldap@${this._directory.lDAPURL.host}`,
      "GSSAPI",
      "sasl-gssapi"
    );
  }

  /**
   * Handler of nsILDAPMessage.RES_BIND message.
   *
   * @param {nsILDAPMessage} msg - The received LDAP message.
   */
  _onLDAPBind(msg) {
    let errCode = msg.errorCode;
    if (
      errCode == Ci.nsILDAPErrors.INAPPROPRIATE_AUTH ||
      errCode == Ci.nsILDAPErrors.INVALID_CREDENTIALS
    ) {
      // Login failed, remove any existing login(s).
      let ldapUrl = this._directory.lDAPURL;
      let logins = Services.logins.findLogins(
        ldapUrl.prePath,
        "",
        ldapUrl.spec
      );
      for (let login of logins) {
        Services.logins.removeLogin(login);
      }
      // Trigger the auth prompt.
      this.onLDAPInit();
      return;
    }
    if (errCode != Ci.nsILDAPErrors.SUCCESS) {
      this._actionOnBindFailure();
      return;
    }
    this._actionOnBindSuccess();
  }

  /**
   * @see nsILDAPMessageListener
   * @abstract
   */
  onLDAPMessage() {
    throw new Components.Exception(
      `${this.constructor.name} does not implement onLDAPMessage.`,
      Cr.NS_ERROR_NOT_IMPLEMENTED
    );
  }

  /**
   * Callback when BindResponse succeeded.
   *
   * @abstract
   */
  _actionOnBindSuccess() {
    throw new Components.Exception(
      `${this.constructor.name} does not implement _actionOnBindSuccess.`,
      Cr.NS_ERROR_NOT_IMPLEMENTED
    );
  }

  /**
   * Callback when BindResponse failed.
   *
   * @abstract
   */
  _actionOnBindFailure() {
    throw new Components.Exception(
      `${this.constructor.name} does not implement _actionOnBindFailure.`,
      Cr.NS_ERROR_NOT_IMPLEMENTED
    );
  }
}