Skip to content

Commit 009932c

Browse files
committed
feat(security): add TFA exemption header for bypassing two-factor authentication
1 parent 34e2ac9 commit 009932c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

backend/src/main/java/com/park/utmstack/config/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public final class Constants {
146146
public static final String API_KEY_HEADER = "Utm-Api-Key";
147147
public static final List<String> API_ENDPOINT_IGNORE = Collections.emptyList();
148148

149+
public static final String TFA_EXEMPTION_HEADER = "X-Bypass-TFA";
150+
149151

150152
private Constants() {
151153
}

backend/src/main/java/com/park/utmstack/security/jwt/TokenProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.park.utmstack.security.jwt;
22

33

4+
import com.park.utmstack.config.Constants;
45
import com.park.utmstack.security.AuthoritiesConstants;
56
import com.park.utmstack.util.CipherUtil;
67
import io.jsonwebtoken.*;
@@ -16,10 +17,12 @@
1617
import org.springframework.stereotype.Component;
1718
import tech.jhipster.config.JHipsterProperties;
1819

20+
import javax.servlet.http.HttpServletRequest;
1921
import java.security.Key;
2022
import java.util.Arrays;
2123
import java.util.Collection;
2224
import java.util.Date;
25+
import java.util.Optional;
2326
import java.util.stream.Collectors;
2427

2528
@Component
@@ -116,4 +119,16 @@ public boolean validateToken(String authToken) {
116119
}
117120
return false;
118121
}
122+
123+
public boolean canBypassTwoFactorAuth(HttpServletRequest request) {
124+
boolean tfaExemptionRequested = Boolean.parseBoolean(request.getHeader(Constants.TFA_EXEMPTION_HEADER));
125+
126+
boolean forceTfaAuth = Boolean.parseBoolean(
127+
Optional.ofNullable(System.getenv(Constants.PROP_TFA_ENABLE)).orElse("true")
128+
);
129+
130+
return tfaExemptionRequested || !forceTfaAuth;
131+
}
132+
133+
119134
}

backend/src/main/java/com/park/utmstack/web/rest/UserJWTController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public ResponseEntity<JWTToken> authorize(@Valid @RequestBody LoginVM loginVM, H
7777
throw new TooMuchLoginAttemptsException(String.format("Authentication blocked: IP %s exceeded login attempt threshold", ip));
7878
}
7979

80-
boolean authenticated = !Boolean.parseBoolean(Constants.CFG.get(Constants.PROP_TFA_ENABLE));
80+
boolean isTfaExempted = this.tokenProvider.canBypassTwoFactorAuth(request);
81+
boolean authenticated = !Boolean.parseBoolean(Constants.CFG.get(Constants.PROP_TFA_ENABLE)) || isTfaExempted;
8182

8283
UsernamePasswordAuthenticationToken authenticationToken =
8384
new UsernamePasswordAuthenticationToken(loginVM.getUsername(), loginVM.getPassword());

0 commit comments

Comments
 (0)