Skip to content

Commit

Permalink
Started work on authentication abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 14, 2024
1 parent 2986d08 commit 83b7872
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
library kiota_abstractions;

import 'dart:async';
import 'dart:collection';
import 'dart:math';
import 'dart:typed_data';

import 'package:kiota_abstractions/src/case_insensitive_map.dart';
import 'package:std_uritemplate/std_uritemplate.dart';

part 'src/authentication/allowed_hosts_validator.dart';
part 'src/authentication/access_token_provider.dart';
part 'src/authentication/anonymous_authentication_provider.dart';
part 'src/authentication/authentication_provider.dart';
part 'src/api_client_builder.dart';
part 'src/base_request_builder.dart';
part 'src/error_mappings.dart';
Expand Down
14 changes: 14 additions & 0 deletions lib/src/authentication/access_token_provider.dart
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;
}
41 changes: 41 additions & 0 deletions lib/src/authentication/allowed_hosts_validator.dart
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 lib/src/authentication/anonymous_authentication_provider.dart
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();
}
}
11 changes: 11 additions & 0 deletions lib/src/authentication/authentication_provider.dart
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,
]);
}

0 comments on commit 83b7872

Please sign in to comment.