Skip to content

Commit a817508

Browse files
authored
Merge pull request #49 from namib-project/combo
feat: add ComboSecurityScheme
2 parents a61666b + 97637e0 commit a817508

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023 The NAMIB Project Developers. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import 'package:curie/curie.dart';
8+
9+
import '../extensions/json_parser.dart';
10+
import 'security_scheme.dart';
11+
12+
const _schemeName = 'combo';
13+
14+
/// A combination of other security schemes identified by the Vocabulary Term
15+
/// `combo` (i.e., "scheme": "combo").
16+
class ComboSecurityScheme extends SecurityScheme {
17+
/// Constructor.
18+
ComboSecurityScheme({
19+
this.allOf,
20+
this.oneOf,
21+
super.description,
22+
super.descriptions,
23+
super.proxy,
24+
}) : super(_schemeName);
25+
26+
/// Creates a [ComboSecurityScheme] from a [json] object.
27+
ComboSecurityScheme.fromJson(
28+
Map<String, dynamic> json,
29+
PrefixMapping prefixMapping,
30+
Set<String> parsedFields,
31+
) : oneOf = json.parseArrayField<String>('oneOf', parsedFields),
32+
allOf = json.parseArrayField<String>('allOf', parsedFields),
33+
super(_schemeName) {
34+
parseSecurityJson(json, parsedFields, prefixMapping);
35+
}
36+
37+
/// Array of two or more strings identifying other named security scheme
38+
/// definitions, any one of which, when satisfied, will allow access.
39+
///
40+
/// Only one may be chosen for use.
41+
final List<String>? oneOf;
42+
43+
/// Array of two or more strings identifying other named security scheme
44+
/// definitions, all of which must be satisfied for access.
45+
final List<String>? allOf;
46+
}

lib/src/definitions/security/security_scheme.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'apikey_security_scheme.dart';
1212
import 'auto_security_scheme.dart';
1313
import 'basic_security_scheme.dart';
1414
import 'bearer_security_scheme.dart';
15+
import 'combo_security_scheme.dart';
1516
import 'digest_security_scheme.dart';
1617
import 'no_security_scheme.dart';
1718
import 'oauth2_security_scheme.dart';
@@ -84,6 +85,8 @@ abstract class SecurityScheme {
8485
return BasicSecurityScheme.fromJson(json, prefixMapping);
8586
case 'bearer':
8687
return BearerSecurityScheme.fromJson(json, prefixMapping);
88+
case 'combo':
89+
return ComboSecurityScheme.fromJson(json, prefixMapping, {});
8790
case 'nosec':
8891
return NoSecurityScheme.fromJson(json, prefixMapping);
8992
case 'psk':

test/core/consumed_thing_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:dart_wot/dart_wot.dart';
88
import 'package:dart_wot/src/definitions/security/apikey_security_scheme.dart';
99
import 'package:dart_wot/src/definitions/security/basic_security_scheme.dart';
1010
import 'package:dart_wot/src/definitions/security/bearer_security_scheme.dart';
11+
import 'package:dart_wot/src/definitions/security/combo_security_scheme.dart';
1112
import 'package:dart_wot/src/definitions/security/digest_security_scheme.dart';
1213
import 'package:dart_wot/src/definitions/security/no_security_scheme.dart';
1314
import 'package:dart_wot/src/definitions/security/oauth2_security_scheme.dart';
@@ -79,6 +80,14 @@ void main() {
7980
"refresh": "http://example.org",
8081
"scopes": "test",
8182
"flow": "client"
83+
},
84+
"combo_sc1": {
85+
"scheme": "combo",
86+
"allOf": ["digest_sc", "apikey_sc"]
87+
},
88+
"combo_sc2": {
89+
"scheme": "combo",
90+
"oneOf": ["oauth2_sc", "bearer_sc"]
8291
}
8392
},
8493
"security": "nosec_sc",
@@ -210,6 +219,22 @@ void main() {
210219
expect(oauth2Sc.token, 'http://example.org');
211220
expect(oauth2Sc.scopes, ['test']);
212221
expect(oauth2Sc.flow, 'client');
222+
223+
final comboSc1 = parsedTd.securityDefinitions['combo_sc1'];
224+
expect(comboSc1 is ComboSecurityScheme, true);
225+
expect(
226+
(comboSc1 as ComboSecurityScheme?)!.allOf,
227+
['digest_sc', 'apikey_sc'],
228+
);
229+
expect(comboSc1!.oneOf, null);
230+
231+
final comboSc2 = parsedTd.securityDefinitions['combo_sc2'];
232+
expect(comboSc2 is ComboSecurityScheme, true);
233+
expect(
234+
(comboSc2 as ComboSecurityScheme?)!.oneOf,
235+
['oauth2_sc', 'bearer_sc'],
236+
);
237+
expect(comboSc2!.allOf, null);
213238
});
214239
});
215240

0 commit comments

Comments
 (0)