I am running this simple query 'ALTER USER hr IDENTIFIED BY secret'
using oracledb nodejs package. Not sure why I am getting illegal variable name error. Can I do something like this "ALTER USER :user IDENTIFIED BY :password";
if yes then what will be correct syntax for me
function get(req, res, next) {
oracledb.getConnection(
config.database,
function(err, connection){
if (err) {
return next(err);
}
connection.execute(
'ALTER USER :user IDENTIFIED BY :password'+
{
user: req.body.user
},
{
password: req.body.password
},
{
outFormat: oracledb.OBJECT
}
});
}
Thanks for the help
As Matthew pointed out, you'll have to use string concatenation and protect against SQL injection.
To some degree, I question the need to do this. I know you're already aware of this post: https://jsao.io/2015/06/authentication-with-node-js-jwts-and-oracle-database/
Because that solution uses a table to store user credentials, rather than depending on database users, the values can be safely bound in without worrying about SQL injection. Why not use that approach instead?
Using database users with dynamic SQL requires that single quotes (') not be allowed in passwords. Frankly, I hate those kinds of restrictions. Password rules should exist to increase security (what must be included), not ensure that code can be executed safely (what's not allowed). That would not be an issue with a custom table.
However, just so you have one, here's an example solution that's based on promises that shows how to use dbms_assert to sanitize the values coming in:
Finally, combining database logic with controller logic may lead to code that's difficult to maintain. Have a look at this recording for some tips on organizing things a little better: https://www.youtube.com/watch?v=hQgw2WmyuFM