Skip to main content

Token Connection

Token Connection

The following example illustrates how to make a connection to a key service with a Token.


Code Sample

See the C environment settings for details on setting the project environment.


< c_connect_token.cpp>
/****************************************************************************************
* c_connect_token.c
*
*
*****************************************************************************************/

#include <stdio.h>
#include "teagent_c.h"

void PrintError(const char * msg)
{
char szErr[1024];
uint32_t nLength = 1024;
uint32_t nCode = GetLastTEError(szErr, &nLength);
printf("%s Code = 0x%x, Message = %s\n", msg, nCode, szErr);
}

int main()
{
TE_HANDLE h;
TE_RETCODE ret;
char * szServerName = "localhost"; /* server name */
long lPort = 8888; /* port number */
char token[50];
int length = 50;

memset(token, 0, length);

/* Initialize the TE environment */
TEEnvInit();

/* Open a connection with native user and get token */
{
char * szUserName = "ls"; /* user name */
char * szPassword = "password1A"; /* password */

h = OpenConnectionWithNativeUser(
TE_SSL_CONNECTION, /* connection type */
szServerName, /* TE server name */
lPort, /* TE server port */
szUserName, /* user name */
strlen(szUserName), /* user name length */
szPassword, /* password */
strlen(szPassword) /* password length */
);
if (h == 0)
{
PrintError("Connection error:");
}
else
{
printf("Connected to %s at %d as %s\n", szServerName, lPort, szUserName);

/* Get token */
ret = GetToken(h, token, &length);
if(ret) {
PrintError("GetToken() failed:");
}
else {
printf("token (length %d) = %s\n", length, token);
}

/* Close a connection */
CloseConnection(h);
printf("Disconnected from server\n\n");
}
}

/* Connect to server with token */
if(strlen(token) > 0)
{
TE_HANDLE lHandle2 = OpenConnectionWithToken(
TE_SSL_CONNECTION, /* connection type */
szServerName, /* TE server name */
lPort, /* TE server port */
token, /* token */
strlen(token) /* token length */
);
if (lHandle2 == 0)
{
PrintError("Connection error:");
}
else {
printf("Connected to %s at %d with token %s.\n", szServerName, lPort, token);

/* Close a connection */
CloseConnection(lHandle2);
printf("Disconnected from server\n\n");
}
}

/* Uninitialize the TE environment */
TEEnvClose();

return 0;
}
Output
Connected to localhost at 8888 as ls
token (length 28) = iiTHbpSBJvC8H9dZR0pLgciXx44=
Disconnected from server

Connected to localhost at 8888 with token iiTHbpSBJvC8H9dZR0pLgciXx44=.
Disconnected from server