Skip to content

Commit 31cb8d4

Browse files
authored
CDA-105: Updated authentication error message for expired API keys (#1667)
Resolves #1554 Updates API key error message from `no user for key` to `Authentication failed. The API Key may be invalid or no longer active.`
1 parent f9ca418 commit 31cb8d4

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

cwms-data-api/src/main/java/cwms/cda/data/dao/AuthDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.concurrent.TimeUnit;
4444
import java.util.stream.Collectors;
4545

46-
import javax.servlet.http.HttpServletResponse;
4746
import javax.sql.DataSource;
4847
import org.jetbrains.annotations.NotNull;
4948
import org.jooq.DSLContext;
@@ -56,6 +55,7 @@ public class AuthDao extends Dao<DataApiPrincipal> {
5655
public static final String SCHEMA_TOO_OLD = "The CWMS-Data-API requires schema version "
5756
+ "23.03.16 or later to handle authorization operations.";
5857
public static final String DATA_API_PRINCIPAL = "DataApiPrincipal";
58+
public static final String AUTH_ERROR_MSG = "Authentication failed. The API Key may be invalid or no longer active.";
5959
// At this level we just care that the user has permissions in *any* office
6060
private static final String RETRIEVE_GROUPS_OF_USER =
6161
ResourceHelper.getResourceAsString("/cwms/data/sql/user_groups.sql", AuthDao.class);
@@ -204,7 +204,7 @@ private String checkKey(String key) throws CwmsAuthException {
204204
if (rs.next()) {
205205
return rs.getString(1);
206206
} else {
207-
throw new CwmsAuthException("No user for key");
207+
throw new CwmsAuthException(AUTH_ERROR_MSG);
208208
}
209209
}
210210
} catch (SQLException ex) {

cwms-data-api/src/test/java/cwms/cda/api/auth/ApiKeyControllerTestIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.restassured.filter.log.LogDetail;
2727
import io.restassured.specification.RequestSpecification;
2828

29+
import static cwms.cda.data.dao.AuthDao.AUTH_ERROR_MSG;
2930
import static cwms.cda.data.dao.JsonRatingUtilsTest.loadResourceAsString;
3031
import static io.restassured.RestAssured.given;
3132
import static org.hamcrest.Matchers.*;
@@ -269,7 +270,8 @@ public void test_key_usage() throws Exception {
269270
.then()
270271
.log().ifValidationFails(LogDetail.ALL,true)
271272
.assertThat()
272-
.statusCode(is(HttpCode.UNAUTHORIZED.getStatus()));
273+
.statusCode(is(HttpCode.UNAUTHORIZED.getStatus()))
274+
.body("message", is(AUTH_ERROR_MSG));
273275
// fail to use no existent key
274276
given()
275277
.log().ifValidationFails(LogDetail.ALL,true)
@@ -284,7 +286,8 @@ public void test_key_usage() throws Exception {
284286
.then()
285287
.log().ifValidationFails(LogDetail.ALL,true)
286288
.assertThat()
287-
.statusCode(is(HttpCode.UNAUTHORIZED.getStatus()));
289+
.statusCode(is(HttpCode.UNAUTHORIZED.getStatus()))
290+
.body("message", is(AUTH_ERROR_MSG));
288291
}
289292

290293
@Order(6)

0 commit comments

Comments
 (0)