-
Notifications
You must be signed in to change notification settings - Fork 13
Add API key authentication to ApiServer #13
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
Open
thechibuikem
wants to merge
8
commits into
hyphae:vertex-4
Choose a base branch
from
thechibuikem:vertex-4
base: vertex-4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49239c5
chore: resolve merge conflicts from stash
thechibuikem 2f6cc2b
Add API key authentication to ApiServer with unit tests
thechibuikem a82fdd3
style: added new line at the end of file
thechibuikem 5bb304f
refactor: replace wildcard imports with explicit named imports
thechibuikem 6633506
config: added api-key to api-server at [config.json]
thechibuikem e9b625c
config: added [sample env]
thechibuikem 0b061ac
refactor(api-server): load API key from environment variable
thechibuikem 34bf4bb
fix: guard against NPE when DEV_INTERNAL_API_KEY unset
thechibuikem 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # ============================================================================== | ||
| # APIS_WEB APPLICATION CONFIGURATION (SAMPLE) | ||
| # Copy this file to '.env' and fill in your actual local configuration. | ||
| # ============================================================================== | ||
|
|
||
| DEV_INTERNAL_API_KEY="replace-with-a-generated-secret" # openssl rand -hex 32 |
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 |
|---|---|---|
|
|
@@ -10,3 +10,6 @@ build | |
| *.log.lck | ||
| *.err | ||
| *.err.lck | ||
| .env | ||
| .env.local | ||
| .env.*.local | ||
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,7 +1,6 @@ | ||
|
|
||
| package: | ||
| mvn package | ||
|
|
||
| clean: | ||
| mvn clean | ||
| rm -f *.log *.err | ||
| rm -f *.log *.err |
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
45 changes: 45 additions & 0 deletions
45
src/test/java/jp/co/sony/csl/dcoes/apis/tools/web/ApiServerAuthTest.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,45 @@ | ||
| package jp.co.sony.csl.dcoes.apis.tools.web; | ||
|
|
||
| import io.vertx.core.http.HttpServerRequest; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
|
|
||
| public class ApiServerAuthTest { | ||
|
|
||
| @Test | ||
| public void nullApiKey_returns500() { | ||
| HttpServerRequest req = mock(HttpServerRequest.class); | ||
| assertEquals(Integer.valueOf(500), ApiServer.checkAuth(req, null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyApiKey_returns500() { | ||
| HttpServerRequest req = mock(HttpServerRequest.class); | ||
| assertEquals(Integer.valueOf(500), ApiServer.checkAuth(req, "")); | ||
| } | ||
|
|
||
| @Test | ||
| public void missingHeader_returns401() { | ||
| HttpServerRequest req = mock(HttpServerRequest.class); | ||
| when(req.getHeader("X-API-Key")).thenReturn(null); | ||
| assertEquals(Integer.valueOf(401), ApiServer.checkAuth(req, "secret")); | ||
| } | ||
|
|
||
| @Test | ||
| public void wrongKey_returns401() { | ||
| HttpServerRequest req = mock(HttpServerRequest.class); | ||
| when(req.getHeader("X-API-Key")).thenReturn("wrong"); | ||
| assertEquals(Integer.valueOf(401), ApiServer.checkAuth(req, "secret")); | ||
| } | ||
|
|
||
| @Test | ||
| public void correctKey_returnsNull() { | ||
| HttpServerRequest req = mock(HttpServerRequest.class); | ||
| when(req.getHeader("X-API-Key")).thenReturn("secret"); | ||
| assertNull(ApiServer.checkAuth(req, "secret")); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the formatting changes to this file