Skip to content

Commit 3b6b4dd

Browse files
committed
feat(core): add support for AutoSecurityScheme #1421
1 parent e5bbddc commit 3b6b4dd

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

packages/core/src/thing-description.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ export interface NullSchema extends BaseSchema {
134134
type: "null";
135135
}
136136

137-
// TODO AutoSecurityScheme
138137
export type SecurityType =
139138
| NoSecurityScheme
139+
| AutoSecurityScheme
140140
| BasicSecurityScheme
141141
| DigestSecurityScheme
142142
| BearerSecurityScheme
@@ -193,6 +193,9 @@ export interface AllOfSecurityScheme extends SecurityScheme {
193193
}
194194
export type ComboSecurityScheme = AllOfSecurityScheme | OneOfSecurityScheme;
195195

196+
export interface AutoSecurityScheme extends SecurityScheme, TDT.AutoSecurityScheme {
197+
scheme: "auto";
198+
}
196199
/** Implements the Thing Property description */
197200
export abstract class ThingProperty extends BaseSchema {
198201
// writable: boolean;

packages/core/test/ClientTest.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ import { Subscription } from "rxjs/Subscription";
2727

2828
import Servient from "../src/servient";
2929
import ConsumedThing from "../src/consumed-thing";
30-
import { AllOfSecurityScheme, Form, OneOfSecurityScheme, SecurityScheme } from "../src/thing-description";
30+
import {
31+
AllOfSecurityScheme,
32+
AutoSecurityScheme,
33+
Form,
34+
OneOfSecurityScheme,
35+
SecurityScheme,
36+
} from "../src/thing-description";
3137
import { ProtocolClient, ProtocolClientFactory } from "../src/protocol-interfaces";
3238
import { Content } from "../src/content";
3339
import { ContentSerdes } from "../src/content-serdes";
@@ -1045,4 +1051,27 @@ class WoTClientTest {
10451051
ct.ensureClientSecurity(pc, form);
10461052
}, /Combo SecurityScheme 'combo_without_oneOf_and_without_allof' is invalid/);
10471053
}
1054+
1055+
@test "with auto security scheme selection"() {
1056+
const ct = new ConsumedThing(WoTClientTest.servient);
1057+
ct.securityDefinitions = {
1058+
auto_sc: {
1059+
scheme: "auto",
1060+
},
1061+
};
1062+
ct.security = ["auto_sc"];
1063+
const pc = new TestProtocolClient();
1064+
const form: Form = {
1065+
href: "https://example.com/",
1066+
};
1067+
ct.ensureClientSecurity(pc, form);
1068+
expect(pc.securitySchemes.length).equals(1);
1069+
expect(pc.securitySchemes[0].scheme).equal("auto");
1070+
1071+
if (pc.securitySchemes[0].scheme === "auto") {
1072+
// casting to AutoSecurityScheme should not bother the IDE.
1073+
const autoScheme = pc.securitySchemes[0] as AutoSecurityScheme;
1074+
expect(autoScheme.scheme).to.equal("auto");
1075+
}
1076+
}
10481077
}

0 commit comments

Comments
 (0)