missing authentification token for REST request elastic search - javascript

It's the first time I'm posting a question here: I'm creating a website in AngularJS for the front end and NodeJS for the back end. This website is supposed to give me information about my elastic search clusters, get some information from Elasticsearch index. I have tried to use Elasticsearch Javascript API to do my request but it doesn't work.
I'm using ElasticSearch 5.4
Here's an example of request :
var client = new elasticsearch.Client ({
host: 'https://noev02pe.fr:9200',
auth: 'user:password',
log: 'trace',
headers: {
'Authorization': 'Basic user:password',
}
});
export function connect() {
client.search({
index: 'metric-prod*',
q: 'kafka'
}
, function (error, response) {
console.log(response);
});
}
and the response on the console is :
{ error:
{ root_cause: [ [Object] ],
type: 'security_exception',
reason: 'missing authentication token for REST request [/metric-
prod*/_search?q=kafka]',
header: { 'WWW-Authenticate': 'Basic realm="security" charset="UTF-8"'
} },
status: 401 }
I also tried doing classic post request :
export function createUser(request,response,next){
var username = request.params.username;
var userData = querystring.stringify(request.body);
console.log(userData);
var options ={
hostname: 'noev02vr.fr',
port: 9200,
rejectUnauthorized: false,
path: "_xpack/security/user/"+username,
method:'POST',
headers: {
'Authorization': 'Basic ' + prodPass,
'Content-Type': 'application/json',
'Content-Length': userData.length
}
};
var post_req=http.request(options, function(res){
console.log('post user reussi');
res.on('data', function(data){
response.writeHead(res.statusCode);
response.write(data);
console.log(res.statusCode);
});
});
post_req.write(userData);
post_req.end();
}
and I get a 500 error.

Basic Authentication
const { Client } = require('#elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200', //Replace with your URL.
auth: {
username: 'elastic',
password: '*****' //Replace with your password
}
})
Otherwise, you can provide your credentials in the node(s) URL
const { Client } = require('#elastic/elasticsearch')
const client = new Client({
node: 'https://username:password#localhost:9200'
})
If you have enable ssl then this are the config
const { Client } = require('#elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200',
auth: {
username: 'elastic',
password: '*****'
},
ssl: {
ca: fs.readFileSync('./cacert.pem'),
rejectUnauthorized: false
}
})
You can get Your username and password refer this link
https://www.elastic.co/guide/en/cloud-enterprise/current/ece-password-reset-elastic.html

Related

Azure AD publisher's authorization token request returned 302

In an Azure Function as a backend for my webpage, I requested an Azure AD publisher's authorization token as per this page instructed.
This is the line of codes of my Azure Functions:
// Stringfy request body
const postData = querystring.stringify({
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'resource': resource,
});
// Initiate options
var httpAgent = new http.Agent();
httpAgent.maxSockets = 200;
const options = {
hostname: 'login.microsoftonline.com',
path: `/${tenantId}/oauth2/token`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
agent: httpAgent,
}
const tokenReq = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf-8')
res.on('data', (chunk) => {
console.log(chunk);
body += chunk;
});
res.on('end', () => {
console.log('No more data in response.');
console.log("body:" + body);
context.res = {
status: 200,
body: body,
};
});
});
tokenReq.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
context.res = {
status: 500,
body: `problem with request: ${e.message}`,
}
});
// write data to request body
tokenReq.write(postData);
tokenReq.end();
The expected response was the access token that I require, however running it locally I got STATUS 302, and a header containing a location and some other parameters, as a response. In my understanding STATUS 302 states that the URL is moved temporarily to the location provided in the header. Now, I don't know what I'm supposed to do, the request that I have to make is supposed to be a POST request so a redirection would not work. I've also tried to make a new request after receiving the redirect URL, but I got an error message saying: getaddrinfo ENOTFOUND {redirect URL from header}. What did I do wrong here?
The 302 error was caused by http module, you use require('http'); and http.request(options, (res).... to do the request, so it shows 302 error.
I suggest you to use var request = require('request'); to do the request, below is my function code for your reference (before use request module, you need to run npm install request to install it first):
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
var result = await generatetoken(context);
context.res = {
body: result
};
}
function generatetoken(context){
var request = require('request');
var options = {
'method': 'POST',
'url': 'https://login.microsoftonline.com/<your tenant id>/oauth2/token',
'headers': {
'Content-Type': 'application/x-www-url-form-urlencoded'
},
form: {
'client_id': 'xxxxxx',
'grant_type': 'client_credentials',
'resource': 'xxxxx',
'client_secret': 'xxxxx'
}
};
return new Promise(function(resolve, reject) {
request(options, function(err, res) {
if (err) {
reject(err);
} else {
context.log(res.body);
resolve(res.body);
}
})
})
}

