Skip to content

user authentication

sea edited this page May 22, 2023 · 3 revisions

Previous examples - in this wiki - assumed a webPDF server, that allowed anonymous user logins (AnonymousAuthProvider) - meaning the server grants access to unauthenticated (unknown) users. Depending on you server configuration anonymous logins may be disallowed.

Should the server require a login via username and password, you should provide a "UserAuthProvider" to your session.

The usage is identical for REST and SOAP - hence only a REST usage example shall be provided:

// ...
authProvider: AuthProvider = new UserAuthProvider("User123", "password");
try {
   let session: RestSession<RestDocument> = await SessionFactory.createInstance(sessionContext, authProvider);
   // ...
   await session.close();
} catch (ex: any) {
   // ...
}

A UserAuthProvider will automatically use the provided authentication data, to login to the server. It will store the session access token - that the server provides for a valid authentication. It will also automatically refresh such a token should it expire.

You might also want to set a skew time for the token refreshing, to avoid expiry issues caused by transfer delays.

// Set the token refresh skew time in seconds.
let sessionContext: SessionContext = new SessionContext(WebServiceProtocol.REST,new URL("http://localhost:8080/webPDF/"));
sessionContext.setSkewTime(5);

Clone this wiki locally