HYPR SDK for Java
This article includes options to integrate HYPR SDK for Java into your app.
HYPR SDK for Java web library can be used with no configuration and will provide a UI that can be used to complete registration, deregistration, login, and the listing of user-associated devices.
HYPR SDK for Java web can be used to register and authenticate users for the following:
- Out-of-band Device Registration
- Out-of-band Device Authentication (Push to Mobile)
- User Device Retrieval
- User Device Removal
To get started integrating HYPR SDK for Java Web into your server, make sure following steps have been taken:
- Create an Access Token. Access Tokens are used for calls between HYPR SDK for Java and HYPR server. Without an Access Token the calls will fail, as the APIs are protects.
- Follow the integration guide in this article
- Follow the example registration, authentication and deregistration call samples in the Example API
Adding HYPR Java to the Path
java-client-<version>.jar
must be added to your project's classpath
.
Creating a Configuration Object
//This is the base URL of your HYPR server.
String baseURL = "https://example.gethypr.com";
//This is the relying party application ID for your application.
String defaultAppId = "<YOUR_APP_ID_FROM_CONTROL_CENTER>";
HYPRConfiguration hyprConfiguration = new HYPRConfiguration(baseURL, defaultAppId).apiToken("<YOUR_APP_ACCESS_TOKEN>");
Device Registration
//Create the registration object
ClientRegistrationAPIClient clientRegistrationAPIClient = new ClientRegistrationAPIClient(hyprConfiguration);
//Generate the random pin
String randomPin = WebUtils.generateRandomPIN() + "";
// Create QR code image for mobile to scan
// This sample code will generate a file with the QR code for testing purposes.
// If using this in a web application, you can use the results from clientRegistrationAPIClient.getQRCode(randomPin) to create a <img> tag in your HTML/Javascript code.
String qrCodeFile = createQRCodeFile(clientRegistrationAPIClient.getQRCode(randomPin));
System.out.println("*********************************************");
System.out.println("TO REGISTER, SCAN THIS QR CODE IMAGE BY YOUR MOBILE APP >>>> " + qrCodeFile + " <<<<<");
System.out.println("*********************************************");
//Get the pin registration response
ClientPinResponse clientPinResponse = clientRegistrationAPIClient.startRegistrationAndGetPin(username, randomPin);
//Get the final registration response
ClientRegistrationCompleteResponse clientRegistrationCompleteResponse = clientRegistrationAPIClient.completeRegistration(clientPinResponse);
clientRegistrationCompleteResponse.getStatus();
System.out.println(clientRegistrationCompleteResponse.getStatus().getResponseMessage());
Device Authentication
//Callback for user authentication function
HyprWebAuthenticationCallback hyprWebAuthenticationCallback = new HyprWebAuthenticationCallback() {
@Override
public void onFailure(HttpResponse httpResponse, String message) {
displayError(objectMapper, httpResponse, message);
}
@Override
public void onComplete(HttpResponse httpResponse, DeviceAuthenticationRequestStatus clientCompleteAuthStatus) throws IOException {
System.out.println("*********************************************");
System.out.println("Authentication completed successfully :: " + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(clientCompleteAuthStatus));
System.out.println("*********************************************");
}
@Override
public void onAuthRequestSent(DeviceAuthenticationRequestResponse clientRequestResponse) {
System.out.println("*********************************************");
try {
System.out.println("Authentication Has Started and PUSH has been sent :: " + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(clientRequestResponse));
} catch (JsonProcessingException e) {
System.out.println("ERROR :: Could not start the authentication and parse the result....");
}
System.out.println("*********************************************");
}
@Override
public void onPollResult(DeviceAuthenticationRequestStatus clientAuthStatus) throws IOException {
System.out.println("*********************************************");
System.out.println("Authentication Polling Result :: " + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(clientAuthStatus));
System.out.println("*********************************************");
}
};
//Create a client authorization object and pass it the configuration
ClientAuthorizationAPIClient clientAuthorizationAPIClient = new ClientAuthorizationAPIClient(hyprConfiguration);
//Start the authorization for a username provided and pass it the callback
clientAuthorizationAPIClient.handleWebAuthentication(username, hyprWebAuthenticationCallback);
Synchronous Authentication
ClientAuthorization clientAuthorization = new ClientAuthorization(hyprConfiguration);
RequestStatus authStatus = clientAuthorization.handleWebAuthSynchronous("bojan12345");
if(authStatus != null && authStatus.getValue().name().equals("COMPLETED")){
//auth is successful!
}
Getting a List of Devices for Users
//Create an instance of the device information class by passing it the hypr configuration
DeviceInformation deviceInformation = new DeviceInformation(hyprConfiguration);
//Call the get user devices method to get a list of devices.
//List will be empty if there are no devices for that user.
List<RegisteredUserDevice> registeredUserDeviceList = deviceInformation.getUserDevices("username");
Deleting a User's Registered Device
//Create a device information object
DeviceInformation deviceInformation = new DeviceInformation(hyprConfiguration);
//Call the delete user device method by passing it the username and the device id.
//You can get the device id by using the getUserDevices method.
boolean successfulDeletion = deviceInformation.deleteUserDevice("username", "deviceId");