Skip to content

Commit

Permalink
fix(binding-http): fill security scheme when baseURI defined (#1244)
Browse files Browse the repository at this point in the history
* fix(binding-http): fill security scheme when baseURI defined

* refactor(binding-http/http-server): move warning near fillSecurityScheme

* docs(binding-http/http-server): remove outdated code comment
  • Loading branch information
relu91 authored Feb 26, 2024
1 parent ad76c37 commit 6b333d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ export default class HttpServer implements ProtocolServer {
debug(`HttpServer on port ${this.getPort()} exposes '${thing.title}' as unique '/${urlPath}'`);
this.things.set(urlPath, thing);

if (this.scheme === "http" && Object.keys(thing.securityDefinitions).length !== 0) {
warn(`HTTP Server will attempt to use your security schemes even if you are not using HTTPS.`);
}

this.fillSecurityScheme(thing);

if (this.baseUri !== undefined) {
const base: string = this.baseUri.concat("/", encodeURIComponent(urlPath));
info("HttpServer TD hrefs using baseUri " + this.baseUri);
Expand All @@ -288,14 +294,7 @@ export default class HttpServer implements ProtocolServer {
this.scheme + "://" + address + ":" + this.getPort() + "/" + encodeURIComponent(urlPath);

this.addEndpoint(thing, tdTemplate, base);
// media types
} // addresses

if (this.scheme === "http" && Object.keys(thing.securityDefinitions).length !== 0) {
warn(`HTTP Server will attempt to use your security schemes even if you are not using HTTPS.`);
}

this.fillSecurityScheme(thing);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/binding-http/test/http-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,21 @@ class HttpServerTest {
expect(testThing.securityDefinitions.bearer).not.eql(undefined);
}

@test async "should fill default security scheme even with baseURI defined"() {
const httpServer = new HttpServer({
port: port2,
baseUri: "https://example.com",
});
await httpServer.start(new Servient());
const testThing = new ExposedThing(new Servient());
testThing.title = "Test";
httpServer.expose(testThing);
await httpServer.stop();

expect(testThing.securityDefinitions.nosec).to.not.eql(undefined);
expect(testThing.securityDefinitions.nosec.scheme).to.be.eql("nosec");
}

@test async "should not accept an unsupported scheme"() {
debug("START SHOULD");
const httpServer = new HttpServer({
Expand Down

0 comments on commit 6b333d6

Please sign in to comment.