Core SDK Integration - rezolved/rezolve_sdk_sampleapp_android GitHub Wiki
Base Rezolve SDK integration requires core module dependency:
dependencies {
def rezolveSdkVersion = "3.2.0"
implementation "com.rezolve.sdk:core-android:$rezolveSdkVersion"
// ...
}
In the samples to the right, accessToken is the Login JWT token you created in previous section.
String API_KEY = "your_api_key";
String ENVIRONMENT = "https://core.sbx.eu.rezolve.com/api";
String accessToken = "abc123.abc123.abc123"; // JWT token from auth server
String entityId = "123"; // from auth server
String partnerId = "123"; // from auth server
String deviceId = "wlkCDA2Hy/CfMqVAShslBAR/0sAiuRIUm5jOg0a"; // from stored device_id, see "Generating the device_id" section from "JWT Authentication".
// Use builder to create instance of SDK and set SDK Params
// Pass in an AuthRequestProvider here, to handle expiring JWT tokens
RezolveSDK rezolveSDK = new RezolveSDK.Builder()
.setApiKey(API_KEY)
.setEnv(ENVIRONMENT)
.setAuthRequestProvider(new PartnerAuthRequestProvider(AuthService.getInstance()))
.build();
// Set JWT Auth Token from partner auth server
rezolveSDK.setAuthToken(accessToken);
// Start session, again supplying JWT auth token
rezolveSDK.createSession(accessToken, entityId, partnerId, new RezolveInterface() {
@Override
public void onInitializationSuccess(RezolveSession rezolveSession, String entityId, String partnerId) {
// set device_id so it can be passed in x-header
RezolveSDK.setDeviceIdHeader(deviceId);
// use created session to access managers. Example...
rezolveSession.getAddressbookManager().get(...);
}
@Override
public void onInitializationFailure() {
// handle error
}
});
Handling JWT Expiration & Session Preservation
The Login JWT you generate is included in the headers of every SDK transmission. Thus, when your consumer logs out, you can expire the JWT, and the app will cease communication with the Rezolve server. To do this, create a new JWT with an expiration stamp in the past, and supply it to the SDK.
This also means you are required to handle JWT token expiration/renewal if you want a session to continue.
Example is provided below. It is NOT an example of implementing SDK code, but rather an example of implementing session renewal with your own authentication server.
The SDK makes every call to the Rezolve server using an http client; if a call to the server results in a "401 token expired" response, the http client will ask for a new token using RezolveSDK.AuthRequestProvider. The Partner Auth Service you passed in to the SDK Builder must handle this JWT renewal.
It should be noted that the Partner Auth Service will typically handle all partner auth needs. Duties may include processing username/passwords for login, handling registering your users, and handling password resets, in addition to JWT renewal.
The code example show one way of implementing JWT renewal.
In the class PartnerAuthRequestProvider the Partner Auth Service implements RezolveSDK.AuthRequestProvider, to handle the JWT renewal requirements of the SDK. If the http client receives a "401 token expired", it will call RezolveSDK.GetAuthRequest to either confirm logout or renew the token. The token is renewed, but is only returned if the ping to the partner auth server to check login status succeeds. If the partner auth server says the user is not logged in, the renewed token is not returned, and the user can no longer make requests. If the user is still logged in, the updated JWT is returned.
// example Partner Auth Request Provider
// this would handle partner user login against partner server, password reset,
// as well as JWT token renewal
class PartnerAuthRequestProvider implements RezolveSDK.AuthRequestProvider {
private final AuthService authService;
PartnerAuthRequestProvider(AuthService authService) {
this.authService = authService;
}
@Override
public RezolveSDK.GetAuthRequest getAuthRequest() {
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new IllegalStateException("You can't run this method from main thread");
}
//set blocking call as the refresh token callback
final RefreshTokenCallbackToBlockingCall callback = new RefreshTokenCallbackToBlockingCall();
authService.refreshAuthToken(callback);
// ping the partner auth service
RezolveSDK.GetAuthRequest authRequest = PartnerPingCallbackToBlockingCall.getResult();
return authRequest;
}
}
class RefreshTokenCallbackToBlockingCall {
private RezolveSDK.GetAuthRequest result = null;
private final CountDownLatch countDownLatch = new CountDownLatch(1);
// on successful refresh, wait for the ping response
public void onRefreshAuthTokenSuccess(@NonNull String authToken) {
result = RezolveSDK.GetAuthRequest.authorizationHeader(authToken);
countDownLatch.countDown();
}
// getResult is only triggered after a result is received from the partner auth server
RezolveSDK.GetAuthRequest getResult() {
try {
countDownLatch.await();
return result;
} catch (InterruptedException e) {
// handle the exception
}
}
}
Another example of AuthRequestProvider can be found in our sample app.