OokTech / TW5-Bob

A plugin that makes tiddlywiki a multi-user wiki on node
BSD 3-Clause "New" or "Revised" License
216 stars 28 forks source link

possible to use header to set username ? #195

Open cornernote opened 1 year ago

cornernote commented 1 year ago

I would like to use a header from nginx proxy to set the $:/status/UserName.

I have nginx setup like this:

upstream websocket {
    server tiddlywiki:8080;
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    location / {
        auth_basic "Login";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-BasicAuth-RemoteUser $remote_user;
    }
}

Importantly these lines:

        auth_basic "Login";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_set_header X-BasicAuth-RemoteUser $remote_user;

I tried like to run node tiddlywiki with the authenticated-user-header option, as described here:

https://tiddlywiki.com/static/WebServer%2520Parameter%253A%2520authenticated-user-header.html

Something like this:

node tiddlywiki --wsserver authenticated-user-header=X-RemoteUser

But that doesn't seem to do anything.

I'm looking for advise on if this is possible, if so then I would also be grateful for a gentle push in the right direction.


I am running:

and using

Before posting I read issue guidelines and:

cornernote commented 1 year ago

I added something like this in ws-server.js, and I can get the header value. How can I write it to a tiddler?

            if(request.headers['X-BasicAuth-RemoteUser']) {
                console.log("set state.authenticatedUsername to "+request.headers[request.headers['X-BasicAuth-RemoteUser']]);

                // what can i do here to write to the tiddler ? - this doesn't work...
                self.wiki.addTiddler({
                    title: "$:/status/UserName",
                    text: request.headers[header]});

                //state.authenticatedUsername = request.headers[header];
            }
cornernote commented 1 year ago

I got it working with a change to get-status.js:

...
exports.handler = function(request,response,state) {
   ...
  const header = 'X-BasicAuth-RemoteUser'.toLowerCase();
  const username = header && request.headers[header] ? request.headers[header] : '';

  const status = {
    ...
    username: username,
    ...
  }
  ...
}

}());

and to BrowserWSAdaptor.js

      ...
      $tw.wiki.addTiddler(new $tw.Tiddler({title:'$:/status/IsLoggedIn', text:data.logged_in}));
      $tw.wiki.addTiddler(new $tw.Tiddler({title:'$:/status/UserName', text:data.username})); // add this
      ...