How to get a Token API with fetch

I have to get a token from an url for an API of "ile de France mobilité", I'm using Fetch and I don't understand how to do this. This is my actual code :
var client_id = "(my client_id)";
var client_secret = "(my client_secret)";
// var url_get = "https://traffic.api.iledefrance-mobilites.fr/v1/tr-global/estimated-timetable";
var grant_type = 'client_credentials'
var token_url = 'https://as.api.iledefrance-mobilites.fr/api/oauth/token'
var scope = 'read-data'
const options = {
method: 'POST',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
},
body: {
grant_type: grant_type,
client_id: client_id,
client_secret: client_secret,
scope: scope
}
}
const fetch_response = await fetch(token_url, options)
const json = await fetch_response
console.log(json)
response.json(json)
And for answer I have
'Other stuf and :'
[Symbol(Response internals)]: {
url: 'https://as.api.iledefrance-mobilites.fr/api/oauth/token',
status: 401,
statusText: 'Unauthorized',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
}
}
Does someone know how to do this ?
In my index.html i call this script:
async function asyncCall() {
const api_url = `/trajet`
const response = await fetch(api_url, {method: 'POST'})
const json = await response
//console.log(json)
}
asyncCall()
server:
const app = express()
app.listen(3000, () => console.log('listening at 3000'))
app.use(express.static('public'))
app.use(express.json())
app.use(bodyParser.json())
HTTP Status Code 401 Unauthorized indicates that your request lacks valid authentication credentials. So there must be a problem in your body that you are sending.
Try sending the body in this format:
body: 'grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret,

How to use hapi-auth-jwt2 authentication on a path on hapi.js?

