5858import com .appirio .tech .core .service .identity .representation .Achievement ;
5959import com .appirio .tech .core .service .identity .representation .Country ;
6060import com .appirio .tech .core .service .identity .representation .Credential ;
61- import com .appirio .tech .core .service .identity .representation .CredentialInvitation ;
6261import com .appirio .tech .core .service .identity .representation .CredentialRequest ;
63- import com .appirio .tech .core .service .identity .representation .CredentialVerification ;
62+ import com .appirio .tech .core .service .identity .representation .User2fa ;
6463import com .appirio .tech .core .service .identity .representation .Email ;
6564import com .appirio .tech .core .service .identity .representation .ProviderType ;
6665import com .appirio .tech .core .service .identity .representation .Role ;
@@ -1504,6 +1503,67 @@ public ApiResponse validateSocial(
15041503 createValidationResult ((err == null ), err ));
15051504 }
15061505
1506+ @ PATCH
1507+ @ Path ("/{resourceId}/2fa" )
1508+ @ Timed
1509+ public ApiResponse updateUser2fa (
1510+ @ Auth AuthUser authUser ,
1511+ @ PathParam ("resourceId" ) String resourceId ,
1512+ @ Valid PostPutRequest <User2fa > postRequest ,
1513+ @ Context HttpServletRequest request ) {
1514+
1515+ logger .info (String .format ("update user 2fa(%s)" , resourceId ));
1516+
1517+ TCID id = new TCID (resourceId );
1518+ validateResourceIdAndCheckPermission (authUser , id , user2faFactory .getUpdateScopes ());
1519+ // checking param
1520+ checkParam (postRequest );
1521+
1522+ User2fa user2fa = postRequest .getParam ();
1523+
1524+ if (user2fa .getEnabled () == null ) {
1525+ throw new APIRuntimeException (SC_BAD_REQUEST , String .format (MSG_TEMPLATE_MANDATORY , "enabled" ));
1526+ }
1527+
1528+ Long userId = Utils .toLongValue (id );
1529+
1530+ logger .info (String .format ("findUserById(%s)" , resourceId ));
1531+ User2fa user2faInDb = userDao .findUser2faById (userId );
1532+ if (user2faInDb ==null )
1533+ throw new APIRuntimeException (SC_NOT_FOUND , MSG_TEMPLATE_USER_NOT_FOUND );
1534+
1535+ Boolean shouldSendInvite = false ;
1536+ if (user2faInDb .getEnabled () == null ) {
1537+ userDao .insertUser2fa (userId , user2fa .getEnabled ());
1538+ shouldSendInvite = user2fa .getEnabled ();
1539+ } else if (!user2faInDb .getEnabled ().equals (user2fa .getEnabled ())) {
1540+ userDao .update2fa (user2faInDb .getId (), user2fa .getEnabled (), false );
1541+ shouldSendInvite = user2fa .getEnabled ();
1542+ }
1543+
1544+ if (shouldSendInvite ) {
1545+ Response response ;
1546+ try {
1547+ response = new Request (diceAuth .getDiceApiUrl () + "/v1/connection/submit" , "POST" )
1548+ .param ("emailId" , user2faInDb .getEmail ())
1549+ .header ("x-api-key" , diceAuth .getApiKey ())
1550+ .execute ();
1551+ } catch (Exception e ) {
1552+ logger .error ("Error when calling 2fa submit api" , e );
1553+ throw new APIRuntimeException (SC_INTERNAL_SERVER_ERROR , "Error when calling 2fa submit api" );
1554+ }
1555+ if (response .getStatusCode () != HttpURLConnection .HTTP_CREATED ) {
1556+ throw new APIRuntimeException (HttpURLConnection .HTTP_INTERNAL_ERROR ,
1557+ String .format ("Got unexpected response from remote service. %d %s" , response .getStatusCode (),
1558+ response .getMessage ()));
1559+ }
1560+ logger .info (response .getText ());
1561+ send2faInvitationEmailEvent (user2faInDb , diceAuth .getDiceUrl () + "/verify/" + response .getText ());
1562+ }
1563+
1564+ return ApiResponseFactory .createResponse ("SUCCESS" );
1565+ }
1566+
15071567 @ POST
15081568 @ Path ("/2faCredentials" )
15091569 @ Timed
@@ -1524,15 +1584,15 @@ public ApiResponse issueCredentials(
15241584 logger .info (String .format ("issue credential (%s)" , credential .getEmail ()));
15251585
15261586 // find user by email
1527- User user = userDao .findUserByEmail (credential .getEmail ());
1587+ User2fa user = userDao .findUserCredentialByEmail (credential .getEmail ());
15281588
15291589 // return 404 if user is not found
15301590 if (user == null )
15311591 throw new APIRuntimeException (SC_NOT_FOUND , MSG_TEMPLATE_USER_NOT_FOUND );
1532- if (user .getMfaEnabled () == null || !user .getMfaEnabled ()) {
1592+ if (user .getEnabled () == null || !user .getEnabled ()) {
15331593 throw new APIRuntimeException (SC_BAD_REQUEST , "2FA is not enabled for user" );
15341594 }
1535- List <Role > roles = roleDao .getRolesBySubjectId (Long . parseLong ( user .getId (). getId () ));
1595+ List <Role > roles = roleDao .getRolesBySubjectId (user .getUserId ( ));
15361596 ObjectMapper mapper = new ObjectMapper ();
15371597 ObjectNode body = mapper .createObjectNode ();
15381598 body .put ("comment" , "TC credential" );
@@ -1559,7 +1619,7 @@ public ApiResponse issueCredentials(
15591619 preview .set ("attributes" , attributes );
15601620 Response response ;
15611621 try {
1562- response = new Request (diceAuth .getDiceUrl ()+"/v1/credentialoffer/api/credentialoffer" , "POST" )
1622+ response = new Request (diceAuth .getDiceApiUrl ()+"/v1/credentialoffer/api/credentialoffer" , "POST" )
15631623 .header ("x-api-key" , diceAuth .getApiKey ())
15641624 .json (mapper .writeValueAsString (body ))
15651625 .execute ();
@@ -1583,12 +1643,12 @@ public ApiResponse issueCredentials(
15831643 @ Timed
15841644 public ApiResponse update2faVerification (
15851645 @ Auth AuthUser authUser ,
1586- @ Valid PostPutRequest <CredentialVerification > putRequest ,
1646+ @ Valid PostPutRequest <User2fa > putRequest ,
15871647 @ Context HttpServletRequest request ) {
15881648
15891649 Utils .checkAccess (authUser , user2faFactory .getUpdateScopes (), Utils .AdminRoles );
15901650 checkParam (putRequest );
1591- CredentialVerification credential = putRequest .getParam ();
1651+ User2fa credential = putRequest .getParam ();
15921652
15931653 if (credential .getEmail () == null || credential .getEmail ().length () == 0 ) {
15941654 throw new APIRuntimeException (SC_BAD_REQUEST , String .format (MSG_TEMPLATE_MANDATORY , "Email address" ));
@@ -1599,7 +1659,7 @@ public ApiResponse update2faVerification(
15991659 logger .info (String .format ("update 2fa verification (%s) - %b" , credential .getEmail (), credential .getVerified ()));
16001660
16011661 // find user by email
1602- CredentialVerification credVerification = userDao .findUserCredentialByEmail (credential .getEmail ());
1662+ User2fa credVerification = userDao .findUserCredentialByEmail (credential .getEmail ());
16031663
16041664 // return 404 if user is not found
16051665 if (credVerification == null )
@@ -1609,43 +1669,11 @@ public ApiResponse update2faVerification(
16091669 throw new APIRuntimeException (SC_BAD_REQUEST , "2FA is not enabled for user" );
16101670 }
16111671 if (!credVerification .getVerified ().equals (credential .getVerified ())) {
1612- userDao .update2faVerification (credVerification .getId (), credential .getVerified ());
1672+ userDao .update2fa (credVerification .getId (), true , credential .getVerified ());
16131673 }
16141674 return ApiResponseFactory .createResponse ("User verification updated" );
16151675 }
16161676
1617- @ POST
1618- @ Path ("/2faInvitation" )
1619- @ Timed
1620- public ApiResponse send2faInvitation (
1621- @ Auth AuthUser authUser ,
1622- @ Valid PostPutRequest <CredentialInvitation > postRequest ,
1623- @ Context HttpServletRequest request ) {
1624- Utils .checkAccess (authUser , user2faFactory .getCreateScopes (), Utils .AdminRoles );
1625- checkParam (postRequest );
1626- CredentialInvitation invitation = postRequest .getParam ();
1627-
1628- if (invitation .getEmail () == null || invitation .getEmail ().length () == 0 ) {
1629- throw new APIRuntimeException (SC_BAD_REQUEST , String .format (MSG_TEMPLATE_MANDATORY , "Email address" ));
1630- }
1631- if (invitation .getInvitationUrl () == null || invitation .getInvitationUrl ().length () == 0 ) {
1632- throw new APIRuntimeException (SC_BAD_REQUEST , String .format (MSG_TEMPLATE_MANDATORY , "Invitation Url" ));
1633- }
1634- logger .info (String .format ("send 2fa invitation to (%s)" , invitation .getEmail ()));
1635-
1636- // find user by email
1637- User user = userDao .findUserByEmail (invitation .getEmail ());
1638-
1639- // return 404 if user is not found
1640- if (user == null )
1641- throw new APIRuntimeException (SC_NOT_FOUND , MSG_TEMPLATE_USER_NOT_FOUND );
1642- if (user .getMfaEnabled () == null || !user .getMfaEnabled ()) {
1643- throw new APIRuntimeException (SC_BAD_REQUEST , "2FA is not enabled for user" );
1644- }
1645- send2faInvitationEmailEvent (user , invitation .getInvitationUrl ());
1646- return ApiResponseFactory .createResponse ("SUCCESS" );
1647- }
1648-
16491677 @ POST
16501678 @ Path ("/oneTimeToken" )
16511679 @ Timed
@@ -2061,7 +2089,7 @@ private void sendActivationEmailEvent(User user, String redirectUrl) {
20612089 }
20622090 }
20632091
2064- private void send2faInvitationEmailEvent (User user , String inviteLink ) {
2092+ private void send2faInvitationEmailEvent (User2fa user , String inviteLink ) {
20652093
20662094 EventMessage msg = EventMessage .getDefault ();
20672095 msg .setTopic ("external.action.email" );
@@ -2086,11 +2114,7 @@ private void send2faInvitationEmailEvent(User user, String inviteLink) {
20862114 payload .put ("recipients" , recipients );
20872115
20882116 msg .setPayload (payload );
2089- try {
2090- this .eventBusServiceClient .reFireEvent (msg );
2091- } catch (Exception e ) {
2092- logger .error ("Error occured while publishing the events to new kafka." );
2093- }
2117+ this .eventBusServiceClient .reFireEvent (msg );
20942118 }
20952119
20962120 private void sendWelcomeEmailEvent (User user ) {
0 commit comments