Setup the connection to the LDAP server and authenticate the user.
(self)
| 114 | return self.__auto_create_user(user_email) |
| 115 | |
| 116 | def connect(self): |
| 117 | """Setup the connection to the LDAP server and authenticate the user. |
| 118 | """ |
| 119 | status, server = self._configure_server() |
| 120 | |
| 121 | if not status: |
| 122 | return status, server |
| 123 | |
| 124 | auto_bind = AUTO_BIND_TLS_BEFORE_BIND if self.start_tls \ |
| 125 | else AUTO_BIND_NO_TLS |
| 126 | |
| 127 | # Create the connection |
| 128 | try: |
| 129 | if self.anonymous_bind: |
| 130 | self.conn = Connection(server, |
| 131 | auto_bind=auto_bind, |
| 132 | authentication=ANONYMOUS |
| 133 | ) |
| 134 | else: |
| 135 | self.conn = Connection(server, |
| 136 | user=self.bind_user, |
| 137 | password=self.bind_pass, |
| 138 | auto_bind=auto_bind, |
| 139 | authentication=SIMPLE |
| 140 | ) |
| 141 | |
| 142 | except LDAPSocketOpenError as e: |
| 143 | current_app.logger.exception( |
| 144 | ERROR_CONNECTING_LDAP_SERVER.format(e)) |
| 145 | return False, ERROR_CONNECTING_LDAP_SERVER.format(e.args[0]) |
| 146 | except LDAPBindError as e: |
| 147 | current_app.logger.exception( |
| 148 | "Error binding to the LDAP server.") |
| 149 | return False, gettext("Error binding to the LDAP server: {}\n". |
| 150 | format(e.args[0])) |
| 151 | except LDAPStartTLSError as e: |
| 152 | current_app.logger.exception( |
| 153 | "Error starting TLS: {}\n".format(e)) |
| 154 | return False, gettext("Error starting TLS: {}\n" |
| 155 | ).format(e.args[0]) |
| 156 | except Exception as e: |
| 157 | current_app.logger.exception( |
| 158 | ERROR_CONNECTING_LDAP_SERVER.format(e)) |
| 159 | return False, ERROR_CONNECTING_LDAP_SERVER.format(e.args[0]) |
| 160 | |
| 161 | return True, None |
| 162 | |
| 163 | def login(self, form): |
| 164 | user = getattr(form, 'user', None) |
no test coverage detected