OpenMarshal / npm-WebDAV-Server

WebDAV Server for npm
The Unlicense
256 stars 66 forks source link

How to interface with the authentication #134

Open Daxcor69 opened 9 months ago

Daxcor69 commented 9 months ago

I am looking to use mongo db as a authentication source for the server. I know nothing about TS and using classes and constructors. I am trying to understand the examples given but it just doesn't make any sense.

here is what Understand so far

const userManager = new webdav.SimpleUserManager();
const user = userManager.addUser("username", "password", false);

// Create a new server instance
const server = new webdav.WebDAVServer({
  // HTTP Basic authentication
  httpAuthentication: new webdav.HTTPBasicAuthentication(userManager, "Default realm"),
  rootFileSystem: new webdav.PhysicalFileSystem("/tmp/"),
});

When the sever starts, it adds the values for this user to some sort of memory table. Then when the requests comes it is compared and go or no go.

What I am looking for is the ability to get the incoming Basic auth values and look up my mongo table instead. I just can't figure out how to grab the values on connection, or does the server have to preload all the values ahead of any connection?

I have goggled around looking for some examples, if you have anything you can share would be most appreciative. Thanks,

Daxcor69 commented 9 months ago

Ok with the help of chatGPT here is what I got working. This might help someone else

const webdav = require("webdav-server").v2;

class SimpleUserManager extends webdav.SimpleUserManager {
  async getUserByNamePassword(name, password, callback) {
    let user = await User.findOne({ username: name }).select("+password");

    if (!user) {
      callback(new Error("Bad Authentication"));
    } else {
      if (await user.matchPassword(password)) {
        callback(null, this.addUser(user.name, user.password, false));
      } else {
        callback(new Error("Bad Authentication"));
      }
    }
  }
}

const userManager = new SimpleUserManager();

// Create a new server instance
const server = new webdav.WebDAVServer({
  // HTTP Basic authentication
  httpAuthentication: new webdav.HTTPBasicAuthentication(userManager, "Default realm"),
  rootFileSystem: new webdav.PhysicalFileSystem("/tmp/"),
});

// Start the server
server.start((s) => console.log("Ready on port", s.address().port));
Daxcor69 commented 9 months ago

I am running into an issue, that windows 11, is totally bypassing the authentication and allows full access with out credentials. Any idea why?

Daxcor69 commented 9 months ago

Adding requireAuthentification: true, has stopped the windows file explorer from having access, but now it wont grant access with the right credentials presented. I tired strictmode on and off. Any suggestions?

hironico commented 9 months ago

Hi,

I would try using a privilege manager that allows access only to users that belongs to a directory. This is the way I manage user access rights RO, RW to my directories. privilege manager can also manage disk space quota.

To acheive this I do :

  1. build user config from a json file or whatever is your preference
  2. for each user returned by user manager, configure the privilege manager to set user rights and compute quota used.
  3. when creating server then affect the privilege manager in the config

Hope this helps !

Cheers

N.