Skip to content

Commit bf74436

Browse files
committedJun 26, 2024
Add docs for HTTP User Detail Provider
1 parent 6b09d1e commit bf74436

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
 

‎docs/server/http-usr.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
title: User config over HTTP
3+
---
4+
5+
In the "Per User Config" section we showed how to create configuration unique for each user.
6+
This
7+
configuration was stored in a JSON file. This same JSON file can be consumed by unFTP over HTTP.
8+
9+
Start be defining the user configuration:
10+
11+
```shell
12+
touch user-options.json
13+
```
14+
15+
```json
16+
[
17+
{
18+
"username": "alice",
19+
"vfs_perms": [
20+
"-mkdir",
21+
"-rmdir",
22+
"-del",
23+
"-ren",
24+
"-md5"
25+
],
26+
"root": "alice",
27+
"account_enabled": true
28+
},
29+
{
30+
"username": "bob",
31+
"vfs_perms": [
32+
"none",
33+
"+put",
34+
"+list",
35+
"+md5"
36+
],
37+
"root": "bob"
38+
}
39+
]
40+
```
41+
42+
Then serve this over HTTP with any method you prefer. Here is an example doing with a Go script:
43+
44+
```shell
45+
touch main.go
46+
```
47+
48+
```go
49+
package main
50+
51+
import (
52+
"fmt"
53+
"net/http"
54+
"os"
55+
)
56+
57+
func main() {
58+
// Specify the path to the JSON file containing user details
59+
jsonFilePath := "./user-options.json"
60+
61+
// Create a simple HTTP handler function
62+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
63+
64+
fmt.Println("Requested URL: ", r.URL)
65+
66+
// Check if the request method is GET
67+
if r.Method == http.MethodGet {
68+
// Read the contents of the JSON file
69+
jsonData, err := os.ReadFile(jsonFilePath)
70+
if err != nil {
71+
http.Error(w, fmt.Sprintf("Error reading JSON file: %s", err), http.StatusInternalServerError)
72+
return
73+
}
74+
75+
// Set the Content-Type header to indicate JSON content
76+
w.Header().Set("Content-Type", "application/json")
77+
78+
// Write the JSON data to the response writer
79+
w.Write(jsonData)
80+
} else {
81+
// If the request method is not GET, respond with a 405 Method Not Allowed status
82+
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
83+
}
84+
})
85+
86+
// Start the web server on port 8080
87+
fmt.Println("Server is running on http://localhost:8080")
88+
err := http.ListenAndServe(":8080", nil)
89+
if err != nil {
90+
fmt.Printf("Error starting server: %s\n", err)
91+
}
92+
}
93+
```
94+
95+
You'll need [Go](https://go.dev/) installed and then you can do:
96+
97+
```go
98+
go run main.go
99+
```
100+
101+
and then notice the HTTP address in the output:
102+
103+
```
104+
Server is running on http://localhost:8080
105+
```
106+
107+
You then run unFTP with the `--usr-http-url` command line argument:
108+
109+
```shell
110+
unftp \
111+
--root-dir=. \
112+
--auth-type=json \
113+
--auth-json-path=credentials.json \
114+
--usr-http-url='http://localhost:8080/users/'
115+
```
116+
117+
Notice that when running unFTP, we still do `--auth-type=json` and `--auth-json-path=credentials.json`. The
118+
authenticator and user detail provider mechanisms are disjoint. So passwords are specified in `credentials.json` and
119+
the user details or options like where the user's root path is gets served over HTTP.
120+
121+
Create the credentials file:
122+
123+
```shell
124+
touch credentials.json
125+
```
126+
127+
and add this as contents:
128+
129+
```json
130+
[
131+
{
132+
"username": "alice",
133+
"password": "12345678"
134+
},
135+
{
136+
"username": "bob",
137+
"password": "secret"
138+
}
139+
]
140+
```
141+
142+
Let us also make home directories for Bob and Alice:
143+
144+
```shell
145+
mkdir bob
146+
mkdir alice
147+
touch bob/hello-bob.txt
148+
touch alice/hello-alice.txt
149+
```
150+
151+
Finally, let us run an FTP client
152+
153+
```shell
154+
lftp localhost -p 2121 -u bob
155+
```
156+
157+
Provide the password 'secret' for Bob do an FPT `ls`.
158+
159+
You should see the file `hello-bob.txt` being listed and the output of the little Go webserver should be:
160+
161+
```
162+
Requested URL: /users/bob
163+
```

‎doctave.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ navigation:
1717
- path: docs/server/gcs.md
1818
- path: docs/server/jsonconfig.md
1919
- path: docs/server/rest.md
20+
- path: docs/server/http-usr.md
2021
- path: docs/server/pubsub.md
2122
- path: docs/server/anti-brute.md
2223
- path: docs/server/proxy-protocol.md

0 commit comments

Comments
 (0)