-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
7 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
68 changes: 68 additions & 0 deletions
68
proteus-openapi/src/main/java/io/sinistral/proteus/openapi/wrappers/HeaderApiKeyWrapper.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,68 @@ | ||
package io.sinistral.proteus.openapi.wrappers; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import com.google.inject.name.Named; | ||
import io.sinistral.proteus.server.exceptions.ServerException; | ||
import io.undertow.server.HandlerWrapper; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.util.AttachmentKey; | ||
import io.undertow.util.HttpString; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.core.Response; | ||
import java.util.Optional; | ||
|
||
@Singleton | ||
public class HeaderApiKeyWrapper implements HandlerWrapper | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(HeaderApiKeyWrapper.class.getName()); | ||
|
||
public static final AttachmentKey<Throwable> THROWABLE = AttachmentKey.create(Throwable.class); | ||
|
||
@Inject | ||
@Named("openapi.securitySchemes.ApiKeyAuth.name") | ||
protected static String AUTH_KEY_NAME; | ||
|
||
@Inject(optional = true) | ||
@Named("security.apiKey") | ||
protected static String API_KEY; | ||
|
||
private final HttpString API_KEY_HEADER; | ||
|
||
public HeaderApiKeyWrapper() | ||
{ | ||
API_KEY_HEADER = new HttpString(AUTH_KEY_NAME); | ||
} | ||
|
||
@Override | ||
public HttpHandler wrap(HttpHandler handler) | ||
{ | ||
return exchange -> { | ||
|
||
if(API_KEY == null) | ||
{ | ||
handler.handleRequest(exchange); | ||
return; | ||
} | ||
|
||
Optional<String> keyValue = Optional.ofNullable(exchange.getRequestHeaders().getFirst(API_KEY_HEADER)); | ||
|
||
if(!keyValue.isPresent() || !keyValue.get().equals(API_KEY)) | ||
{ | ||
|
||
logger.error("Missing security credentials"); | ||
exchange.putAttachment(THROWABLE, new ServerException("Unauthorized access", Response.Status.UNAUTHORIZED)); | ||
throw new ServerException("Unauthorized access", Response.Status.UNAUTHORIZED); | ||
|
||
} | ||
|
||
handler.handleRequest(exchange); | ||
|
||
|
||
|
||
}; | ||
} | ||
|
||
} |
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