-
Notifications
You must be signed in to change notification settings - Fork 0
Finish functionality to generate API key and authenticate with it #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
backend/src/main/java/net/hackyourfuture/coursehub/data/UserAccountEntity.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| package net.hackyourfuture.coursehub.data; | ||
|
|
||
| public record UserAccountEntity(Integer userId, String emailAddress, String passwordHash, Role role) {} | ||
| public record UserAccountEntity(Integer userId, String emailAddress, String passwordHash, Role role, String apiKey) {} |
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
backend/src/main/java/net/hackyourfuture/coursehub/security/ApiKeyAuthenticationFilter.java
This file contains hidden or 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,59 @@ | ||
| package net.hackyourfuture.coursehub.security; | ||
|
|
||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import net.hackyourfuture.coursehub.data.AuthenticatedUser; | ||
| import net.hackyourfuture.coursehub.service.UserAuthenticationService; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class ApiKeyAuthenticationFilter extends OncePerRequestFilter { | ||
|
|
||
| private final UserAuthenticationService userAuthenticationService; | ||
| private static final String AUTHORIZATION_HEADER_KEY = "Authorization"; | ||
|
|
||
| public ApiKeyAuthenticationFilter(UserAuthenticationService userAuthenticationService) { | ||
| this.userAuthenticationService = userAuthenticationService; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doFilterInternal( | ||
| HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain filterChain) throws ServletException, IOException { | ||
|
|
||
| // Skip API key authentication if already authenticated | ||
| if (SecurityContextHolder.getContext().getAuthentication() != null && | ||
| SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) { | ||
Breus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| filterChain.doFilter(request, response); | ||
| return; | ||
| } | ||
|
|
||
| String apiKey = request.getHeader(AUTHORIZATION_HEADER_KEY); | ||
|
|
||
| if (apiKey != null && !apiKey.isEmpty()) { | ||
| // Look up user by API key | ||
| AuthenticatedUser user = userAuthenticationService.findUserByApiKey(apiKey); | ||
|
|
||
| if (user != null) { | ||
| // Create an authentication token | ||
| UsernamePasswordAuthenticationToken authentication = | ||
| new UsernamePasswordAuthenticationToken( | ||
| user, | ||
| null, // No credentials needed as we authenticated via API key | ||
| user.getAuthorities() | ||
| ); | ||
|
|
||
| // Set authentication in context | ||
| SecurityContextHolder.getContext().setAuthentication(authentication); | ||
| } | ||
| } | ||
|
|
||
| filterChain.doFilter(request, response); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/net/hackyourfuture/coursehub/web/model/ApiKeyResponse.java
This file contains hidden or 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,8 @@ | ||
| package net.hackyourfuture.coursehub.web.model; | ||
|
|
||
| /** | ||
| * Response object for API key generation. | ||
| * @param apiKey The generated API key | ||
| */ | ||
| public record ApiKeyResponse(String apiKey) { | ||
| } |
4 changes: 4 additions & 0 deletions
4
backend/src/main/resources/db/migration/V3__add_api_key_column.sql
This file contains hidden or 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,4 @@ | ||
| -- Add API key column to user_account table | ||
| ALTER TABLE user_account ADD COLUMN api_key VARCHAR(100) NULL; | ||
| CREATE UNIQUE INDEX idx_api_key_unique ON user_account (api_key) WHERE api_key IS NOT NULL; | ||
|
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.