ltworf / localslackirc

IRC gateway for slack, running on localhost for one user. The project is now on codeberg.
https://codeberg.org/ltworf/localslackirc
GNU General Public License v3.0
138 stars 24 forks source link

keep getting invalid_auth even when verifying that it works #456

Closed sahal closed 6 months ago

sahal commented 6 months ago

Hello, I've set a config envvars file with the following configs:

TOKEN="xoxc-long-string"
COOKIE="d=xoxd-longstring;"
DEBUG=1

I'm running localslackirc via docker using the instructions in the README.

$ docker run -p 9007:9007 --name=slackworkspacename --env-file localslackirc.d/slackworkspacename localslackirc
Traceback (most recent call last):
  File "/localslackirc/localslackirc", line 24, in <module>
    main()
  File "/localslackirc/irc.py", line 1167, in main
    asyncio.run(restart_listener_loop())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/localslackirc/irc.py", line 1163, in restart_listener_loop
    await irc_listener()
  File "/localslackirc/irc.py", line 1103, in irc_listener
    await sl_client.login()
  File "/localslackirc/slack.py", line 430, in login
    self.login_info = await self.client.login(15)
  File "/localslackirc/slackclient/client.py", line 117, in login
    raise SlackLoginError(reply=login_data)
slackclient.exceptions.SlackLoginError: message=None reply={'ok': False, 'error': 'invalid_auth'}

I know that this TOKEN and COOKIE pair works, as I've used both in slackdump and wee-slack.

ltworf commented 6 months ago

are you using the latest version?

sahal commented 6 months ago

I'm using the code tagged 1.30, which I believe points to the latest commit on the master branch.

$ git status
HEAD detached at 1.30
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        localslackirc.d/<ACTUAL_WORKSPACE_NAME_REDACTED>

nothing added to commit but untracked files present (use "git add" to track)
(ins)sahal@shakuntala[~/code/localslackirc]$ docker build  --no-cache -t localslackirc -f docker/Dockerfile .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  2.297MB
Step 1/10 : FROM python:3.10-slim
 ---> 797a4d7093b1
Step 2/10 : RUN mkdir -p /localslackirc/slackclient
 ---> Running in 89fff032e188
 ---> Removed intermediate container 89fff032e188
 ---> 238694e2f288
Step 3/10 : RUN adduser --group --system localslackirc
 ---> Running in 9c1f80ca66c3
