Set password of user created with Scripted API in Service now - javascript

I am new to the Service now and trying to build a Client Web-app with Service Now.
My requirement is to register a user in Service Now using my web app.
I have tried to do so with Table API and Scripted Rest API.
Though I am able to create the user (makes an entry in sys_user table) but the created user is not able to login on Service Now portal.
If I login as admin and change the password of created user the user is able to login.
Please tell me what am I doing wrong?
Below is my Scripted API -
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body.data;
var gr = new GlideRecord('sys_user');
gr.initialize();
gr.user_name = requestBody.user_name;
gr.first_name = requestBody.first_name;
gr.last_name = requestBody.last_name;
gr.title = requestBody.title;
gr.department = requestBody.department;
gr.user_password = requestBody.user_password;
gr.active = requestBody.active;
gr.email = requestBody.email;
gr.mobile_phone = requestBody.mobile_phone;
gr.insert();
//Create a body object. Add property value pairs to the body.
var body = {};
body.user_name = gr.user_name;
body.sys_id = gr.sys_id;
body.password = gr.user_password;
response.setBody(body);
})(request, response);

I found a hook for this question.
When you create the user instead of setting the password before gr.insert(), try updating the record for setting the password as per the following code.
gr.user_password.setDisplayValue(requestBody.user_password);
gr.update();
Looks like password in sys_user table is a one way encrypted field. So you need to set the display value.

Related

I want to keep the user information during user registration even after reloading

Sorry, this is not a code question.
I'm currently working on a web application using React.
I've been using Redux to manage user registration information (ex: email address, etc.) for user registration over several pages, but I noticed that the registered information disappears after reloading.
I thought about saving the information to localStorage, but gave up due to the security risk.
How would you guys keep your users' registration information?
If the information is not much, you could use encrypted cookie data, and read back the data on page load.
Create a .env file in your project root folder
Save your pass access in this file like so REACT_APP_PASS=8604460484466
use a function like
const encryptWithAES = (text, pass) => {
const passphrase = pass;
return CryptoJS.AES.encrypt(text, passphrase).toString();
}; // remember to import CryptoJS
When the page loads you can read the data with:
export const decryptWithAES = (ciphertext, pass) => {
const passphrase = pass;
const bytes = CryptoJS.AES.decrypt(ciphertext, passphrase);
const originalText = bytes.toString(CryptoJS.enc.Utf8);
return originalText;
};
When you want to store the cookie, then you can do it like so:
const tobeStored = encryptWithAES('text to encrypt', process.env.REACT_APP_PASS);
// then store it in your cookie.
When you want to retrieve it:
const cookieData = someGetCookieFunction('UserInfo');
const decrypted = decryptWithAES(cookieData,process.env.REACT_APP_PASS);
I am assuming that you know how to read cookie from your script.
See more information here and Here As well on how to set and call environment variables.

Why is firebase creating a random identifier between parent and child?

I'm playing around with a firebase web app and having some difficulty diagnosing where something is coming from.
I am trying to simply push some data to my project under the heading of the uid created when authentication takes place. The authentication works fine and it is returning the uid correctly however, when values are passed it seems to be adding a second layer before the actual values.
function registerAccount() {
var firebase = app_firebase;
var firebaseRef = app_firebase.database(); //database reference
var user = firebase.auth().currentUser;
uid = user.uid;
var ref = firebaseRef.ref('User').child(uid); //referencing node
var userName = document.getElementById("txtUsernameInput").value;
if (user) {
// User is signed in.
var data = { //data being added
Username: userName,
}
window.location = 'myHome.aspx';
} else {
// No user is signed in.
console.log("Cannot get UID");
}
ref.push(data);
}
I am expecting the data entry to show with the child of user to be the uid taken from the authentication (this is working) then have the passed values immediately in the uid without the seemingly auto generated child between the uid and the values.
Image shows the unwanted field being generated
[See here][1]
André Kool's suggestion in a comment:
Change ref.push(data); to ref.set(data);
worked.

