Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ private static void start(String[] args) {
if (commandLine.getCommand().getName().equals("private-jwk-to-public")) {
privateJwkToPublic(commandLine);
}
var router = getRouter(commandLine);
if (router instanceof DefaultRouter dr)
if (getRouter(commandLine) instanceof DefaultRouter dr)
dr.waitFor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.predic8.membrane.core.interceptor.apikey;

import com.predic8.membrane.annot.*;
import com.predic8.membrane.core.config.spring.*;
import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.interceptor.*;
import com.predic8.membrane.core.interceptor.apikey.extractors.*;
Expand All @@ -23,11 +22,10 @@
import org.slf4j.*;

import java.util.*;
import java.util.stream.*;

import static com.predic8.membrane.core.exceptions.ProblemDetails.*;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.*;
import static java.util.stream.Stream.*;

/**
Expand Down Expand Up @@ -85,10 +83,17 @@ public String getLongDescription() {
@Override
public void init() {
super.init();
// At the moment the beanFactory is only there when the Membrane configuration was read from XML

// Todo: Move logic into the registry
// The beanFactory is only there when the Membrane configuration was read from XML
if (router.getBeanFactory() != null) {
stores.addAll(router.getBeanFactory().getBeansOfType(ApiKeyStore.class).values());
}
// For YAML configuration
if (router.getRegistry() != null) {
this.stores.addAll(router.getRegistry().getBeans(ApiKeyStore.class));
}
Comment thread
predic8 marked this conversation as resolved.

stores.forEach(s -> s.init(router));

// Add the default extractor if none is configured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.predic8.membrane.core.security.ApiKeySecurityScheme.In.*;

/**
* @deprecated Set an expression like ${header['api']} on apiKey
* @description Extracts an API key from a specific HTTP request header. By default, the header name
* is <code>X-Api-Key</code>. If the header is present, its first value is returned as the API key.
* <p>
Expand Down
25 changes: 25 additions & 0 deletions distribution/examples/security/api-key/apikey-openapi/apis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# yaml-language-server: $schema=https://www.membrane-api.io/v7.0.5.json

components:
store:
apiKeyFileStore:
location: ./demo-keys.txt
---

api:
port: 2000
specs:
- openapi:
location: fruitshop-api-v2-openapi-3-security.yml
validateSecurity: true
flow:
- apiKey:
# API keys are validated in the OpenAPI validator with validateSecurity: true. See the OpenAPI document for details.
required: false
Comment thread
predic8 marked this conversation as resolved.
extractors:
- headerExtractor:
name: "X-Api-Key"
# The API key must be extracted before the OpenAPI validator is called.
# Normally, the OpenAPI is validated before the flow is executed. By explicitly
# setting the openapiValidator to this position, the OpenAPI is validated after the apiKey plugin.
- openapiValidator: {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

public class APIKeyWithOpenAPIExampleTest extends AbstractSampleMembraneStartStopTestcase {

public static final String API_KEY_HEADER = "X-Api-Key";

@Override
protected String getExampleDirName() {
return "security/api-key/apikey-openapi";
}

@Test
public void noApiKey() {
void noApiKey() {
when()
.get("http://localhost:2000/shop/v2/products")
.then().assertThat()
Expand All @@ -41,9 +43,9 @@ public void noApiKey() {
}

@Test
public void noScopesGet() {
void noScopesGet() {
given()
.header("X-Api-Key", "111")
.header(API_KEY_HEADER, "111")
.when()
.get("http://localhost:2000/shop/v2/products")
.then().assertThat()
Expand All @@ -53,9 +55,9 @@ public void noScopesGet() {
}

@Test
public void noScopesPost() {
void noScopesPost() {
given()
.header("X-Api-Key", "111")
.header(API_KEY_HEADER, "111")
.contentType(APPLICATION_JSON)
.body("""
{
Expand All @@ -78,9 +80,9 @@ public void noScopesPost() {
}

@Test
public void writeScopes() {
void writeScopes() {
given()
.headers("X-Api-Key", "222")
.headers(API_KEY_HEADER, "222")
.contentType(APPLICATION_JSON)
.body("{\"name\": \"Mango\", \"price\": 2.79}")
.when()
Expand Down
Loading