|
1 |
| -# WebDAV and Web File Server With User Database and ACL |
| 1 | +# Authentication and Authorization Example |
2 | 2 |
|
3 |
| -This document includes the example 2 source code for the [How to Create a Cloud Storage Server Tutorial](https://makoserver.net/articles/How-to-Create-a-Cloud-Storage-Server#udb). This example is compatible with any [BAS](https://realtimelogic.com/products/barracuda-application-server/) derivative product with a file system, including the Mako Server and Xedge32. |
| 3 | +This example shows how to enable both authentication and authorization using an Access Control List (ACL). The **Barracuda App Server (BAS)** provides several API functions for managing authentication and, optionally, authorization. See the [authenticator documentation](https://realtimelogic.com/ba/doc/en/lua/lua.html#auth_overview) for details. |
4 | 4 |
|
5 |
| -Run the example, using the Mako Server, as follows: |
| 5 | +## Authentication vs. Authorization |
6 | 6 |
|
7 |
| -``` |
| 7 | +- **Authentication** verifies the identity of a user. |
| 8 | +- **Authorization** determines what resources a user can access after authentication. |
| 9 | + |
| 10 | +While applications may implement their own ACL directly, this example utilizes both authentication and authorization APIs provided by BAS. We use the easy-to-use **[JSON Authenticator](https://realtimelogic.com/ba/doc/en/lua/lua.html#ba_create_jsonuser)**, which includes an optional authorizer that assigns permissions based on the following: |
| 11 | +- The authenticated user |
| 12 | +- A predefined set of URIs |
| 13 | +- The HTTP method used |
| 14 | + |
| 15 | +## File Server Integration |
| 16 | + |
| 17 | +This example applies the combined authenticator and authorizer to a **[File Server object](https://realtimelogic.com/ba/doc/en/lua/lua.html#ba_create_wfs)**, which integrates: |
| 18 | +- **WebDAV**, enabling file management through WebDAV clients. |
| 19 | +- **Web File Manager**, allowing users to browse and manage files through a web interface. |
| 20 | + |
| 21 | +For details on using WebDAV, refer to the tutorial [How to Create a Cloud Storage Server](https://makoserver.net/articles/How-to-Create-a-Cloud-Storage-Server). |
| 22 | + |
| 23 | +**Note:** The **JSON Authenticator** is not restricted to use with a **File Server** object. It can also be used in standard web applications, offering an easy to implement authentication mechanism. Additionally, its built-in authorization capabilities provide a convenient way to manage access control within your application. |
| 24 | + |
| 25 | + |
| 26 | +## User Database and ACL Configuration |
| 27 | + |
| 28 | +The user database and ACL setup are also covered in the tutorial **How to Create a Cloud Storage**, section **[Creating a User Database](https://makoserver.net/articles/How-to-Create-a-Cloud-Storage-Server#udb)**. |
| 29 | + |
| 30 | +To keep the example concise, user credentials and ACL rules are **hardcoded** within the [.preload](www/.preload) script. |
| 31 | + |
| 32 | +## Authentication and Authorization |
| 33 | + |
| 34 | +### HTTP Digest Authentication |
| 35 | + |
| 36 | +This example uses **[HTTP Digest Authentication](https://realtimelogic.com/ba/doc/en/authentication.html#authtypes)**, which prompts users for credentials via a browser pop-up. Note that: |
| 37 | +- Browsers **cache** the HTTP credentials until they are completely closed. |
| 38 | +- Some browsers may keep processes in memory even after closing all windows, retaining authentication. |
| 39 | + |
| 40 | +### Hardcoded User Credentials |
| 41 | + |
| 42 | +The following credentials are preconfigured in this example: |
| 43 | + |
| 44 | +| Username | Password | |
| 45 | +|----------|----------| |
| 46 | +| guest | guest | |
| 47 | +| kids | kids | |
| 48 | +| mom | mom | |
| 49 | +| dad | dad | |
| 50 | + |
| 51 | + |
| 52 | +### Server-Side Authentication via request:login() |
| 53 | + |
| 54 | +This example also shows how to use the [request:login()](https://realtimelogic.com/ba/doc/en/lua/lua.html#request_login) method, which allows server-side code to authenticate a user without relying on **HTTP authentication** or the web-based authentication mechanisms provided by **BAS**. This method is designed for integrating authentication systems not natively supported by the server, such as **[Single Sign-On](../fs-sso/README.md) (SSO)** or **WebAuthn**. |
| 55 | + |
| 56 | +#### Using `request:login()` with an Authorizer |
| 57 | + |
| 58 | +In this example, `request:login()` is used alongside an **authorizer**. When an authorizer is enabled: |
| 59 | +- `request:login()` **must** be called with valid user credentials. |
| 60 | +- Attempting to log in without arguments or with a non-existent user will be denied. |
| 61 | + |
| 62 | +The `index.lsp` page demonstrates this by allowing authentication with the registered users: **mom, dad, kids, and guest**. It also provides options to: |
| 63 | +- Attempt authentication with `nil` (Lua's equivalent of "no value"). |
| 64 | +- Logging in as an unregistered user. |
| 65 | + |
| 66 | +When using an unregistered user, authentication succeeds, but the authorizer **blocks access** and returns a "No Access" message. |
| 67 | + |
| 68 | +#### Testing Without an Authorizer |
| 69 | + |
| 70 | +The included `mako.conf` file contains a setting that is read by the `.preload` script. This setting allows you to **disable the authorizer** for testing purposes. Without an authorizer, `request:login()` grants access to the protected **File Server** resource regardless of the username provided. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## How to Use the Example |
| 76 | + |
| 77 | +This example is designed to run on the **[Mako Server](https://makoserver.net/download/overview/)**. To start the example, navigate to the project directory and launch the server with the following command: |
| 78 | + |
| 79 | +```sh |
8 | 80 | cd JSON-File-Server
|
9 | 81 | mako -l::www
|
10 | 82 | ```
|
11 | 83 |
|
12 | 84 | For detailed instructions on starting the Mako Server, please refer to our [Mako Server command line video tutorial](https://youtu.be/vwQ52ZC5RRg) and review the [server's command line options](https://realtimelogic.com/ba/doc/?url=Mako.html#loadapp) in our documentation.
|
13 | 85 |
|
14 |
| -Once you've successfully started the Mako Server, open a web browser and go to http://localhost:portno/fs/, where 'portno' represents the HTTP port number used by the Mako Server (this number is displayed in the console). |
| 86 | +Once you have successfully started the Mako Server, open a web browser and navigate to http://localhost:portno, where 'portno' represents the HTTP port number used by the Mako Server (this number is displayed in the console). |
15 | 87 |
|
16 |
| -To access the system, use the following login credentials: |
| 88 | +On this page, you can: |
| 89 | +- **Access the File Server** at `fs/` and log in using **HTTP Digest Authentication**. |
| 90 | +- **Test `request:login()`** to authenticate users programmatically. |
17 | 91 |
|
18 |
| -| Username | Password | |
19 |
| -|----------|----------| |
20 |
| -| guest | guest | |
21 |
| -| kids | kids | |
22 |
| -| mom | mom | |
23 |
| -| dad | dad | |
| 92 | +Try the different login methods and observe how authentication works. When using **HTTP Digest Authentication**, the browser's login dialog will prompt for credentials - enter one of the **usernames and passwords** [listed above](#hardcoded-user-credentials). |
24 | 93 |
|
25 |
| -For guidance on how to map the WebDAV drive, watch this instructional video: https://youtu.be/i5ubScGwUOc |
| 94 | +After testing the login methods, stop the server and open [mako.conf](mako.conf) in an editor, remove the comment to disable the authorizer, and re-start the server. |
26 | 95 |
|
27 |
| -Please be aware that this application does not contain any web pages or resources except for the WebDAV/file-server installed at /fs/. Additional information can be found in the www/.preload Lua script. |
| 96 | +## Files in the `www` Directory |
28 | 97 |
|
| 98 | +- **`.preload`** - The [.preload startup script](https://realtimelogic.com/ba/doc/en/Mako.html#preload) configures the **authenticator** and **authorizer** using hardcoded values. It initializes a **File Server instance** and integrates it into the **[Virtual File System](https://realtimelogic.com/ba/doc/en/VirtualFileSystem.html) (VFS)**. Additionally, the script sets up a directory structure on your hard drive for use by the File Server. |
29 | 99 |
|
| 100 | +- **`index.lsp`** - Provides navigation options: |
| 101 | + - Access the **File Server** at `fs/` and log in using **Digest Authentication**. |
| 102 | + - Authenticate using `request:login()`. |
| 103 | + - If already authenticated, accessing this page will redirect you to `logout.lsp`. |
30 | 104 |
|
| 105 | +- **`logout.lsp`** - Handles user logout: |
| 106 | + - Logs the user out and redirects the users back to `index.lsp`, except for those using **Digest Authentication** (due to browser-based credential caching). |
| 107 | + - Displays additional logout-related details when not redirected. |
0 commit comments