Skip to content

Commit

Permalink
Removed unnecessary code and refactored the remaining AuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
jamcunha committed Aug 18, 2023
1 parent a44e14c commit 3e54dda
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,7 @@ class AuthService(
}

fun refreshAccessToken(refreshToken: String): String {
val jwt =
try {
jwtDecoder.decode(refreshToken)
} catch (e: Exception) {
throw InvalidBearerTokenException(ErrorMessages.invalidRefreshToken)
}
if (jwt.expiresAt?.isBefore(Instant.now()) != false) {
throw InvalidBearerTokenException(ErrorMessages.expiredRefreshToken)
}
val jwt = jwtDecoder.decode(refreshToken)
val account = accountService.getAccountByEmail(jwt.subject)
return generateAccessToken(account)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ object ErrorMessages {

const val invalidCredentials = "invalid credentials"

const val invalidRefreshToken = "invalid refresh token"
const val invalidToken = "invalid token"

const val expiredRefreshToken = "refresh token has expired"

const val invalidToken = "invalid password recovery token"

const val expiredToken = "password recovery token has expired"
const val expiredToken = "token has expired"

const val noGenerations = "no generations created yet"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.model.CustomWebsite
import pt.up.fe.ni.website.backend.model.constants.AccountConstants
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.service.ErrorMessages
import pt.up.fe.ni.website.backend.utils.TestUtils
import pt.up.fe.ni.website.backend.utils.ValidationTester
import pt.up.fe.ni.website.backend.utils.annotations.ControllerTest
Expand Down Expand Up @@ -170,7 +169,7 @@ class AuthControllerTest @Autowired constructor(
)
.andExpectAll(
status().isUnauthorized,
jsonPath("$.errors[0].message").value("invalid refresh token")
jsonPath("$.errors[0].message").value("invalid token")
)
.andDocumentErrorResponse(documentation, hasRequestPayload = true)
}
Expand Down Expand Up @@ -344,7 +343,7 @@ class AuthControllerTest @Autowired constructor(
).andExpectAll(
status().isUnauthorized(),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value("invalid password recovery token")
jsonPath("$.errors[0].message").value("invalid token")
).andDocumentCustomRequestSchemaErrorResponse(
documentation,
passwordRecoveryPayload,
Expand Down Expand Up @@ -394,8 +393,6 @@ class AuthControllerTest @Autowired constructor(
.expiresAt(Instant.now().minus(1, ChronoUnit.DAYS))
.subject(decoded.subject)
.claim("scope", decoded.claims["scope"])
claimsBuilder.claim("exp", Instant.now().minus(1, ChronoUnit.DAYS).epochSecond)
claimsBuilder.claim("iat", Instant.now().minus(2, ChronoUnit.DAYS).epochSecond)

val newToken = jwtEncoder.encode(JwtEncoderParameters.from(claimsBuilder.build())).tokenValue

Expand All @@ -412,7 +409,7 @@ class AuthControllerTest @Autowired constructor(
).andExpectAll(
status().isUnauthorized(),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value(ErrorMessages.expiredToken)
jsonPath("$.errors[0].message").value("token has expired")
)
}
}
Expand Down Expand Up @@ -462,7 +459,7 @@ class AuthControllerTest @Autowired constructor(
).andExpectAll(
status().isUnauthorized(),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value(ErrorMessages.invalidToken)
jsonPath("$.errors[0].message").value("invalid token")
)
}
}
Expand Down Expand Up @@ -512,7 +509,7 @@ class AuthControllerTest @Autowired constructor(
).andExpectAll(
status().isUnauthorized(),
jsonPath("$.errors.length()").value(1),
jsonPath("$.errors[0].message").value(ErrorMessages.invalidToken)
jsonPath("$.errors[0].message").value("invalid token")
).andDocumentCustomRequestSchemaErrorResponse(
documentation,
passwordRecoveryPayload,
Expand Down

0 comments on commit 3e54dda

Please sign in to comment.