Adding system user `localslackirc' (UID 100) ...
Adding new group `localslackirc' (GID 101) ...
Adding new user `localslackirc' (UID 100) with group `localslackirc' ...
Not creating `/nonexistent'.
 ---> Removed intermediate container 9c1f80ca66c3
 ---> 54c7dcc06d65
Step 4/10 : COPY requirements.txt /localslackirc
 ---> dd5d4f21c6ba
Step 5/10 : RUN python3 -m pip install -r /localslackirc/requirements.txt a
 ---> Running in 3ec99a60f468
Collecting a
  Downloading a-1.0.tar.gz (564 bytes)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting typedload>=2.16
  Downloading typedload-2.27-py3-none-any.whl (53 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.6/53.6 kB 605.1 kB/s eta 0:00:00
Collecting emoji>=2.0.0
  Downloading emoji-2.11.0-py2.py3-none-any.whl (433 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 433.8/433.8 kB 2.6 MB/s eta 0:00:00
Collecting types-emoji
  Downloading types_emoji-2.1.0.3-py3-none-any.whl (3.5 kB)
Collecting websockets
  Downloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 130.2/130.2 kB 5.0 MB/s eta 0:00:00
Building wheels for collected packages: a
  Building wheel for a (setup.py): started
  Building wheel for a (setup.py): finished with status 'done'
  Created wheel for a: filename=a-1.0-py3-none-any.whl size=1153 sha256=a0c968434ae17e9f818e05dd4945113a92755866f0060c89f0f8530a552c8352
  Stored in directory: /root/.cache/pip/wheels/59/a7/38/7f4b673aac35f69ec66d368db90f7779212c4694210924b8cd
Successfully built a
Installing collected packages: types-emoji, a, websockets, typedload, emoji
Successfully installed a-1.0 emoji-2.11.0 typedload-2.27 types-emoji-2.1.0.3 websockets-12.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

[notice] A new release of pip is available: 23.0.1 -> 24.0
[notice] To update, run: pip install --upgrade pip
 ---> Removed intermediate container 3ec99a60f468
 ---> 611dff1dd862
Step 6/10 : COPY *.py /localslackirc/
 ---> 68d9b471d25a
Step 7/10 : COPY localslackirc /localslackirc/
 ---> bc789d455ae9
Step 8/10 : COPY slackclient/*.py /localslackirc/slackclient/
 ---> 9c61175cb180
Step 9/10 : USER localslackirc
 ---> Running in bfc3d0d4b042
 ---> Removed intermediate container bfc3d0d4b042
 ---> e7b6773c6a7f
Step 10/10 : ENTRYPOINT PYTHONPATH=/localslackirc python3 /localslackirc/localslackirc -o -i "0.0.0.0"
 ---> Running in 643c7d5cd511
 ---> Removed intermediate container 643c7d5cd511
 ---> 6721590bfd88
Successfully built 6721590bfd88
Successfully tagged localslackirc:latest
(ins)sahal@shakuntala[~/code/localslackirc]$ docker run -p 9007:9007 --name=<ACTUAL_WORKSPACE_NAME_REDACTED> --env-file localslackirc.d/<ACTUAL_WORKSPACE_NAME_REDACTED> localslackirc
Traceback (most recent call last):
  File "/localslackirc/localslackirc", line 24, in <module>
    main()
  File "/localslackirc/irc.py", line 1167, in main
    asyncio.run(restart_listener_loop())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/localslackirc/irc.py", line 1163, in restart_listener_loop
    await irc_listener()
  File "/localslackirc/irc.py", line 1103, in irc_listener
    await sl_client.login()
  File "/localslackirc/slack.py", line 430, in login
    self.login_info = await self.client.login(15)
  File "/localslackirc/slackclient/client.py", line 117, in login
    raise SlackLoginError(reply=login_data)
slackclient.exceptions.SlackLoginError: message=None reply={'ok': False, 'error': 'invalid_auth'}
(cmd)sahal@shakuntala[~/code/localslackirc]$ 

The "IRC" client I'm using is pidgin configured as follows:

pidgin_pg1 pidgin_pg2

I've tried with irssi too with the recommend config and that's failed in the same exact way.

ltworf commented 6 months ago

Ok I asked because there was an issue that got fixed in a previous version, when slack changed the needed headers to authenticate.

Unfortunately I can't do anything to debug this for you. I use mine daily and it works.

I don't use the docker version, I install it from debian myself. It could be that something is broken there and the env vars are no longer passed correctly?

If not you could add some print() in the code to show the full request that it does, and compare to what your other software does and possibly come back to me and tell me what needs to be changed.

Since it works for me and I have no way of trying it myself, I don't think I can help.

sahal commented 6 months ago

Based on your tip, I tried two things. One is I built a docker image based off of debian:testing-slim and installed the package from your releases page.:

FROM debian:testing-slim

ARG DEB_VERSION="1.30"

RUN useradd -ms /bin/bash localslackirc

RUN apt-get update -y && \
    apt-get upgrade -y && \
    apt-get install -y wget python3-typedload python3-websockets python3-minimal && \
    wget -O localslackirc.deb "https://github.com/ltworf/localslackirc/releases/download/${DEB_VERSION}/localslackirc_${DEB_VERSION}-1_all.deb" && \
    dpkg -i localslackirc.deb && \
    apt-get clean  && rm -rf /var/lib/apt/lists/*

USER localslackirc

ENTRYPOINT localslackirc -o -i "0.0.0.0"

This failed the exact same way as before.

I then attempted the print() line you suggested. I just added a print statement to print the headers:

diff --git a/slackclient/client.py b/slackclient/client.py
index 6af95b2..529ece3 100644
--- a/slackclient/client.py
+++ b/slackclient/client.py
@@ -102,7 +102,7 @@ class SlackClient:
         }
         if self._cookie:
             headers['cookie'] = self._cookie
-
+        print(headers)
         return await self._request.post(request, headers, post_data, timeout)

     async def login(self, timeout: float = 0.0) -> LoginInfo:

Adding the print() statement led me to the solution. Looks like the documented way to write the environment config file is incorrect, in certain circumstances. From README.md:

   145  So, for example (the real values will be longer):
   146  
   147  ```
   148  TOKEN=xoxc-1111111
   149  COOKIE="d=xoxd-1111111;"
   150  ```

When you dump the headers using the print statement -- its clear that the ENV vars are passed exactly as specified in the ENV var file, i.e. without stripping the quotation marks. I did some more research on this, and apparently this is a Docker thing: Don't Quote Environment Variables in Docker

If I quote either TOKEN or COOKIE, connecting to the Slack workspace fails because the Auth Bearer token and Cookie headers include extraneous quotes.

Sidenote: I did attempt this with podman/buildah and ran into the same issue with quoting env vars. I think the reason why you had no issues was because if your config file/environment was set up locally, your shell probably removes the quotes once the envvar is set. (as it should, afaik)

Not sure if you want to fix this in documentation or in code or just leave it as its my own user error. Either way, this is resolved for me.

Thank you for your help! Very excited to use this tool moving forward.

ltworf commented 6 months ago

Ah I see the problem. I wrote the README with bash syntax in mind, where quotes are needed or the ";" is interpreted as the end of the instruction.

Also in the "settings" file that on debian goes in /etc/localslackirc.d the syntax used by systemd to load env vars accepts quotes.

Since it seems that the settings file I use works fine also without quotes, I've removed the quotes from the README and added a reminder to quote them in shell.