-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support ldap auth #4751
base: master
Are you sure you want to change the base?
support ldap auth #4751
Changes from 1 commit
31bc6e8
a2140d4
dec7ef5
90130c9
da72713
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ const { log } = require("../src/util"); | |||||||||||||
const { loginRateLimiter, apiRateLimiter } = require("./rate-limiter"); | ||||||||||||||
const { Settings } = require("./settings"); | ||||||||||||||
const dayjs = require("dayjs"); | ||||||||||||||
const ldap = require("ldapjs"); | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Login to web app | ||||||||||||||
|
@@ -14,26 +15,71 @@ const dayjs = require("dayjs"); | |||||||||||||
* @returns {Promise<(Bean|null)>} User or null if login failed | ||||||||||||||
*/ | ||||||||||||||
exports.login = async function (username, password) { | ||||||||||||||
|
||||||||||||||
if (typeof username !== "string" || typeof password !== "string") { | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
let user = await R.findOne("user", " username = ? AND active = 1 ", [ | ||||||||||||||
username, | ||||||||||||||
]); | ||||||||||||||
|
||||||||||||||
if (user && passwordHash.verify(password, user.password)) { | ||||||||||||||
// Upgrade the hash to bcrypt | ||||||||||||||
if (passwordHash.needRehash(user.password)) { | ||||||||||||||
await R.exec("UPDATE `user` SET password = ? WHERE id = ? ", [ | ||||||||||||||
passwordHash.generate(password), | ||||||||||||||
user.id, | ||||||||||||||
]); | ||||||||||||||
if (process.env.AUTH_METHOD === "LDAP") { | ||||||||||||||
let ldapAuthSuccess = false; | ||||||||||||||
await new Promise((resolve, reject) => { | ||||||||||||||
const client = ldap.createClient({ | ||||||||||||||
url: [ process.env.LDAP_ADDRESS ] | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the parameters to this function (
|
||||||||||||||
}); | ||||||||||||||
client.on("connectError", (err) => { | ||||||||||||||
log.error("auth", "connecting ldap server failed"); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. include the error message if it contains context aiding in debugging |
||||||||||||||
resolve(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please refactor this to reject/resolve with the value of |
||||||||||||||
}); | ||||||||||||||
client.bind(process.env.LDAP_UID + "=" + username + "," + process.env.LDAP_BASE_DN, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please include debug logging what query is being executed |
||||||||||||||
password, (err) => { | ||||||||||||||
if (err) { | ||||||||||||||
log.warn("auth", "ldap authentication failed for user: " + username); | ||||||||||||||
} else { | ||||||||||||||
log.info("auth", "ldap authentication succeeded for user: " + username); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets use a template for better readability
Suggested change
|
||||||||||||||
ldapAuthSuccess = true; | ||||||||||||||
} | ||||||||||||||
resolve(); | ||||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
if (!ldapAuthSuccess) { | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
let user = await R.findOne("user", " username = ? AND active = 1 ", [ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please extract the below parts into functions. |
||||||||||||||
username, | ||||||||||||||
]); | ||||||||||||||
if (user) { | ||||||||||||||
if (passwordHash.needRehash(user.password)) { | ||||||||||||||
await R.exec("UPDATE `user` SET password = ? WHERE id = ? ", [ | ||||||||||||||
passwordHash.generate(password), | ||||||||||||||
user.id, | ||||||||||||||
]); | ||||||||||||||
} | ||||||||||||||
return user; | ||||||||||||||
} else { | ||||||||||||||
log.info("auth", "user: " + username + "not exists in db, create it"); | ||||||||||||||
let user = R.dispense("user"); | ||||||||||||||
user.username = username; | ||||||||||||||
user.password = passwordHash.generate(password); | ||||||||||||||
await R.store(user); | ||||||||||||||
return await R.findOne("user", " username = ? AND active = 1 ", [ username ]); | ||||||||||||||
} | ||||||||||||||
} else { | ||||||||||||||
let user = await R.findOne("user", " username = ? AND active = 1 ", [ | ||||||||||||||
username, | ||||||||||||||
]); | ||||||||||||||
|
||||||||||||||
if (user && passwordHash.verify(password, user.password)) { | ||||||||||||||
// Upgrade the hash to bcrypt | ||||||||||||||
if (passwordHash.needRehash(user.password)) { | ||||||||||||||
await R.exec("UPDATE `user` SET password = ? WHERE id = ? ", [ | ||||||||||||||
passwordHash.generate(password), | ||||||||||||||
user.id, | ||||||||||||||
]); | ||||||||||||||
} | ||||||||||||||
return user; | ||||||||||||||
} | ||||||||||||||
return user; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return null; | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please extract the ldap parts of this implementation into a function.
This method is too long already and adding more will not aid in readability ^^