Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit 1866c9d

Browse files
authored
Merge pull request #80 from appirio-tech/feature/2fa
Feature/2fa
2 parents 272bc44 + dca8473 commit 1866c9d

File tree

9 files changed

+31
-8
lines changed

9 files changed

+31
-8
lines changed

buildtokenproperties.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ DICEAUTH_DICE_API_URL=$(eval "echo \$${ENV}_DICEAUTH_DICE_API_URL")
2020
DICEAUTH_DICE_VERIFIER=$(eval "echo \$${ENV}_DICEAUTH_DICE_VERIFIER")
2121
DICEAUTH_DICE_API_KEY=$(eval "echo \$${ENV}_DICEAUTH_DICE_API_KEY")
2222
DICEAUTH_CREDDEFID=$(eval "echo \$${ENV}_DICEAUTH_CREDDEFID")
23+
DICEAUTH_OTP_DURATION=$(eval "echo \$${ENV}_OTP_DURATION")
2324
ZENDESK_ID=$(eval "echo \$${ENV}_ZENDESK_ID")
2425
SERVICEACC02_UID=$(eval "echo \$${ENV}_SERVICEACC02_UID")
2526
AUTH_SECRET=$(eval "echo \$${ENV}_AUTH_SECRET")
@@ -94,6 +95,7 @@ perl -pi -e "s|\{\{DICEAUTH_DICE_API_URL\}\}|$DICEAUTH_DICE_API_URL|g" $CONFFILE
9495
perl -pi -e "s|\{\{DICEAUTH_DICE_VERIFIER\}\}|$DICEAUTH_DICE_VERIFIER|g" $CONFFILENAME
9596
perl -pi -e "s|\{\{DICEAUTH_DICE_API_KEY\}\}|$DICEAUTH_DICE_API_KEY|g" $CONFFILENAME
9697
perl -pi -e "s/\{\{DICEAUTH_CREDDEFID\}\}/$DICEAUTH_CREDDEFID/g" $CONFFILENAME
98+
perl -pi -e "s/\{\{DICEAUTH_OTP_DURATION\}\}/$DICEAUTH_OTP_DURATION/g" $CONFFILENAME
9799
perl -pi -e "s/\{\{ZENDESK_KEY\}\}/$ZENDESK_KEY/g" $CONFFILENAME
98100
perl -pi -e "s/\{\{ZENDESK_ID\}\}/$ZENDESK_ID/g" $CONFFILENAME
99101
perl -pi -e "s/\{\{SERVICEACC01_CID\}\}/$SERVICEACC01_CID/g" $CONFFILENAME

