From e0fa1672f6ded943926256135522630d4962072f Mon Sep 17 00:00:00 2001 From: vmalaviya Date: Fri, 20 Aug 2021 14:03:05 +0530 Subject: [PATCH 1/4] login example added --- examples/node/login.js | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/node/login.js diff --git a/examples/node/login.js b/examples/node/login.js new file mode 100644 index 000000000..7eb5396f5 --- /dev/null +++ b/examples/node/login.js @@ -0,0 +1,48 @@ +var splunkjs = require('../../index'); + +/* +################ Login with sessionKey ################# +Execute following command to create sessionKey manually: +curl -k -u : ://:/services/auth/login -d username= -d password= +*/ +var serviceWithSessionKey = new splunkjs.Service( + { + // Replace the host if you are accessing remote host + scheme: 'https', + host: 'localhost', + port: '8089', + sessionKey: 'SESSION_KEY', // Add your session key + version: '8', + }); + +serviceWithSessionKey.get("search/jobs", { count: 2 }, function (err, res) { + if (err) + console.log(err); + else + console.log("Login successful with sessionKey"); +}); + +/* +################ Login with token ################# +Execute following command to enable token authentication: +curl -k -u : -X POST ://:/services/admin/token-auth/tokens_auth -d disabled=false + +Execute following command to create bearer token manually: +curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name=admin --data audience=Users --data-urlencode expires_on=+30d +*/ +var serviceWithBearerToken = new splunkjs.Service( + { + // Replace the host if you are accessing remote host + scheme: 'https', + host: 'localhost', + port: '8089', + sessionKey: 'TOKEN', // Add your token here + version: '8', + }); + +serviceWithBearerToken.get("search/jobs", { count: 2 }, function (err, res) { + if (err) + console.log(err); + else + console.log("Login successful with bearer token"); +}); From 467400adc04c49492782b7ab6c33ec40ca529a41 Mon Sep 17 00:00:00 2001 From: vmalaviya Date: Fri, 20 Aug 2021 14:03:25 +0530 Subject: [PATCH 2/4] Update README.md --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README.md b/README.md index 6932640cc..7d066b267 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ This HTML example uses the Splunk Enterprise SDK for JavaScript to list all jobs This example shows how to use the Splunk Enterprise SDK for JavaScript and Node.js to list all jobs: +##### Login with username and password + ```javascript var splunkjs = require('splunk-sdk'); @@ -107,6 +109,69 @@ This example shows how to use the Splunk Enterprise SDK for JavaScript and Node. }); }); ``` +##### Login with sessionKey +```shell +# Create a sessionKey +curl -k -u : ://:/services/auth/login -d username= -d password= +``` + +```javascript +var serviceWithSessionKey = new splunkjs.Service( + { + // Replace the host if you are accessing remote host + scheme: 'https', + host: 'localhost', + port: '8089', + sessionKey: 'SESSION_KEY', // Add your session key + version: '8', + }); + +serviceWithSessionKey.get("search/jobs", { count: 1 }, function (err, res) { + if (err) { + console.log(err); + } else } + console.log("Login successful with sessionKey"); + } +}); +``` + +##### Login with token + +```shell +# Enable token authetication from shell +curl -k -u : -X POST ://:/services/admin/token-auth/tokens_auth -d disabled=false + +# Enable token authentication from web +Go to settings > Tokens and click on 'Enable Token Authentication' +``` +```shell +# Create a token from shell +curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name=admin --data audience=Users --data-urlencode expires_on=+30d + +# Create a token from web +1. Go to settings > Token and click on 'New Token' +2. Enter the relevant information +3. Copy the created token and save it somewhere safe. +``` + +```javascript +var serviceWithBearerToken = new splunkjs.Service( + { + // Replace the host if you are accessing remote host + scheme: 'https', + host: 'localhost', + port: '8089', + sessionKey: 'TOKEN', // Add your token here + version: '8', + }); + +serviceWithBearerToken.get("search/jobs", { count: 2 }, function (err, res) { + if (err) + console.log(err); + else + console.log("Login successful with bearer token"); +}); +``` ## SDK examples From 8a9b586dd04bdf4302c7292f3f247f4e9727cd0d Mon Sep 17 00:00:00 2001 From: vmalaviya Date: Fri, 20 Aug 2021 14:20:05 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7d066b267..ca099f762 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ This example shows how to use the Splunk Enterprise SDK for JavaScript and Node. }); ``` ##### Login with sessionKey + ```shell # Create a sessionKey curl -k -u : ://:/services/auth/login -d username= -d password= @@ -122,7 +123,7 @@ var serviceWithSessionKey = new splunkjs.Service( scheme: 'https', host: 'localhost', port: '8089', - sessionKey: 'SESSION_KEY', // Add your session key + sessionKey: SESSION_KEY, // Add your sessionKey here version: '8', }); @@ -138,17 +139,20 @@ serviceWithSessionKey.get("search/jobs", { count: 1 }, function (err, res) { ##### Login with token ```shell -# Enable token authetication from shell +#### From shell #### +# Enable token authetication curl -k -u : -X POST ://:/services/admin/token-auth/tokens_auth -d disabled=false -# Enable token authentication from web -Go to settings > Tokens and click on 'Enable Token Authentication' +# Create a token +curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name=admin --data audience=Users --data-urlencode expires_on=+30d ``` + ```shell -# Create a token from shell -curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name=admin --data audience=Users --data-urlencode expires_on=+30d +#### From web #### +# Enable token authentication +Go to settings > Tokens and click on 'Enable Token Authentication' -# Create a token from web +# Create a token 1. Go to settings > Token and click on 'New Token' 2. Enter the relevant information 3. Copy the created token and save it somewhere safe. @@ -161,7 +165,7 @@ var serviceWithBearerToken = new splunkjs.Service( scheme: 'https', host: 'localhost', port: '8089', - sessionKey: 'TOKEN', // Add your token here + sessionKey: TOKEN, // Add your token here version: '8', }); From d4f2997425a6e687898a5688d897e376b84fb2cd Mon Sep 17 00:00:00 2001 From: vmalaviya Date: Mon, 23 Aug 2021 12:44:24 +0530 Subject: [PATCH 4/4] Change: admin -> username for token creation --- README.md | 2 +- examples/node/login.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ca099f762..646d863b1 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ serviceWithSessionKey.get("search/jobs", { count: 1 }, function (err, res) { curl -k -u : -X POST ://:/services/admin/token-auth/tokens_auth -d disabled=false # Create a token -curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name=admin --data audience=Users --data-urlencode expires_on=+30d +curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name= --data audience=Users --data-urlencode expires_on=+30d ``` ```shell diff --git a/examples/node/login.js b/examples/node/login.js index 7eb5396f5..f37d343e2 100644 --- a/examples/node/login.js +++ b/examples/node/login.js @@ -28,7 +28,7 @@ Execute following command to enable token authentication: curl -k -u : -X POST ://:/services/admin/token-auth/tokens_auth -d disabled=false Execute following command to create bearer token manually: -curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name=admin --data audience=Users --data-urlencode expires_on=+30d +curl -k -u : -X POST ://:/services/authorization/tokens?output_mode=json --data name= --data audience=Users --data-urlencode expires_on=+30d */ var serviceWithBearerToken = new splunkjs.Service( {