-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from Health-Education-England/refactor/traine…
…eTisId refactor: inject trainee ID in to controllers
- Loading branch information
Showing
13 changed files
with
491 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/uk/nhs/hee/trainee/details/config/InterceptorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright 2024 Crown Copyright (Health Education England) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or | ||
* substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package uk.nhs.hee.trainee.details.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.context.annotation.RequestScope; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import uk.nhs.hee.trainee.details.dto.TraineeIdentity; | ||
import uk.nhs.hee.trainee.details.interceptor.TraineeIdentityInterceptor; | ||
|
||
/** | ||
* Configuration for interceptors. | ||
*/ | ||
@Configuration | ||
public class InterceptorConfiguration implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(traineeIdentityInterceptor()); | ||
} | ||
|
||
/** | ||
* Create an interceptor for creating a {@link TraineeIdentity} from a request. | ||
* | ||
* @return The trainee identity inteceptor. | ||
*/ | ||
@Bean | ||
public TraineeIdentityInterceptor traineeIdentityInterceptor() { | ||
return new TraineeIdentityInterceptor(traineeIdentity()); | ||
} | ||
|
||
/** | ||
* Create a {@link TraineeIdentity} for each request. | ||
* | ||
* @return The created trainee identity. | ||
*/ | ||
@Bean | ||
@RequestScope | ||
public TraineeIdentity traineeIdentity() { | ||
return new TraineeIdentity(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/uk/nhs/hee/trainee/details/dto/TraineeIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright 2024 Crown Copyright (Health Education England) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or | ||
* substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package uk.nhs.hee.trainee.details.dto; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* Trainee identity data for an authenticated user. | ||
*/ | ||
@Data | ||
public class TraineeIdentity { | ||
|
||
String traineeId; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/uk/nhs/hee/trainee/details/interceptor/TraineeIdentityInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright 2024 Crown Copyright (Health Education England) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or | ||
* substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package uk.nhs.hee.trainee.details.interceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import uk.nhs.hee.trainee.details.api.util.AuthTokenUtil; | ||
import uk.nhs.hee.trainee.details.dto.TraineeIdentity; | ||
|
||
/** | ||
* An interceptor for creating a {@link TraineeIdentity} from a request. | ||
*/ | ||
@Slf4j | ||
public class TraineeIdentityInterceptor implements HandlerInterceptor { | ||
|
||
private final TraineeIdentity traineeIdentity; | ||
|
||
public TraineeIdentityInterceptor(TraineeIdentity traineeIdentity) { | ||
this.traineeIdentity = traineeIdentity; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, | ||
Object handler) { | ||
String authToken = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
|
||
if (authToken != null) { | ||
try { | ||
String traineeId = AuthTokenUtil.getTraineeTisId(authToken); | ||
traineeIdentity.setTraineeId(traineeId); | ||
} catch (IOException e) { | ||
log.warn("Unable to extract trainee ID from authorization token.", e); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.