Skip to content

Commit 4892ef0

Browse files
committed
adding displayname feature
1 parent fef5357 commit 4892ef0

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Besides settings.json, it is now possible to store the user-database in a filesy
77
```JSON
88
"users": {
99
"admin": {"password": "admin","is_admin": true},
10-
"y": {"is_admin": true, "hash": "b2112aa7399 ... b071ea5976"}
10+
"y": {"is_admin": true, "hash": "b2112aa7399 ... b071ea5976"},
11+
"z": {"is_admin": true, "hash": "b5152ab7359 ... a041fa5646", "displayname": "Jane Doe"}
1112
}
1213
```
1314

@@ -19,7 +20,8 @@ optionally specify hash type and digest, folders and extension, defaults are:
1920
"hash_dig": "hex",
2021
"hash_dir": "/var/etherpad/users",
2122
"hash_ext": "/.hash",
22-
"hash_adm": false
23+
"hash_adm": false,
24+
"displayname_ext": "/.displayname"
2325
},
2426
```
2527
This means user Alice would have to have her hash in sha512 hex OR in bcrypt format in the following file:
@@ -28,6 +30,8 @@ This means user Alice would have to have her hash in sha512 hex OR in bcrypt for
2830
```
2931
The hash_adm parameter defines the role of file-authenticated users, by default they are not admins.
3032

33+
The displayname_ext parameter defines from which file the displayname of a user can be read. If the file does not exist for a user, the displayname remains unchanged.
34+
3135
## Generate the hashes
3236
#### Bcrypt:
3337
```Shell

ep.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
{
44
"name": "ep_hash_auth",
55
"hooks": {
6-
"authenticate": "ep_hash_auth/ep_hash_auth"
6+
"authenticate": "ep_hash_auth/ep_hash_auth",
7+
"handleMessage": "ep_hash_auth/ep_hash_auth"
78
}
89
}
910
]

ep_hash_auth.js

+38-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ var hash_dir = '/var/etherpad/users';
3737
var hash_ext = '/.hash';
3838
// by default peple logged in that authenticated over a hash file, are admins?
3939
var hash_adm = false;
40+
// default filename containing the displayname of a user
41+
var displayname_ext = '/.displayname';
4042

4143

4244
if (settings.ep_hash_auth) {
@@ -45,6 +47,7 @@ if (settings.ep_hash_auth) {
4547
if (settings.ep_hash_auth.hash_dir) hash_dir = settings.ep_hash_auth.hash_dir;
4648
if (settings.ep_hash_auth.hash_ext) hash_ext = settings.ep_hash_auth.hash_ext;
4749
if (settings.ep_hash_auth.hash_adm) hash_adm = settings.ep_hash_auth.hash_adm;
50+
if (settings.ep_hash_auth.displayname_ext) displayname_ext = settings.ep_hash_auth.displayname_ext;
4851
}
4952

5053
// Let's make a function to compare our hashes now that we have multiple comparisons required.
@@ -104,6 +107,13 @@ exports.authenticate = function(hook_name, context, cb) {
104107
console.log("Log: Authenticated ("+hashType+") " + username);
105108
settings.users[username].username = username;
106109
context.req.session.user = settings.users[username];
110+
// use displayname if available
111+
if(settings.users[username].displayname !== undefined) {
112+
context.req.session.user['displayname'] = settings.users[username].displayname;
113+
}
114+
else {
115+
console.log("Log: displayname not found for user " + username);
116+
}
107117
return cb([true]);
108118
} else {return cb([false]);}
109119
});
@@ -119,9 +129,19 @@ exports.authenticate = function(hook_name, context, cb) {
119129
compareHashes(password, contents, function(hashType) {
120130
if (hashType) {
121131
console.log("Log: Authenticated ("+hashType+"-file) " + username);
122-
settings.users[username] = {'username': username, 'is_admin': hash_adm};
123-
context.req.session.user = settings.users[username];
124-
return cb([true]);
132+
// read displayname if available
133+
var displaynamepath = hash_dir + "/" + username + displayname_ext;
134+
fs.readFile(displaynamepath, 'utf8', function(err, contents) {
135+
var displayname;
136+
if (err) {
137+
console.log("Log: Could not load displayname for " + username);
138+
} else {
139+
displayname = contents;
140+
}
141+
settings.users[username] = {'username': username, 'is_admin': hash_adm, 'displayname': displayname};
142+
context.req.session.user = settings.users[username];
143+
return cb([true]);
144+
});
125145
} else {return cb([false]);}
126146
});
127147
}
@@ -130,3 +150,18 @@ exports.authenticate = function(hook_name, context, cb) {
130150
} else return cb([false]);
131151

132152
};
153+
154+
exports.handleMessage = function (hook_name, context, cb) {
155+
// skip if we don't have any information to set
156+
var session = context.client.client.request.session;
157+
if (!session || !session.user || !session.user.displayname) return cb();
158+
159+
authorManager.getAuthor4Token(context.message.token).then(function (author) {
160+
authorManager.setAuthorName(author, context.client.client.request.session.user.displayname);
161+
cb();
162+
}).catch(function (error) {
163+
console.error('handleMessage: could not get authorid for token %s', context.message.token, error);
164+
cb();
165+
});
166+
};
167+

0 commit comments

Comments
 (0)