Re-authenticating your client session
A client session can re-authenticate with the Diffusion™ server at any time in order to change its principal or session properties, or avoid session expiry.
JavaScript
await session.security.reauthenticate('principal', 'password', {});
console.log('Authenticated as admin');
.NET
bool result = await session.Security.ReauthenticateAsync("control", Diffusion.Credentials.Password("password"), new Dictionary<string, string>());
Java and Android
final Session session = Diffusion.sessions().open("ws://localhost:8080");
final Security security = session.feature(Security.class);
security.reauthenticate(
"principal",
Diffusion.credentials().password("password"),
Collections.emptyMap());
C
static int on_reauthenticate(
const SESSION_T *session,
bool success,
void *context)
{
// session has been successfully reauthenticated
return HANDLER_SUCCESS;
}
void reauthenticate_session(
SESSION_T *session,
const char *new_principal,
const char *new_password)
{
CREDENTIALS_T *new_credentials = credentials_create_password(new_password);
DIFFUSION_REAUTHENTICATE_PARAMS_T params = {
.principal = new_principal,
.credentials = new_credentials,
.on_reauthenticate = on_reauthenticate
};
diffusion_reauthenticate(session, params, NULL);
credentials_free(new_credentials);
}
Apple
// create a credentials object encapsulating a string password
let credentials = PTDiffusionCredentials(password: "password")
// use the security feature from your session
session.security.reauthenticate("principal", credentials: credentials, sessionProperties: nil, completionHandler: { success, error in
if (error != nil) {
print("Error while reauthenticating: %@", error!.localizedDescription)
return
}
if (!success) {
print("Reauthentication failed")
return
}
print("Reauthenticated")
}, error: nil)
When the principal associated with a session changes, the following happens:
- The $Principal session property is updated to contain the new principal.
- The roles associated with the old principal are removed from the session and those roles associated with the new principal are assigned to the session.
- Topic subscriptions are re-evaluated. The session is unsubscribed from any topic the new principal does not have permissions for, and will be subscribed to new topics that it now does have permissions for.