src/main/java/com/appirio/tech/core/service/identity/dao/UserDAO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ public abstract class UserDAO implements DaoBase<User>, Transactional<UserDAO> {
171171
@SqlUpdate(
172172
"UPDATE common_oltp.user_2fa SET " +
173173
"otp=:otp, " +
174-
"otp_expire=current_timestamp + (5 ||' minutes')::interval " +
174+
"otp_expire=current_timestamp + (:duration ||' minutes')::interval " +
175175
"WHERE id=:id")
176-
public abstract int update2faOtp(@Bind("id") long id, @Bind("otp") String otp);
176+
public abstract int update2faOtp(@Bind("id") long id, @Bind("otp") String otp, @Bind("duration") int duration);
177177

178178
@SqlQuery(
179179
"UPDATE common_oltp.user_2fa x SET otp=null, otp_expire=null " +

src/main/java/com/appirio/tech/core/service/identity/resource/UserResource.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,10 @@ public ApiResponse update2faVerification(
16761676
if(credVerification.getEnabled() == null || !credVerification.getEnabled()) {
16771677
throw new APIRuntimeException(SC_BAD_REQUEST, "2FA is not enabled for user");
16781678
}
1679-
if(!credVerification.getVerified().equals(credential.getVerified())) {
1679+
// update only if it's true. We need to prevent changing verification status from true to false
1680+
// Otherwise 2fa will be skipped during the login flow.
1681+
// The only way to set verification to false is disabling the 2fa for that user.
1682+
if(credential.getVerified()) {
16801683
userDao.update2fa(credVerification.getId(), true, credential.getVerified());
16811684
}
16821685
return ApiResponseFactory.createResponse("User verification updated");
@@ -1706,8 +1709,8 @@ public ApiResponse createOtp(
17061709
throw new APIRuntimeException(SC_BAD_REQUEST, "2FA is not enabled for user");
17071710
}
17081711
String otp = Utils.generateRandomString(ALPHABET_DIGITS_EN, 6);
1709-
userDao.update2faOtp(user2faInDb.getId(), otp);
1710-
send2faCodeEmailEvent(user2faInDb, otp);
1712+
userDao.update2faOtp(user2faInDb.getId(), otp, diceAuth.getOtpDuration());
1713+
send2faCodeEmailEvent(user2faInDb, otp, diceAuth.getOtpDuration());
17111714
return ApiResponseFactory.createResponse("SUCCESS");
17121715
}
17131716

@@ -2193,7 +2196,7 @@ private void send2faInvitationEmailEvent(User2fa user, String inviteLink) {
21932196
this.eventBusServiceClient.reFireEvent(msg);
21942197
}
21952198

2196-
private void send2faCodeEmailEvent(User2fa user, String code) {
2199+
private void send2faCodeEmailEvent(User2fa user, String code, Integer duration) {
21972200

21982201
EventMessage msg = EventMessage.getDefault();
21992202
msg.setTopic("external.action.email");
@@ -2202,6 +2205,7 @@ private void send2faCodeEmailEvent(User2fa user, String code) {
22022205
Map<String,Object> data = new LinkedHashMap<String,Object>();
22032206
data.put("handle", user.getHandle());
22042207
data.put("code", code);
2208+
data.put("duration", duration);
22052209

22062210
payload.put("data", data);
22072211

src/main/java/com/appirio/tech/core/service/identity/util/auth/DICEAuth.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ public class DICEAuth {
1919
@NotNull
2020
private String credDefId;
2121

22+
@NotNull
23+
private Integer otpDuration;
24+
2225
private String credPreview = "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview";
2326

2427
public DICEAuth() {
2528
}
2629

27-
public DICEAuth(String diceUrl, String diceApiUrl, String diceVerifier, String diceApiKey, String credDefId) {
30+
public DICEAuth(String diceUrl, String diceApiUrl, String diceVerifier, String diceApiKey, String credDefId,
31+
Integer otpDuration) {
2832
this.diceUrl = diceUrl;
2933
this.diceApiUrl = diceApiUrl;
3034
this.diceVerifier = diceVerifier;
3135
this.diceApiKey = diceApiKey;
3236
this.credDefId = credDefId;
37+
this.otpDuration = otpDuration;
3338
}
3439

3540
public String getDiceUrl() {
@@ -72,6 +77,14 @@ public void setCredDefId(String credDefId) {
7277
this.credDefId = credDefId;
7378
}
7479

80+
public Integer getOtpDuration() {
81+
return otpDuration;
82+
}
83+
84+
public void setOtpDuration(Integer otpDuration) {
85+
this.otpDuration = otpDuration;
86+
}
87+
7588
public String getCredPreview() {
7689
return credPreview;
7790
}

src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ diceAuth:
9999
diceVerifier: @diceAuth.diceVerifier@
100100
diceApiKey: @diceAuth.diceApiKey@
101101
credDefId: @diceAuth.credDefId@
102+
otpDuration: @diceAuth.otpDuration@
102103

103104
# Authorized accounts
104105
serviceAccount:

src/main/resources/config.yml.localdev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ diceAuth:
9191
diceVerifier: dummy
9292
diceApiKey: dummy
9393
credDefId: dummy
94+
otpDuration: 10
9495

9596
# LDAP Settings
9697
ldap:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CREATE TABLE common_oltp.user_2fa (id SERIAL, user_id DECIMAL(10,0) UNIQUE NOT NULL, enabled BOOLEAN DEFAULT false NOT NULL, verified BOOLEAN DEFAULT false NOT NULL, PRIMARY KEY (id), FOREIGN KEY (user_id) REFERENCES common_oltp.user (user_id));
1+
CREATE TABLE common_oltp.user_2fa (id SERIAL NOT NULL, user_id NUMERIC(10,0) NOT NULL, enabled BOOLEAN DEFAULT false NOT NULL, verified BOOLEAN DEFAULT false NOT NULL, otp CHARACTER VARYING(6), otp_expire TIMESTAMP(6) WITHOUT TIME ZONE, CONSTRAINT user_2fa_pk PRIMARY KEY (id), CONSTRAINT user_2fa_user_id_fkey FOREIGN KEY (user_id) REFERENCES "user" ("user_id"), UNIQUE (user_id));

token.properties.localdev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
@diceAuth.diceVerifier@=dummy
3737
@diceAuth.diceApiKey@=dummy
3838
@diceAuth.credDefId@=dummy
39+
@diceAuth.otpDuration@=10
3940

4041
@zendesk.secret@=ZENDESK_SECRET
4142
@zendesk.idprefix@=ZENDESK_PREFIX

token.properties.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
@diceAuth.diceVerifier@={{DICEAUTH_DICE_VERIFIER}}
5757
@diceAuth.diceApiKey@={{DICEAUTH_DICE_API_KEY}}
5858
@diceAuth.credDefId@={{DICEAUTH_CREDDEFID}}
59+
@diceAuth.otpDuration@={{DICEAUTH_OTP_DURATION}}
5960

6061
@zendesk.secret@={{ZENDESK_KEY}}
6162
@zendesk.idprefix@={{ZENDESK_ID}}

0 commit comments

Comments
 (0)