Skip to content

Commit f431ca7

Browse files
committed
Added some documentation
1 parent 116cc15 commit f431ca7

File tree

1 file changed

+199
-7
lines changed

1 file changed

+199
-7
lines changed

README.md

Lines changed: 199 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@
77
[![License](https://img.shields.io/npm/l/webdav-server.svg)](http://unlicense.org/)
88
[![npm Version](https://img.shields.io/npm/v/webdav-server.svg)](https://www.npmjs.com/package/webdav-server)
99

10+
# Index
11+
12+
* Disclaimer / Project information
13+
* Install
14+
* Usage
15+
* Import
16+
* Create server
17+
* Options
18+
* Sample
19+
* Resources
20+
* Root resource
21+
* Resource creation
22+
* Authentication / Privileges / User management
23+
* User management
24+
* Authentication
25+
* Privileges
26+
* Control the response body type
27+
1028
# Disclaimer / Project information
1129

1230
Find more details on the development process at [https://github.com/OpenMarshal/npm-WebDAV-Server/projects/1](https://github.com/OpenMarshal/npm-WebDAV-Server/projects/1).
@@ -62,6 +80,12 @@ const server = new webdav.WebDAVServer(options);
6280
### Options
6381
Key | Default value | Description
6482
-|-|-
83+
requireAuthentification | false | Define if your require to be authenticated.
84+
httpAuthentication | new HTTPBasicAuthentication('default realm') | Define the object which will provide the authentication method (HTTP : Basic, Digest, custom, etc).
85+
privilegeManager | new FakePrivilegeManager() | Allow to check the privileges of the user (grant or restrict access).
86+
rootResource | new RootResource() | The root resource to use as `/`.
87+
userManager | new SimpleUserManager() | Define the object which will provide the users.
88+
lockTimeout | 3600 | Define the lock timeout.
6589
port | 1900 | The default port to use if no port is specified when calling `server.start()`.
6690

6791
## Sample
@@ -87,15 +111,183 @@ file.content = 'The content of the virtual file.';
87111
server.rootResource.addChild(file, e => {
88112
if(e)
89113
throw e;
114+
115+
// Create a virtual folder
116+
var folder = new webdav.VirtualFolder('testFolder');
117+
server.rootResource.addChild(folder, e => {
118+
if(e)
119+
throw e;
120+
121+
var file2 = new webdav.PhysicalFile('/home/testFile2.txt');
122+
folder.addChild(file2, e => {
123+
if(e)
124+
throw e;
125+
126+
var folder2 = new webdav.PhysicalFolder('/home/testFolder2');
127+
folder.addChild(folder2, e => {
128+
if(e)
129+
throw e;
130+
131+
// Start the server
132+
server.start();
133+
134+
// [...]
135+
136+
// Stop the server
137+
server.stop(() => {
138+
console.log('Server stopped with success!');
139+
})
140+
});
141+
});
142+
});
90143
});
144+
```
145+
146+
In this example, the resource tree will be the following :
147+
Path|Class
148+
-|-
149+
`/` | RootResource
150+
`/testFile.txt` | VirtualFile
151+
`/testFolder` | VirtualFolder
152+
`/testFolder/testFile2.txt` | PhysicalFile
153+
`/testFolder/testFolder2` | PhysicalFolder
154+
155+
# Resources
156+
157+
There are two kinds of resource provided : the virtual file/folder and the physical file/folder. The virtual resource is a resource you create and keep in memory. The physical resource is a resource present in your current file system.
158+
159+
You can create a custom resource by creating an object or a class which will inherit the members of the IResource class.
160+
161+
The type of a resource can be (class ResourceType) :
162+
163+
-|Is a directory|Is not a directory
164+
-|-|-
165+
**Is a file**|Hibrid|File
166+
**Is not a file**|Directory|NoResource
167+
168+
Here are the differences between the resource types :
169+
-|Hibrid|File|Directory|NoResource
170+
-|-|-|-|-
171+
**Can have content**|yes|yes|no|no
172+
**Can have child resource**|yes|no|yes|no
173+
**Is standard (RFC)**|no|yes|yes|no
174+
175+
## Root resource
176+
177+
The root resource (`class RootResource`) is a resource which disallow almost everything (no copy, no move, etc), but provides a static root folder. It is instanciated by default if no `option.rootResource` is provided to the constructor of the server. This resource is a virtual resource. That's why, if you create a child through the WebDAV interface (HTTP), it will be a virtual resource.
178+
179+
Method|Must be allowed?||Method|Must be allowed?
180+
-|-|-|-|-
181+
**create** | no || **delete** | no
182+
**moveTo** | no || **rename** | no
183+
**append** | may || **write** | may
184+
**read** | may || **mimeType** | may
185+
**size** | may || **getLocks** | yes
186+
**setLock** | yes || **removeLock** | yes
187+
**canLock** | yes || **getAvailableLocks** | yes
188+
**canRemoveLock** | yes || **getLock** | yes
189+
**addChild** | yes || **removeChild** | yes
190+
**getChildren** | yes || **setProperty** | yes
191+
**getProperty** | yes || **removeProperty** | yes
192+
**getProperties** | yes || **creationDate** | yes
193+
**lastModifiedDate** | may || **webName** | yes
194+
**type** | yes
195+
196+
## Resource creation
197+
198+
When a user create a resource through the WebDAV interface (HTTP), it will inherit the parent file system manager. For instance, if you create a file or a folder as a child of the path `/myFolder` and `myFolder` contains a virtual file system manager, then the file or folder created will be a virtual file or a virtual folder. The same reflection can be made with the physical resources and custom resources. The file system manager is in charge of the creation of the resource object (not to call `.create(...)`, just the object). This way, you can have a behavior for a resource (the resource class itself) and a different behavior for its children (the file system manager).
199+
200+
If you create a resource through the module (for instance in JavaScript), you can add as child of a resource the kind of resource you want. You are limited only by the type of the parent resource and its implementation.
201+
202+
# Authentication / Privileges / User management
203+
204+
## User management
205+
206+
The user management is get through an instance of the class `IUserManager` which provide users of the class `IUser`.
207+
208+
A `IUserManager` class must contains the following public fields :
209+
```typescript
210+
getUserByName(name : string, callback : (error : Error, user : IUser) => void)
211+
getDefaultUser(callback : (user : IUser) => void)
212+
getUsers(callback : (error : Error, users : IUser[]) => void)
213+
```
214+
215+
A `IUser` class must contains the following public fields :
216+
```typescript
217+
isAdministrator : boolean
218+
isDefaultUser : boolean
219+
password : string
220+
username : string
221+
```
222+
223+
The `IUserManager` class can get a user by name ; it can get the list of all users ; and it can get the default user.
224+
225+
The default user is the user which is given to an unauthentication user. This way, an unauthenticated user will have the privileges of the default user. If the server's option `requireAuthentification` equals `true`, the default user will not be used.
226+
227+
Thanks to the server's option `userManager`, the user manager can be set with a custom instance. This way, you can create a user manager which, for instance, retrieve its users from a database.
228+
229+
## Authentication
230+
231+
The authentication is made through the HTTP authentication system. The standard authentication systems use a HTTP header to get the user credentials.
232+
233+
Thanks to the server's option `httpAuthentication`, it is possible to define the authentication system you want to use, even a custom one.
234+
235+
It musts inherit from the interface `HTTPAuthentication` :
236+
```typescript
237+
realm : string
91238

92-
// Start the server
93-
server.start();
239+
askForAuthentication() : any
240+
getUser(arg : MethodCallArgs, userManager : IUserManager, callback : (error : Error, user : IUser) => void)
241+
```
242+
243+
The `askForAuthentication()` method is used by the server to know what headers the method needs to add to its response.
244+
245+
The `getUser()` method is used by the server to get the user of the current request.
246+
247+
There are two authentication system implemented in the modules : `HTTPBasicAuthentication` and `HTTPDigestAuthentication`.
248+
249+
The class `HTTPBasicAuthentication` implements the `Basic` authentication system and is the most commonly used on internet. The class `HTTPDigestAuthentication` implements the `Digest` authentication system and provides a more secure way to authenticate.
250+
251+
## Privileges
94252

95-
// [...]
253+
The privileges of a user upon a resource is defined by the instance of the interface `IPrivilegeManager` provided in the server's option `privilegeManager`. This object provides a list of methods to tell the server that a resource is accessible by a user or if it is not.
96254

97-
// Stop the server
98-
server.stop(() => {
99-
console.log('Server stopped with success!');
100-
})
255+
Here is the list of the methods in the interface `IPrivilegeManager` :
256+
```typescript
257+
canCreate : PrivilegeManagerMethod
258+
canDelete : PrivilegeManagerMethod
259+
canMove : PrivilegeManagerMethod
260+
canRename : PrivilegeManagerMethod
261+
canAppend : PrivilegeManagerMethod
262+
canWrite : PrivilegeManagerMethod
263+
canRead : PrivilegeManagerMethod
264+
canGetMimeType : PrivilegeManagerMethod
265+
canGetSize : PrivilegeManagerMethod
266+
canListLocks : PrivilegeManagerMethod
267+
canSetLock : PrivilegeManagerMethod
268+
canRemoveLock : PrivilegeManagerMethod
269+
canGetAvailableLocks : PrivilegeManagerMethod
270+
canGetLock : PrivilegeManagerMethod
271+
canAddChild : PrivilegeManagerMethod
272+
canRemoveChild : PrivilegeManagerMethod
273+
canGetChildren : PrivilegeManagerMethod
274+
canSetProperty : PrivilegeManagerMethod
275+
canGetProperty : PrivilegeManagerMethod
276+
canGetProperties : PrivilegeManagerMethod
277+
canRemoveProperty : PrivilegeManagerMethod
278+
canGetCreationDate : PrivilegeManagerMethod
279+
canGetLastModifiedDate : PrivilegeManagerMethod
280+
canGetWebName : PrivilegeManagerMethod
281+
canGetType : PrivilegeManagerMethod
282+
```
283+
With :
284+
```typescript
285+
type PrivilegeManagerCallback = (error : Error, hasAccess : boolean) => void;
286+
type PrivilegeManagerMethod = (arg : MethodCallArgs, resource : IResource, callback : PrivilegeManagerCallback) => void
101287
```
288+
289+
The request relative information (the user, the request, etc) are in the `arg` parameter.
290+
291+
# Control the response body type
292+
293+
By default, when there is a body in the WebDAV response, it will be in XML. If there is a `Accept` header in the request with the `json` type as a priority, the result will be in JSON.

0 commit comments

Comments
 (0)