-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started work on authentication abstractions
- Loading branch information
1 parent
2986d08
commit 83b7872
Showing
5 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
part of '../../kiota_abstractions.dart'; | ||
|
||
/// Defines a contract for obtaining an access token for a given url. | ||
abstract class AccessTokenProvider { | ||
/// Gets an access token for the given url and additional authentication | ||
/// context. | ||
Future<String> getAuthorizationToken( | ||
Uri uri, [ | ||
Map<String, Object>? additionalAuthenticationContext, | ||
]); | ||
|
||
/// The [AllowedHostsValidator] to use when validating the host of the url. | ||
AllowedHostsValidator get allowedHostsValidator; | ||
} |
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,41 @@ | ||
part of '../../kiota_abstractions.dart'; | ||
|
||
/// Validator for handling allowed hosts for authentication. | ||
class AllowedHostsValidator { | ||
/// Initializes a new instance of the [AllowedHostsValidator] class. | ||
AllowedHostsValidator([Iterable<String>? validHosts]) { | ||
validHosts ??= <String>[]; | ||
_validateHosts(validHosts); | ||
_allowedHosts = HashSet( | ||
equals: (a, b) => a.toUpperCase() == b.toUpperCase(), | ||
hashCode: (o) => o.toUpperCase().hashCode, | ||
)..addAll(validHosts); | ||
} | ||
|
||
late final Set<String> _allowedHosts; | ||
|
||
/// Gets the allowed hosts. | ||
Iterable<String> get allowedHosts => _allowedHosts; | ||
|
||
/// Sets the allowed hosts. | ||
set allowedHosts(Iterable<String> value) { | ||
_validateHosts(value); | ||
_allowedHosts = HashSet( | ||
equals: (a, b) => a.toUpperCase() == b.toUpperCase(), | ||
hashCode: (o) => o.toUpperCase().hashCode, | ||
)..addAll(value); | ||
} | ||
|
||
/// Validates that the given [uri] is valid. | ||
bool isUrlHostValid(Uri uri) { | ||
return _allowedHosts.isEmpty || _allowedHosts.contains(uri.host); | ||
} | ||
|
||
static void _validateHosts(Iterable<String> hostsToValidate) { | ||
if (hostsToValidate | ||
.map((e) => e.toLowerCase()) | ||
.any((x) => x.startsWith('http://') || x.startsWith('https://'))) { | ||
throw ArgumentError('Host should not contain http or https prefix'); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
lib/src/authentication/anonymous_authentication_provider.dart
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,9 @@ | ||
part of '../../kiota_abstractions.dart'; | ||
|
||
/// This authentication provider does not perform any authentication. | ||
class AnonymousAuthenticationProvider implements AuthenticationProvider { | ||
@override | ||
Future<void> authenticateRequest(RequestInformation request, [Map<String, Object>? additionalAuthenticationContext]) { | ||
return Future<void>.value(); | ||
} | ||
} |
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,11 @@ | ||
part of '../../kiota_abstractions.dart'; | ||
|
||
/// Authenticates the application request. | ||
abstract class AuthenticationProvider { | ||
/// Authenticates the give [request] with the given | ||
/// [additionalAuthenticationContext]. | ||
Future<void> authenticateRequest( | ||
RequestInformation request, [ | ||
Map<String, Object>? additionalAuthenticationContext, | ||
]); | ||
} |