Skip to content

Commit

Permalink
docs: add docstrings to auth objects and endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jorwoods committed Oct 2, 2024
1 parent e1b8281 commit e41321d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tableauserverclient/models/tableau_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@ def deprecate_site_attribute():

# The traditional auth type: username/password
class TableauAuth(Credentials):
"""
The TableauAuth class defines the information you can set in a sign-in
request. The class members correspond to the attributes of a server request
or response payload. To use this class, create a new instance, supplying
user name, password, and site information if necessary, and pass the
request object to the Auth.sign_in method.
Parameters
----------
username : str
The user name for the sign-in request.
password : str
The password for the sign-in request.
site_id : str, optional
This corresponds to the contentUrl attribute in the Tableau REST API.
The site_id is the portion of the URL that follows the /site/ in the
URL. For example, "MarketingTeam" is the site_id in the following URL
MyServer/#/site/MarketingTeam/projects. To specify the default site on
Tableau Server, you can use an empty string '' (single quotes, no
space). For Tableau Cloud, you must provide a value for the site_id.
user_id_to_impersonate : str, optional
Specifies the id (not the name) of the user to sign in as. This is not
available for Tableau Online.
Examples
--------
>>> import tableauserverclient as TSC
>>> tableau_auth = TSC.TableauAuth('USERNAME', 'PASSWORD', site_id='CONTENTURL')
>>> server = TSC.Server('https://SERVER_URL', use_server_version=True)
>>> server.auth.sign_in(tableau_auth)
"""

def __init__(
self, username: str, password: str, site_id: Optional[str] = None, user_id_to_impersonate: Optional[str] = None
) -> None:
Expand All @@ -55,6 +92,43 @@ def __repr__(self):

# A Tableau-generated Personal Access Token
class PersonalAccessTokenAuth(Credentials):
"""
The PersonalAccessTokenAuth class defines the information you can set in a sign-in
request. The class members correspond to the attributes of a server request
or response payload. To use this class, create a new instance, supplying
token name, token secret, and site information if necessary, and pass the
request object to the Auth.sign_in method.
Parameters
----------
token_name : str
The name of the personal access token.
personal_access_token : str
The personal access token secret for the sign in request.
site_id : str, optional
This corresponds to the contentUrl attribute in the Tableau REST API.
The site_id is the portion of the URL that follows the /site/ in the
URL. For example, "MarketingTeam" is the site_id in the following URL
MyServer/#/site/MarketingTeam/projects. To specify the default site on
Tableau Server, you can use an empty string '' (single quotes, no
space). For Tableau Cloud, you must provide a value for the site_id.
user_id_to_impersonate : str, optional
Specifies the id (not the name) of the user to sign in as. This is not
available for Tableau Online.
Examples
--------
>>> import tableauserverclient as TSC
>>> tableau_auth = TSC.PersonalAccessTokenAuth("token_name", "token_secret", site_id='CONTENTURL')
>>> server = TSC.Server('https://SERVER_URL', use_server_version=True)
>>> server.auth.sign_in(tableau_auth)
"""

def __init__(
self,
token_name: str,
Expand Down Expand Up @@ -88,6 +162,45 @@ def __repr__(self):

# A standard JWT generated specifically for Tableau
class JWTAuth(Credentials):
"""
The JWTAuth class defines the information you can set in a sign-in
request. The class members correspond to the attributes of a server request
or response payload. To use this class, create a new instance, supplying
an encoded JSON Web Token, and site information if necessary, and pass the
request object to the Auth.sign_in method.
Parameters
----------
token_name : str
The name of the personal access token.
personal_access_token : str
The personal access token secret for the sign in request.
site_id : str, optional
This corresponds to the contentUrl attribute in the Tableau REST API.
The site_id is the portion of the URL that follows the /site/ in the
URL. For example, "MarketingTeam" is the site_id in the following URL
MyServer/#/site/MarketingTeam/projects. To specify the default site on
Tableau Server, you can use an empty string '' (single quotes, no
space). For Tableau Cloud, you must provide a value for the site_id.
user_id_to_impersonate : str, optional
Specifies the id (not the name) of the user to sign in as. This is not
available for Tableau Online.
Examples
--------
>>> import jwt
>>> import tableauserverclient as TSC
>>> jwt_token = jwt.encode(...)
>>> tableau_auth = TSC.JWTAuth(token, site_id='CONTENTURL')
>>> server = TSC.Server('https://SERVER_URL', use_server_version=True)
>>> server.auth.sign_in(tableau_auth)
"""

def __init__(self, jwt: str, site_id: Optional[str] = None, user_id_to_impersonate: Optional[str] = None) -> None:
if jwt is None:
raise TabError("Must provide a JWT token when using JWT authentication")
Expand Down
11 changes: 11 additions & 0 deletions tableauserverclient/server/endpoint/auth_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ def sign_in(self, auth_req: "Credentials") -> contextmgr:
# The distinct methods are mostly useful for explicitly showing api version support for each auth type
@api(version="3.6")
def sign_in_with_personal_access_token(self, auth_req: "Credentials") -> contextmgr:
"""Passthrough to sign_in method"""
return self.sign_in(auth_req)

@api(version="3.17")
def sign_in_with_json_web_token(self, auth_req: "Credentials") -> contextmgr:
"""Passthrough to sign_in method"""
return self.sign_in(auth_req)

@api(version="2.0")
def sign_out(self) -> None:
"""Sign out of current session."""
url = f"{self.baseurl}/signout"
# If there are no auth tokens you're already signed out. No-op
if not self.parent_srv.is_signed_in():
Expand All @@ -88,6 +91,11 @@ def sign_out(self) -> None:

@api(version="2.6")
def switch_site(self, site_item: "SiteItem") -> contextmgr:
"""
Switch to a different site on the server. This will sign out of the
current site and sign in to the new site. If used as a context manager,
will sign out of the new site upon exit.
"""
url = f"{self.baseurl}/switchSite"
switch_req = RequestFactory.Auth.switch_req(site_item.content_url)
try:
Expand All @@ -109,6 +117,9 @@ def switch_site(self, site_item: "SiteItem") -> contextmgr:

@api(version="3.10")
def revoke_all_server_admin_tokens(self) -> None:
"""
Revokes all personal access tokens for all server admins on the server.
"""
url = f"{self.baseurl}/revokeAllServerAdminTokens"
self.post_request(url, "")
logger.info("Revoked all tokens for all server admins")

0 comments on commit e41321d

Please sign in to comment.