Querying firebase database to retrieve user name

I am working on firebase for the first time. I am confused in querying the database. I have two objects on the database, one is the Auth and second one is the Chats. In Auths I have number of UID(user id) nodes, each of these nodes have their respective username. What I am trying to do is, I want to query that Auth object and get all the usernames which is equal to the one I take from a user through input box. In my sql it will be simple as SELECT * FROM auth WHERE username = userInputValue. I need same query like this, Below is what I have done so far.
var _firbaseDB = firebase.database(),
_firebaseAuthRef = _firbaseDB.ref("Auth/");
$("body").on("submit",".search-from",function(e){
e.preventDefault();
var ev = e.target,
_allUsers = $(ev).serializeArray()
_user = _allUsers[0].value;
_firebaseAuthRef.equalTo(_user).on("child_added",function(res){
console.log(res.val());
})
})
You were almost there. What's missing is that you need to specify which attribute you're querying on:
_firebaseAuthRef.orderByChild("n").equalTo(_user).on("child_‌​added",function(res)‌​{
console.log(res.val());
})
More info on the Firebase Database Documentation.

How to get userIdentity from inside javascript adapter?

I am on MobileFirst 7.1, and I am trying to do something similar to this: Get user id from inside a protected adapter but this time is in javascript.
I have followed the tutorial and the protected procedure triggers login, I have made sure that application-descriptor.xml contains <userIdentityRealms>MyRealm</userIdentityRealms> however user identity is null(again).
How can I get the user identity from inside the following procedure?
function myProcedure() {
// I want to get the userid this Java into Javascript,
// SecurityAPI security = serverAPI.getSecurityAPI();
// OAuthUserIdentity identity = security.getSecurityContext().getUserIdentity();
// String userid = identity.getId();
var userid = ???
var facade = new com.ibm.jp.facade.SomeFacade();
var list = facade.SomeMethod(userid);
return JSON.parse(list);
}
In the beginning I was trying to get the user identity from inside the Java facade but it is null. I suspect it is not in the same context? That is why I am trying to get it from the js adapter and pass it as a parameter of someMethod(). If there is a better way to get it I would like to know.
I found it!
var user = WL.Server.getActiveUser();
var userid = user.userId;
...

save passwords for my website using JavaScript

Rigth now i'm creating my own website and there need to be a log in function. I've made a sign up page that retrieves data from the input fields and now comes the problem. I don't know how to save the passwords and username etc. Is it possible to save this into a text file and every time i open the website it retrieves this data? So, yes how do i do this and if this can't is there another way?
Here you see the code it retrieves. These variables need to be safed in a text file and retrieved next time website will be opend
var voornaam = document.getElementById("voorn").value;
var achternaam = document.getElementById("achtern").value;
var mail = document.getElementById("mail").value;
var password = document.getElementById("pass").value;
var gender = document.getElementById("iGender").value;
Solution:
Use a Database to save this information. Here a some Databases Mongo DB and Firebase
https://www.firebase.com/
Here's a course for Firebase http://www.codecademy.com/en/tracks/firebase
In addition to hashing the password, you probably want to be storing this information in a database. You will then have to add some logic to your application so that when a user logs in to your website, the values they enter into the log in form are checked against the values stored in the database when they signed up. In very general terms, that is probably the most standard architecture to get a log in functionality up and running.
You will have to write a service on your server that takes the form values, checks them against a database and then updates the HTML accordingly.
You could create a cookie and read it when the page loads: http://www.w3schools.com/js/js_cookies.asp
Create: document.cookie = "voornaam="+voornaam
Read: $(function(){ voornaam = document.cookie.replace("voornaam=",""); })
EDIT:
To save objects, you can use the JSON-library like so:
document.cookie = JSON.stringify({foo:"bar"}); cookieObject = JSON.parse(document.cookie);
But have in mind that the user can read and manipulate this cookie...

Categories

Resources