import { Zodios } from '@zodios/core';
import { AxiosRequestConfig } from '../../../utils/axios.js';
import { getJwt } from '../../../utils/getJwt.js';
import { authenticationApis } from '../apis/authenticationApi.js';
import { AuthConfig } from '../authConfig.js';
import { Credentials } from '../types/credentials.js';
import AuthenticatedMethods from './authenticatedMethods.js';
import Methods from './methods.js';
/**
* Authentication methods of the Tableau Server REST API
*
* @export
* @class AuthenticationMethods
* @link https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_authentication.htm
*/
export class AuthenticationMethods extends Methods<typeof authenticationApis> {
constructor(baseUrl: string, axiosConfig: AxiosRequestConfig) {
super(new Zodios(baseUrl, authenticationApis, { axiosConfig }));
}
/**
* Signs you in as a user on the specified site on Tableau Server or Tableau Cloud.
*
* @param {AuthConfig} authConfig - The authentication configuration
* @link https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_authentication.htm#sign_in
*/
signIn = async (authConfig: AuthConfig): Promise<Credentials> => {
return (
await this._apiClient.signIn({
credentials: {
site: {
contentUrl: authConfig.siteName,
},
...(await (async () => {
switch (authConfig.type) {
case 'pat':
return {
personalAccessTokenName: authConfig.patName,
personalAccessTokenSecret: authConfig.patValue,
};
case 'direct-trust':
return {
jwt: await getJwt({
username: authConfig.username,
config: {
type: 'connected-app',
clientId: authConfig.clientId,
secretId: authConfig.secretId,
secretValue: authConfig.secretValue,
},
scopes: authConfig.scopes,
additionalPayload: authConfig.additionalPayload,
}),
};
case 'uat':
return {
isUat: true,
jwt: await getJwt({
username: authConfig.username,
config: {
type: 'uat',
tenantId: authConfig.tenantId,
issuer: authConfig.issuer,
usernameClaimName: authConfig.usernameClaimName,
privateKey: authConfig.privateKey,
keyId: authConfig.keyId,
},
scopes: authConfig.scopes,
additionalPayload: authConfig.additionalPayload,
}),
};
}
})()),
},
})
).credentials;
};
}
/**
* Authentication methods of the Tableau Server REST API that assume the user is already authenticated
*
* @export
* @class AuthenticatedAuthenticationMethods
* @link https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_authentication.htm
*/
export class AuthenticatedAuthenticationMethods extends AuthenticatedMethods<
typeof authenticationApis
> {
constructor(baseUrl: string, creds: Credentials, axiosConfig: AxiosRequestConfig) {
super(new Zodios(baseUrl, authenticationApis, { axiosConfig }), creds);
}
/**
* Signs you out of the current session. This call invalidates the authentication token that is created by a call to Sign In.
*
* Required scopes: none
*
* @link https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_authentication.htm#sign_out
*/
signOut = async (): Promise<void> => {
await this._apiClient.signOut(undefined, {
...this.authHeader,
});
};
}