def authenticate(self, retries=3):
        """Set API token by authenticating via username/password.

        :param retries: Number of authentication attempts to make before giving up as an int.
        :return: Learned API token
        :rtype: string
        """

        username = self.username or self._getuser(self.base)
        password = self.password

        while retries > 0:
            if password is None:
                password = self._getpass(self.base, username)

            try:
                resp = self.post(self.base+'/authenticate', params={'username': username, 'password': password})

                schema = UserSchema()
                u = self.decode(schema, resp)

                if u.token is not None:
                    self.lock.acquire()
                    self.token = u.token
                    self.lock.release()
                    break
            except CDRouterError as cde:
                password = None
                retries -= 1
                if retries == 0:
                    raise cde

        return self.token