-
-
Notifications
You must be signed in to change notification settings - Fork 444
Feature/persistent api keys #1342
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
15 commits
Select commit
Hold shift + click to select a range
f6230c8
cleanup and fix LOB issue
andreybavt 8f5035f
fixed LOB for H2 and Postgres
andreybavt 20a623c
persist API keys
andreybavt f6482e5
Merge branch 'main' into bug/fix-h2-db-lob
andreybavt d1b5d32
Merge branch 'bug/fix-h2-db-lob' into feature/persistent-api-keys
andreybavt 74db84e
added integration test
andreybavt 84f708f
test refactoring
andreybavt 6956701
sonar
andreybavt e6d1eb0
Merge branch 'main' into bug/fix-h2-db-lob
andreybavt 541c470
Merge branch 'bug/fix-h2-db-lob' into feature/persistent-api-keys
andreybavt 3c72b09
sonar cleanup
andreybavt 9fa926e
added api key deletion integration test
andreybavt d51b14c
cleanup after tests
andreybavt 3f5eb15
harmonize the use of HttpHeaders.AUTHORIZATION header, removed giskar…
andreybavt 653ea2b
Merge branch 'main' into feature/persistent-api-keys
andreybavt 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
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
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,38 @@ | ||
| package ai.giskard.domain; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.apache.commons.lang3.RandomStringUtils; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Entity(name = "api_keys") | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public class ApiKey extends AbstractAuditingEntity { | ||
|
|
||
| public static final String PREFIX = "gsk-"; | ||
| public static final int KEY_LENGTH = 32; | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| private UUID id; | ||
|
|
||
| @ManyToOne | ||
| private User user; | ||
|
|
||
| @Column(name = "api_key", length = KEY_LENGTH, unique = true, nullable = false) | ||
| private String key; | ||
|
|
||
| private String name; | ||
|
|
||
| public ApiKey(User user) { | ||
| this.user = user; | ||
| id = UUID.randomUUID(); | ||
| key = PREFIX + RandomStringUtils.randomAlphanumeric(KEY_LENGTH - PREFIX.length()); //NOSONAR | ||
| } | ||
|
|
||
| public static boolean doesStringLookLikeApiKey(String str) { | ||
| return str != null && str.length() == KEY_LENGTH && str.startsWith(PREFIX); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/ai/giskard/repository/ApiKeyRepository.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,11 @@ | ||
| package ai.giskard.repository; | ||
|
|
||
| import ai.giskard.domain.ApiKey; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Repository | ||
| public interface ApiKeyRepository extends MappableJpaRepository<ApiKey, UUID> { | ||
| void deleteApiKeyByIdAndUserLogin(UUID keyId, String login); | ||
| } |
50 changes: 50 additions & 0 deletions
50
backend/src/main/java/ai/giskard/security/ee/ApiKeyAuthFilter.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,50 @@ | ||
| package ai.giskard.security.ee; | ||
|
|
||
| import ai.giskard.domain.ApiKey; | ||
| import ai.giskard.domain.Role; | ||
| import ai.giskard.security.GiskardUser; | ||
| import ai.giskard.service.ApiKeyService; | ||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.ServletRequest; | ||
| import jakarta.servlet.ServletResponse; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.web.filter.GenericFilterBean; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Optional; | ||
|
|
||
| import static ai.giskard.security.AuthoritiesConstants.API; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class ApiKeyAuthFilter extends GenericFilterBean { | ||
| private final ApiKeyService apiKeyService; | ||
|
|
||
| public static Authentication getAuthentication(ApiKey apiKey) { | ||
| Collection<SimpleGrantedAuthority> authorities = new ArrayList<>(); | ||
| authorities.add(new SimpleGrantedAuthority(API)); | ||
| for (Role role : apiKey.getUser().getRoles()) { | ||
| authorities.add(new SimpleGrantedAuthority(role.getName())); | ||
| } | ||
|
|
||
| GiskardUser principal = new GiskardUser(apiKey.getUser().getId(), apiKey.getUser().getLogin(), "", authorities); | ||
|
|
||
| return new UsernamePasswordAuthenticationToken(principal, apiKey.getKey(), authorities); | ||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
| String apiKey = ((HttpServletRequest) request).getHeader(HttpHeaders.AUTHORIZATION).substring(7); | ||
| Optional<ApiKey> foundKey = apiKeyService.getKey(apiKey); | ||
| foundKey.ifPresent(key -> SecurityContextHolder.getContext().setAuthentication(getAuthentication(key))); | ||
| chain.doFilter(request, response); | ||
| } | ||
| } |
11 changes: 6 additions & 5 deletions
11
backend/src/main/java/ai/giskard/security/ee/GiskardAuthConfigurer.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,24 +1,25 @@ | ||
| package ai.giskard.security.ee; | ||
|
|
||
| import ai.giskard.security.ee.jwt.TokenProvider; | ||
| import ai.giskard.service.ApiKeyService; | ||
| import ai.giskard.service.ee.LicenseService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.config.annotation.SecurityConfigurerAdapter; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.web.DefaultSecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class GiskardAuthConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> { | ||
| private final LicenseService licenseService; | ||
| private final ApiKeyService apiKeyService; | ||
| private final TokenProvider tokenProvider; | ||
|
|
||
| public GiskardAuthConfigurer(LicenseService licenseService, TokenProvider tokenProvider) { | ||
| this.licenseService = licenseService; | ||
| this.tokenProvider = tokenProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public void configure(HttpSecurity http) { | ||
| GiskardAuthFilter customFilter = new GiskardAuthFilter(licenseService, tokenProvider); | ||
| GiskardAuthFilter customFilter = new GiskardAuthFilter(licenseService, apiKeyService, tokenProvider); | ||
| http.addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class); | ||
|
|
||
| } | ||
| } |
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
2 changes: 1 addition & 1 deletion
2
backend/src/main/java/ai/giskard/security/ee/jwt/JWTTokenType.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,5 +1,5 @@ | ||
| package ai.giskard.security.ee.jwt; | ||
|
|
||
| public enum JWTTokenType { | ||
| UI, API, INVITATION | ||
| UI, INVITATION | ||
| } |
Oops, something went wrong.
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.