I've an issue on using an access token in hapi.js. I'm unable to understand how I can use that token to authenticate. I'm following this article dwyl/hapi-auth-jwt2. I'm using mongodb as my database. But until I send my request like this http://localhost:8000/restricted?token=mycreatedtoken, I can't log into {auth: 'jwt'} pages. But sending the request like this does not seem right. So how can I use that token? Don't I have to save that in local storage or a database to access? This is my code:
app.js
const jwt = require('jsonwebtoken');
await server.register(require('hapi-auth-jwt2'));
server.auth.strategy('jwt', 'jwt', {
key: 'NeverShareYourSecret',
validate: validate,
verifyOptions: { algorithms: ['HS256'] }
});
server.auth.default('jwt');
validate function:
const validate = async (decoded, req) => {
let user = await User.findOne({ _id: decoded.id });
if (user) {
req.user = user;
return { isValid: true };
} else {
return { isValid: false };
}
};
for login:
method: 'POST',
path: '/login',
config: { auth: false },
handler: async function(req, h) {
try {
let { username, password } = req.payload;
let student = await student.findOne({
username
});
let validUser = student && (await bcrypt.compareSync(password,student.password));
if (validUser) {
let token = jwt.sign({ id: user.id }, 'mysecretkey');
console.log('tpken'+token);
// return h.view('welcome');
return { token };
} else {
return boom.unauthorized('incorrect pass');
}
}
}
signup
method: 'POST',
path: '/student',
config: { auth: false },
handler: async function(req, h) {
try {
let salt = bcrypt.genSaltSync(10);
req.payload.password = bcrypt.hashSync(req.payload.password, salt);
let student = new User(req.payload);
let result = await student.save();
const expiresIn = 24 * 60 * 60;
let token = jwt.sign({ id: result.id }, 'mysecretkey',{ expiresIn: expiresIn
});
return {token} ;
}
}
this path is using jwt token.
{
method: 'GET',
path: '/register',
config: { auth: 'jwt' },
handler: async (request, h) => {
try {
return h.view('student');
} catch(err){
return h.response(err).code(500);
}
}
}
Could you please share your validate function? I understand that you can generate the JWT token. In order to user that token to authenticate your request, you need to send that token with "Authorization" header on your requests to your server.
I am using react for frontend and this is my setup to send JWT token to server.
import axios, {AxiosInstance} from 'axios';
const createClient: () => AxiosInstance = () => {
const options = {
baseURL: process.env.REACT_APP_API_URL,
responseType: 'json',
withCredentials: true,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Authorization': ''
},
};
const instance = axios.create(options);
// Set the AUTH token for any request
// ref: https://stackoverflow.com/questions/43051291/attach-authorization-header-for-all-axios-requests
instance.interceptors.request.use(function (config) {
const token = localStorage.getItem("USER_JWT_TOKEN");
config.headers.Authorization = token ? `Bearer ${token}` : '';
return config;
});
return instance;
};
export default createClient();
Then when I make requests with this setup, automatically axios sends authentication headers in my all requests.
import apiClient from "./apiClient";
const results = await apiClient.get(`/users`);
There is the curl preview of the request that I copied from chrome's network panel.
curl 'https://myserver.com/users' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36' -H 'Accept: application/json, text/plain, */*' -H 'Referer: https://myserver.com/' -H 'Origin: https://myserver.com' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...... long JWT token string here' -H 'X-Requested-With: XMLHttpRequest' --compressed

Get token oAuth using npm

I'm trying to develop a service using nodeJS that retrieve a token OAuth from a server. But I have every time an error.
this the function.
var express = require('express')
var http = require('http');
var httpRequest = require('request');
var bodyParser = require('body-parser');
var app = express()
app.get('/get-token', function (request, response) {
// Ask for token
httpRequest({
url: 'https://my-server.com/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic SdfdhffhPeHVBTV84OExfVWFmR1cwMklh'
},
form: {
'grant_type': 'password',
'username': 'myLogin',
'password': 'myPwd',
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
});
When I make a request, the server return this error:
{ [Error: unable to verify the first certificate] code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }
Would you have an idea how I can process or if there is a package npm that make the same job ?
Best regards
This wokrks for me
...
app.get('/get-token', function (request, response) {
// Ask for token
httpRequest({
rejectUnauthorized: false,
url: 'https://my-server.com/token',
...

Setup Node.js HTTPS to work with HAPROXY

I'm trying to make my nodejs app to communicate with HAPROXY via https. The idea is that nodejs sends message to haproxy via https, haproxy routes message forward.
I used request.js library and all worked fine, but now I need to perform this task without any libraries. The scenario is following. If environment variable is 1, I should use HTTP, in other cases -HTTPS. The problem is that when I use https and haproxy, I get "Socket hangup error", but everything works fine with request.js. Here is my code.
const protocol = process.env.NODE_ENV === 1 ? require('http') : require('https');
then I configure HTTPS
this.api = url.parse(app.get('API_HAPROXY'));
this.options = {
port: this.api.port,
hostname: this.api.hostname,
path: '/api/report',
method: 'POST',
headers: {
"Content-Type": "application/json",
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};
Because I don't want to use ca to validate ssh keys I use NODE_TLS_REJECT_UNAUTHORIZED=0
reportData(json) {
const req = protocol.request(this.options, (res) => {
res.on('error', (err) => {
this.logger.error(`Failed to report ${err.message}`)
})
});
req.write(JSON.stringify(json));
req.end();
req.on('error', (err) => {
this.logger.error(`Failed to report ${err.message}`);
});
}
In this case I get socket hangup error while using HTTPS
Here is my request configuration
request({
uri: `${this.api}/api/report`,
method: 'POST',
json,
}, (err, response) => {
if (err || response.statusCode !== 200) {
this.logger.error(`Failed to report : ${err ? err.message : response.statusCode}`);
} else {
this.logger.info(`Report was sent`);
}
});
The issue was fixed by adding content-length header to the options.headers.
this.api = url.parse(app.get('API_HAPROXY')); this.options = {
port: this.api.port,
hostname: this.api.hostname,
path: '/api/report',
method: 'POST',
headers: {
"Content-Type": "application/json",
"Content-Length: <calculated length of the object you want to send in bytes >
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};

Categories