Splunk Cloud Services Cloud-Auth-Client
Splunk Cloud Services Cloud-Auth-Client contains an authentication library for front-end web applications, along with code and examples to enable you to authenticate with Splunk Cloud Services in a web application using the JavaScript programming language.
You can use the @splunkdev/cloud-auth-client
library alone or with the Splunk Cloud Services SDK for JavaScript to programatically access Splunk Cloud Services.
Terms of Service
Splunk Cloud Services Terms of Service
Authorization Grant Types
This library supports the following OAuth authorization grant types:
For more about authorization flows that are supported by Splunk Cloud Services, see Plan apps for Splunk Cloud Services on the Splunk Developer Portal.
Get started
Install the @splunkdev/cloud-auth-client
package to enable your project to authenticate with Splunk Cloud Services.
Run the following command from your project directory if you use Yarn:
yarn add @splunkdev/cloud-auth-client
Run the following command from your project directory if you use npm:
npm install --save @splunkdev/cloud-auth-client
Migrating from @splunkdev/cloud-auth
If your apps use the previous version of the library, @splunkdev/cloud-auth, you'll need to migrate your apps. For details, see the Migration Guide.
Example
The following example shows how to work with the @splunkdev/cloud-auth-client
library.
React web application
This example demonstrates how to use the @splunkdev/cloud-auth-client
library in a React web application. For an example that you can run, see examples/cloud-auth-client-react-example.
import {
SplunkAuthClient,
SplunkAuthClientSettings,
GrantType,
} from '@splunkdev/cloud-auth-client';
import React, { Component } from 'react';
const authClientSettings = new SplunkAuthClientSettings(
GRANT_TYPE,
CLIENT_ID,
REDIRECT_URI,
ON_RESTORE_PATH,
AUTH_HOST,
AUTO_REDIRECT_TO_LOGIN,
RESTORE_PATH_AFTER_LOGIN,
MAX_CLOCK_SKEW,
QUERY_PARAMS_FOR_LOGIN,
AUTO_TOKEN_RENEWAL_BUFFER,
TOKEN_STORAGE_NAME,
REDIRECT_PARAMS_STORAGE_NAME,
ENABLE_TENANT_SCOPED_TOKENS,
ENABLE_MULTI_REGION_SUPPORT
);
const authClient = new SplunkAuthClient(authClientSettings, tenant);
class App extends Component {
state = {
loggedIn: false,
error: null,
};
componentDidMount() {
this.authenticate();
}
authenticate = async () => {
try {
const loggedIn = (await authClient.getAccessToken()) !== '';
this.setState({
loggedIn,
});
} catch (e) {
let errorMessage = '';
if (e) {
if (e.code === ERROR_CODE_OAUTH_PARAMS_TOKEN_NOT_FOUND) {
}
errorMessage = e.message ? e.message : e.toString();
}
this.setState({
loggedIn: false,
error: errorMessage,
});
}
};
render() {
const { error, loggedIn } = this.state;
if (error) {
return <div>Error: {error}</div>;
}
if (!loggedIn) {
return <div>Loading ...</div>;
}
return <div>Authenticated!</div>;
}
}
AuthClient configuration
The following example sets configuration options for SplunkAuthClient
.
{
grantType: 'pkce',
clientId: 'YOUR_CLIENT_ID',
redirectUri: 'https://YOUR_REDIRECT_URI.com/',
onRestorePath: function(path) { },
authHost: 'https://auth.scp.splunk.com/',
autoRedirectToLogin: true,
restorePathAfterLogin: true,
maxClockSkew: 600,
queryParamsForLogin: { },
autoTokenRenewalBuffer: 120,
tokenStorageName: 'splunk-token-storage',
redirectParamsStorageName: 'splunk-redirect-params-storage'
}
The SplunkAuthClient
is also configured with a tenant
at initialization.
tenant: ''
Usage with the Splunk Cloud SDK
The Splunk Cloud SDK accepts the following values as valid tokenSource inputs:
- an asynchronous function that returns a token string
- an object that implements the AuthManager interface
- a token string
export interface ServiceClientArgs {
tokenSource: AuthManager | string | TokenProviderAsyncFunction;
}
More information can be found in the @splunkdev/cloud-sdk
The following code snippets demonstrate how you can use the @splunkdev/cloud-auth-client
library to provide a valid tokenSource to the @splunkdev/cloud-sdk
.
const authClient = new SplunkAuthClient(authClientSettings);
const cloudSdk = new SplunkCloud({
tokenSource: authClient,
});
const cloudSdk = new SplunkCloud({
tokenSource: () => {
return authClient.getAccessToken();
},
});
const token = await authClient.getAccessToken();
const cloudSdk = new SplunkCloud({
tokenSource: token,
});
Documentation
For Splunk Cloud Services documentation, see the Splunk Developer Portal.
Contact
If you have questions, reach out to us on Slack in the #sdc channel or email us at devinfo@splunk.com.