Skip to content

Commit

Permalink
SDK-2248: Add Digital Identity Session retrieval service
Browse files Browse the repository at this point in the history
  • Loading branch information
irotech committed Jul 25, 2023
1 parent b45d59a commit 6e7d16a
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,29 @@ public class DigitalIdentityClient {
* Create a sharing session to initiate a sharing process based on a policy
*
* @param request Details of the request like policy, extensions and push notification for the application
* @return an {@link ShareSession} ID, status and expiry of the newly created Share Session
* @return ID, status and expiry of the newly created share session
* @throws DigitalIdentityException Thrown if the session creation is unsuccessful
*/
public ShareSession createShareSession(ShareSessionRequest request) throws DigitalIdentityException {
return identityService.createShareSession(sdkId, keyPair, request);
}

public Object fetchShareSession(String sessionId) {
return identityService.fetchShareSession(sessionId);
/**
* Retrieve the sharing session
*
* @param sessionId ID of the session to retrieve
* @return ID, status and expiry of the share session
* @throws DigitalIdentityException Thrown if the session retrieval is unsuccessful
*/
public ShareSession fetchShareSession(String sessionId) throws DigitalIdentityException {
return identityService.fetchShareSession(sdkId, keyPair, sessionId);
}

/**
* Create a sharing session QR code to initiate a sharing process based on a policy
*
* @param sessionId Session ID the QR code will belong to
* @return ID and URI of the newly created Share Session QR code
* @return ID and URI of the newly created share session QR code
* @throws DigitalIdentityException Thrown if the QR code creation is unsuccessful
*/
public ShareSessionQrCode createShareQrCode(String sessionId) throws DigitalIdentityException {
Expand All @@ -65,7 +72,7 @@ public ShareSessionQrCode createShareQrCode(String sessionId) throws DigitalIden
* Retrieve the sharing session QR code
*
* @param qrCodeId ID of the QR code to retrieve
* @return The content of the Share Session QR code
* @return The content of the share session QR code
* @throws DigitalIdentityException Thrown if the QR code retrieval is unsuccessful
*/
public ShareSessionQrCode fetchShareQrCode(String qrCodeId) throws DigitalIdentityException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.yoti.api.client.identity;

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonProperty;

public class ShareSession {

@JsonProperty(Property.ID)
private String id;

@JsonProperty(Property.STATUS)
private String status;

@JsonProperty(Property.EXPIRY)
private String created;
private String updated;
private String expiry;
private String qrCodeId;
private String receiptId;

public String getId() {
return id;
Expand All @@ -21,15 +22,70 @@ public String getStatus() {
return status;
}

public String getCreated() {
return created;
}

public String getUpdated() {
return updated;
}

public String getExpiry() {
return expiry;
}

public String getQrCodeId() {
return qrCodeId;
}

public String getReceiptId() {
return receiptId;
}

@JsonProperty(Property.ID)
public void setId(String id) {
this.id = id;
}

@JsonProperty(Property.STATUS)
public void setStatus(String status) {
this.status = status;
}

@JsonProperty(Property.CREATED)
public void setCreated(String created) {
this.created = created;
}

@JsonProperty(Property.UPDATED)
public void setUpdated(String updated) {
this.updated = updated;
}

@JsonProperty(Property.EXPIRY)
public void setExpiry(String expiry) {
this.expiry = expiry;
}

@JsonProperty(Property.QR_CODE)
public void setQrCodeId(Map<String, Object> qrCode) {
this.qrCodeId = (String) qrCode.get(Property.ID);
}

@JsonProperty(Property.RECEIPT)
public void setReceiptId(Map<String, Object> receipt) {
this.receiptId = (String) receipt.get(Property.ID);
}

private static final class Property {

private static final String ID = "id";
private static final String CREATED = "created";
private static final String UPDATED = "updated";
private static final String STATUS = "status";
private static final String EXPIRY = "expiry";
private static final String QR_CODE = "qrCode";
private static final String RECEIPT = "receipt";

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public class UnsignedPathFactory {

// Share V2
private static final String IDENTITY_SESSION_CREATION = "/v2/sessions";
private static final String IDENTITY_SESSION_RETRIEVAL = "/v2/sessions/%s";
private static final String IDENTITY_SESSION_QR_CODE_CREATION = "/v2/sessions/%s/qr-codes";
private static final String IDENTITY_SESSION_QR_CODE_RETRIEVAL_TEMPLATE = "/v2/qr-codes/%s";
private static final String IDENTITY_SESSION_QR_CODE_RETRIEVAL = "/v2/qr-codes/%s";

// Share V1
private static final String PROFILE = "/profile/%s?appId=%s";
Expand Down Expand Up @@ -38,12 +39,16 @@ public String createIdentitySessionPath() {
return IDENTITY_SESSION_CREATION;
}

public String createIdentitySessionRetrievalPath(String sessionId) {
return format(IDENTITY_SESSION_RETRIEVAL, sessionId);
}

public String createIdentitySessionQrCodePath(String sessionId) {
return format(IDENTITY_SESSION_QR_CODE_CREATION, sessionId);
}

public String createIdentitySessionQrCodeRetrievalPath(String qrCodeId) {
return format(IDENTITY_SESSION_QR_CODE_RETRIEVAL_TEMPLATE, qrCodeId);
return format(IDENTITY_SESSION_QR_CODE_RETRIEVAL, qrCodeId);
}

public String createProfilePath(String appId, String connectToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ShareSession createShareSession(String sdkId, KeyPair keyPair, ShareSessi

String path = pathFactory.createIdentitySessionPath();

LOG.debug("Requesting Share Session Creation for SDK ID '{}' at '{}'", sdkId, path);
LOG.debug("Requesting share session creation for SDK ID '{}' at '{}'", sdkId, path);

try {
byte[] payload = ResourceMapper.writeValueAsString(shareSessionRequest);
Expand All @@ -79,8 +79,26 @@ public ShareSession createShareSession(String sdkId, KeyPair keyPair, ShareSessi
}
}

public Object fetchShareSession(String sessionId) {
return null;
public ShareSession fetchShareSession(String sdkId, KeyPair keyPair, String sessionId)
throws DigitalIdentityException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application Key Pair");
notNull(sessionId, "Session ID");

String path = pathFactory.createIdentitySessionRetrievalPath(sessionId);

LOG.debug("Requesting share session with ID '{}' at '{}'", sessionId, path);

try {
SignedRequest request = createSignedRequest(sdkId, keyPair, path);

return request.execute(ShareSession.class);
} catch (Exception ex) {
throw new DigitalIdentityException(
String.format("Error while fetching the share session with ID '{%s}' ", sessionId),
ex
);
}
}

public ShareSessionQrCode createShareQrCode(String sdkId, KeyPair keyPair, String sessionId)
Expand All @@ -91,7 +109,7 @@ public ShareSessionQrCode createShareQrCode(String sdkId, KeyPair keyPair, Strin

String path = pathFactory.createIdentitySessionQrCodePath(sessionId);

LOG.debug("Requesting Share Session QR code Creation for session ID '{}' at '{}'", sessionId, path);
LOG.debug("Requesting share session '{}' QR code creation at '{}'", sessionId, path);

try {
SignedRequest request = createSignedRequest(sdkId, keyPair, path, HTTP_POST, EMPTY_JSON);
Expand All @@ -112,16 +130,17 @@ public ShareSessionQrCode fetchShareQrCode(String sdkId, KeyPair keyPair, String

String path = pathFactory.createIdentitySessionQrCodeRetrievalPath(qrCodeId);

LOG.info("Requesting Share Session QR code with ID '{} at '{}'", qrCodeId, path);
LOG.info("Requesting share session QR code with ID '{} at '{}'", qrCodeId, path);

try {
SignedRequest request = createSignedRequest(sdkId, keyPair, path);

return request.execute(ShareSessionQrCode.class);
} catch (GeneralSecurityException ex) {
throw new DigitalIdentityException("Error while signing the share QR code fetch request ", ex);
} catch (IOException | URISyntaxException | ResourceException ex) {
throw new DigitalIdentityException("Error while executing the share QR code fetch request ", ex);
} catch (Exception ex) {
throw new DigitalIdentityException(
String.format("Error while fetching the share session QR code with ID '{%s}' ", qrCodeId),
ex
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,18 @@ public void client_CreateShareSessionException_DigitalIdentityException() throws
}

@Test
public void client_FetchShareSessionException_DigitalIdentityException() {
public void client_FetchShareSessionException_DigitalIdentityException() throws IOException {
when(keyPairSource.getFromStream(any(KeyStreamVisitor.class))).thenReturn(keyPair);

DigitalIdentityClient identityClient = new DigitalIdentityClient(
AN_SDK_ID,
validKeyPairSource,
keyPairSource,
identityService
);

String exMessage = "Fetch Share Session Error";
when(identityService.fetchShareSession(A_SESSION_ID)).thenThrow(new DigitalIdentityException(exMessage));
when(identityService.fetchShareSession(AN_SDK_ID, keyPair, A_SESSION_ID))
.thenThrow(new DigitalIdentityException(exMessage));

DigitalIdentityException ex = assertThrows(
DigitalIdentityException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public String identityShare(Model model) throws URISyntaxException {
model.addAttribute("qrcode_session_status", fetchQrCode.getSession().getStatus());
model.addAttribute("qrcode_session_expiry", fetchQrCode.getSession().getExpiry());

ShareSession fetchSession = execute(() -> client.fetchShareSession(sessionId), model);
if (fetchSession == null) {
return "error";
}

model.addAttribute("fetch_session_id", fetchSession.getId());
model.addAttribute("fetch_session_created", fetchSession.getCreated());
model.addAttribute("fetch_session_updated", fetchSession.getUpdated());
model.addAttribute("fetch_session_expiry", fetchSession.getExpiry());
model.addAttribute("fetch_session_status", fetchSession.getStatus());
model.addAttribute("fetch_session_qrcode_id", fetchSession.getQrCodeId());
model.addAttribute("fetch_session_receipt_id", fetchSession.getReceiptId());

return "digital-identity-share";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ <h2 th:text="${message}">Digital Identity Share Example page</h2>
<p> SdkId: <strong th:text="${sdkId}"></strong></p>

<div>
<p> Id: <strong th:text="${session_id}"></strong></p>
<p><strong>Created Session</strong></p>
<p> ID: <strong th:text="${session_id}"></strong></p>
<p> Status: <strong th:text="${session_status}"></strong></p>
<p> Expiry: <strong th:text="${session_expiry}"></strong></p>
</div>

<div>
<p><strong>Session QR Code</strong></p>
<p> Id: <strong th:text="${session_qrcode_id}"></strong></p>
<p><strong>Created Session QR Code</strong></p>
<p> ID: <strong th:text="${session_qrcode_id}"></strong></p>
<p> URI: <strong th:text="${session_qrcode_uri}"></strong></p>
</div>

Expand All @@ -35,10 +36,19 @@ <h2 th:text="${message}">Digital Identity Share Example page</h2>
<p> Expiry: <strong th:text="${qrcode_expiry}"></strong></p>
<p> Extensions: <strong th:text="${qrcode_extensions}"></strong></p>
<p> Redirect URI: <strong th:text="${qrcode_redirect_uri}"></strong></p>
<p> Session ID: <strong th:text="${qrcode_session_id}"></strong></p>
<p> Session Status: <strong th:text="${qrcode_session_status}"></strong></p>
<p> Session Expiry: <strong th:text="${qrcode_session_expiry}"></strong></p>
</div>

<div>
<p><strong>Fetched Session</strong></p>
<p> Created: <strong th:text="${fetch_session_created}"></strong></p>
<p> Updated: <strong th:text="${fetch_session_updated}"></strong></p>
<p> Expiry: <strong th:text="${fetch_session_expiry}"></strong></p>
<p> Status: <strong th:text="${fetch_session_status}"></strong></p>
<p> QR Code ID: <strong th:text="${fetch_session_qrcode_id}"></strong></p>
<p> Receipt ID: <strong th:text="${fetch_session_receipt_id}"></strong></p>
</div>
</section>
</main>

Expand Down

0 comments on commit 6e7d16a

Please sign in to comment.