Skip to content

Login example and README.md change #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -107,6 +109,73 @@ This example shows how to use the Splunk Enterprise SDK for JavaScript and Node.
});
});
```
##### Login with sessionKey

```shell
# Create a sessionKey
curl -k -u <username>:<password> <scheme>://<host>:<port>/services/auth/login -d username=<username> -d password=<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 sessionKey here
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
#### From shell ####
# Enable token authetication
curl -k -u <username>:<password> -X POST <scheme>://<host>:<port>/services/admin/token-auth/tokens_auth -d disabled=false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe /services/admin/token-auth/tokens_auth the admin in this path is necessary so no change needed


# Create a token
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
```

```shell
#### From web ####
# Enable token authentication
Go to settings > Tokens and click on 'Enable Token Authentication'

# 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.
```

```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

Expand Down
48 changes: 48 additions & 0 deletions examples/node/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var splunkjs = require('../../index');

/*
################ Login with sessionKey #################
Execute following command to create sessionKey manually:
curl -k -u <username>:<password> <scheme>://<host>:<port>/services/auth/login -d username=<username> -d password=<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 <username>:<password> -X POST <scheme>://<host>:<port>/services/admin/token-auth/tokens_auth -d disabled=false

Execute following command to create bearer token manually:
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
*/
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");
});