Skip to content

Commit 697c0ad

Browse files
authored
Merge pull request #138 from splunk/DVPL-9897
Login example and README.md change
2 parents b9eca50 + d4f2997 commit 697c0ad

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

README.md

+69
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ This HTML example uses the Splunk Enterprise SDK for JavaScript to list all jobs
8989

9090
This example shows how to use the Splunk Enterprise SDK for JavaScript and Node.js to list all jobs:
9191

92+
##### Login with username and password
93+
9294
```javascript
9395
var splunkjs = require('splunk-sdk');
9496

@@ -107,6 +109,73 @@ This example shows how to use the Splunk Enterprise SDK for JavaScript and Node.
107109
});
108110
});
109111
```
112+
##### Login with sessionKey
113+
114+
```shell
115+
# Create a sessionKey
116+
curl -k -u <username>:<password> <scheme>://<host>:<port>/services/auth/login -d username=<username> -d password=<password>
117+
```
118+
119+
```javascript
120+
var serviceWithSessionKey = new splunkjs.Service(
121+
{
122+
// Replace the host if you are accessing remote host
123+
scheme: 'https',
124+
host: 'localhost',
125+
port: '8089',
126+
sessionKey: SESSION_KEY, // Add your sessionKey here
127+
version: '8',
128+
});
129+
130+
serviceWithSessionKey.get("search/jobs", { count: 1 }, function (err, res) {
131+
if (err) {
132+
console.log(err);
133+
} else }
134+
console.log("Login successful with sessionKey");
135+
}
136+
});
137+
```
138+
139+
##### Login with token
140+
141+
```shell
142+
#### From shell ####
143+
# Enable token authetication
144+
curl -k -u <username>:<password> -X POST <scheme>://<host>:<port>/services/admin/token-auth/tokens_auth -d disabled=false
145+
146+
# Create a token
147+
curl -k -u <username>:<password> -X POST <scheme>://<host>:<port>/services/authorization/tokens?output_mode=json --data name=<username> --data audience=Users --data-urlencode expires_on=+30d
148+
```
149+
150+
```shell
151+
#### From web ####
152+
# Enable token authentication
153+
Go to settings > Tokens and click on 'Enable Token Authentication'
154+
155+
# Create a token
156+
1. Go to settings > Token and click on 'New Token'
157+
2. Enter the relevant information
158+
3. Copy the created token and save it somewhere safe.
159+
```
160+
161+
```javascript
162+
var serviceWithBearerToken = new splunkjs.Service(
163+
{
164+
// Replace the host if you are accessing remote host
165+
scheme: 'https',
166+
host: 'localhost',
167+
port: '8089',
168+
sessionKey: TOKEN, // Add your token here
169+
version: '8',
170+
});
171+
172+
serviceWithBearerToken.get("search/jobs", { count: 2 }, function (err, res) {
173+
if (err)
174+
console.log(err);
175+
else
176+
console.log("Login successful with bearer token");
177+
});
178+
```
110179

111180
## SDK examples
112181

examples/node/login.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var splunkjs = require('../../index');
2+
3+
/*
4+
################ Login with sessionKey #################
5+
Execute following command to create sessionKey manually:
6+
curl -k -u <username>:<password> <scheme>://<host>:<port>/services/auth/login -d username=<username> -d password=<password>
7+
*/
8+
var serviceWithSessionKey = new splunkjs.Service(
9+
{
10+
// Replace the host if you are accessing remote host
11+
scheme: 'https',
12+
host: 'localhost',
13+
port: '8089',
14+
sessionKey: 'SESSION_KEY', // Add your session key
15+
version: '8',
16+
});
17+
18+
serviceWithSessionKey.get("search/jobs", { count: 2 }, function (err, res) {
19+
if (err)
20+
console.log(err);
21+
else
22+
console.log("Login successful with sessionKey");
23+
});
24+
25+
/*
26+
################ Login with token #################
27+
Execute following command to enable token authentication:
28+
curl -k -u <username>:<password> -X POST <scheme>://<host>:<port>/services/admin/token-auth/tokens_auth -d disabled=false
29+
30+
Execute following command to create bearer token manually:
31+
curl -k -u <username>:<password> -X POST <scheme>://<host>:<port>/services/authorization/tokens?output_mode=json --data name=<username> --data audience=Users --data-urlencode expires_on=+30d
32+
*/
33+
var serviceWithBearerToken = new splunkjs.Service(
34+
{
35+
// Replace the host if you are accessing remote host
36+
scheme: 'https',
37+
host: 'localhost',
38+
port: '8089',
39+
sessionKey: 'TOKEN', // Add your token here
40+
version: '8',
41+
});
42+
43+
serviceWithBearerToken.get("search/jobs", { count: 2 }, function (err, res) {
44+
if (err)
45+
console.log(err);
46+
else
47+
console.log("Login successful with bearer token");
48+
});

0 commit comments

Comments
 (0)