Skip to content

Commit

Permalink
Merge branch 'nullsafety' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Francessco121 committed Mar 20, 2021
2 parents 3bb35da + 16848e6 commit 2c8aaa5
Show file tree
Hide file tree
Showing 32 changed files with 371 additions and 339 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.4.0-nullsafety.0
- Migrated to null safety. (#21, thanks [nyarian](https://github.com/nyarian)!)
- **Note:** Many properties and returns are now nullable that seem like they shouldn't be. This is because msal.js is written in a way that makes it impossible to guarantee null safety.
- Updated minimum SDK version to `2.12.0-259.9.beta`.
- Added `LogLevel.unknown` to represent msal.js log levels that don't have an enum value counterpart.

## v1.3.0
- Added `UserAgentApplication.ssoSilent`.
- Added `UserAgentApplication.setLogger`.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void _loggerCallback(LogLevel level, String message, bool containsPii) {
print('[$level] $message');
}
void _authCallback(AuthException error, [AuthResponse response]) {
void _authCallback(AuthException? error, [AuthResponse? response]) {
// ...
}
```
Expand All @@ -91,7 +91,8 @@ Often, msal.js will make API additions and changes in their "patch" releases. Th

| Dart (msal_js) | JavaScript (msal.js) |
| ------------------ | -------------------- |
| **1.3.0** | 1.3.2 |
| **1.4.0-nullsafety.0**| 1.4.x |
| **1.3.0** | 1.3.2, 1.4.x |
| **1.2.0** | 1.2.0 |
| **1.1.0** | 1.1.0 |
| **1.0.0** | 1.0.0 |
Expand Down
2 changes: 1 addition & 1 deletion example/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final userAgentApplication = UserAgentApplication(config);
// Register a callback for redirect flows (optional)
userAgentApplication.handleRedirectCallback(authCallback);
void authCallback(AuthException error, [AuthResponse response]) {
void authCallback(AuthException? error, [AuthResponse? response]) {
// handle redirect response or error
}
```
Expand Down
27 changes: 12 additions & 15 deletions example/flutter_web_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:msal_js/msal_js.dart';
// **Setup your directory settings here:**
const String clientId = '';
const String authority = '';
const List<String> scopes = [];
void main() {
Expand All @@ -34,9 +33,7 @@ void main() {
// Create an MSAL UserAgentApplication
final userAgentApplication = UserAgentApplication(
Configuration()
..auth = (AuthOptions()
..clientId = clientId
..authority = authority)
..auth = (AuthOptions()..clientId = clientId)
..system = (SystemOptions()..logger = logger),
);
Expand All @@ -59,13 +56,13 @@ void _loggerCallback(LogLevel level, String message, bool containsPii) {
print('MSAL: [$level] $message');
}
void _redirectCallback(AuthException error, [AuthResponse response]) {
void _redirectCallback(AuthException? error, [AuthResponse? response]) {
if (error != null) {
// Redirect login failed
print('MSAL: ${error.errorCode}:${error.errorMessage}');
} else {
// Redirect login succeeded
print('Redirect login successful. name: ${response.account.name}');
print('Redirect login successful. name: ${response!.account!.name}');
}
}
Expand All @@ -75,7 +72,7 @@ void _redirectCallback(AuthException error, [AuthResponse response]) {
class MyApp extends StatelessWidget {
final UserAgentApplication userAgentApplication;
MyApp({@required this.userAgentApplication});
MyApp({required this.userAgentApplication});
@override
Widget build(BuildContext context) {
Expand All @@ -96,16 +93,16 @@ class MyHomePage extends StatefulWidget {
final UserAgentApplication userAgentApplication;
MyHomePage({
@required this.userAgentApplication,
Key key,
required this.userAgentApplication,
Key? key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Account _account;
Account? _account;
@override
void initState() {
Expand All @@ -132,7 +129,7 @@ class _MyHomePageState extends State<MyHomePage> {
_account = response.account;
});
print('Popup login successful. name: ${response.account.name}');
print('Popup login successful. name: ${_account!.name}');
} on AuthException catch (ex) {
print('MSAL: ${ex.errorCode}:${ex.errorMessage}');
}
Expand All @@ -154,18 +151,18 @@ class _MyHomePageState extends State<MyHomePage> {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (_account == null) ...[
RaisedButton(
ElevatedButton(
child: Text('Login Redirect'),
onPressed: _loginRedirect,
),
RaisedButton(
ElevatedButton(
child: Text('Login Popup'),
onPressed: _loginPopup,
),
],
if (_account != null) ...[
Text('Signed in as ${_account.name}'),
RaisedButton(
Text('Signed in as ${_account!.name}'),
ElevatedButton(
child: Text('Logout'),
onPressed: _logout,
),
Expand Down
14 changes: 7 additions & 7 deletions lib/src/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ part of '../msal_js.dart';
/// An authenticated user account.
class Account {
/// The ID of this user's account.
String get accountIdentifier => _jsObject.accountIdentifier;
String? get accountIdentifier => _jsObject.accountIdentifier;

/// The account ID of the home account for the user.
/// This uniquely identifies the user across AAD tenants.
String get homeAccountIdentifier => _jsObject.homeAccountIdentifier;
String? get homeAccountIdentifier => _jsObject.homeAccountIdentifier;

/// The account's 'preferred' username (`idToken.preferred_username`).
String get userName => _jsObject.userName;
String? get userName => _jsObject.userName;

/// The account's name (`idToken.name`).
String get name => _jsObject.name;
String? get name => _jsObject.name;

/// The ID token associated with this account.
///
Expand All @@ -22,14 +22,14 @@ class Account {
dynamic get idToken => _jsObject.idToken;

/// A map of all claims in the [idToken].
Map<String, dynamic> get idTokenClaims =>
Map<String, dynamic>? get idTokenClaims =>
jsDecodeMap<dynamic>(_jsObject.idTokenClaims);

/// The account's session identifier (`idToken.sid`).
String get sid => _jsObject.sid;
String? get sid => _jsObject.sid;

/// The authority that issued the token (`idToken.issuer`).
String get environment => _jsObject.environment;
String? get environment => _jsObject.environment;

final interop.Account _jsObject;

Expand Down
66 changes: 34 additions & 32 deletions lib/src/auth_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ part of '../msal_js.dart';
///
/// Note: In msal.js, this is called `AuthenticationParameters`.
class AuthRequest {
List<String> get scopes => jsDecodeList<String>(_jsObject.scopes);
List<String>? get scopes => jsDecodeList<String>(_jsObject.scopes);

/// Sets the scopes, which the user will need to consent to, for gaining
/// permission to access specific parts of a resource protected by scopes.
///
/// Also see [extraScopesToConsent] if you would like to ask for consent
/// for scopes in multiple resources.
set scopes(List<String> value) => _jsObject.scopes = jsEncode(value);
set scopes(List<String>? value) => _jsObject.scopes = jsEncode(value);

List<String> get extraScopesToConsent =>
List<String>? get extraScopesToConsent =>
jsDecodeList<String>(_jsObject.extraScopesToConsent);

/// Sets the additional scopes the user must consent to, usually for a different
Expand All @@ -22,59 +22,61 @@ class AuthRequest {
/// When making an auth request, [scopes] usually can only contain scopes for
/// a single resource. This property makes it possible to ask the user to
/// pre-consent to scopes for a different resource.
set extraScopesToConsent(List<String> value) =>
set extraScopesToConsent(List<String>? value) =>
_jsObject.extraScopesToConsent = jsEncode(value);

String get prompt => _jsObject.prompt;
String? get prompt => _jsObject.prompt;

/// Set to control the interactivity with the user during authentication.
///
/// See https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-prompt-behavior
/// for more information.
set prompt(String value) => _jsObject.prompt = value;
set prompt(String? value) => _jsObject.prompt = value;

Map<String, String> get extraQueryParameters =>
Map<String, String>? get extraQueryParameters =>
jsDecodeMap<String>(_jsObject.extraQueryParameters);

/// Sets additional query parameters to attach to the HTTP request.
set extraQueryParameters(Map<String, String> value) =>
set extraQueryParameters(Map<String, String>? value) =>
_jsObject.extraQueryParameters = jsEncode(value);

String get claimsRequest => _jsObject.claimsRequest;
String? get claimsRequest => _jsObject.claimsRequest;

/// Sets additional claims to be requested.
set claimsRequest(String value) => _jsObject.claimsRequest = value;
set claimsRequest(String? value) => _jsObject.claimsRequest = value;

String get authority => _jsObject.authority;
String? get authority => _jsObject.authority;

/// Set to override the authority used for this request.
///
/// May only be used when using `acquire` methods.
///
/// If not set, the authority configured with the [UserAgentApplication] will be used.
set authority(String value) => _jsObject.authority = value;
set authority(String? value) => _jsObject.authority = value;

String get state => _jsObject.state;
String? get state => _jsObject.state;

/// Sets the OAuth 2.0 state which will be returned in the auth response.
///
/// This is used to prevent cross-site request forgery attacks.
///
/// If not set, msal.js will use a randomly generated unique string.
set state(String value) => _jsObject.state = value;
set state(String? value) => _jsObject.state = value;

String get correlationId => _jsObject.correlationId;
String? get correlationId => _jsObject.correlationId;

/// Sets a unique identifier that can be used to map requests and responses.
set correlationId(String value) => _jsObject.correlationId = value;
set correlationId(String? value) => _jsObject.correlationId = value;

Account get account => Account._fromJsObject(_jsObject.account);
Account? get account => _jsObject.account == null
? null
: Account._fromJsObject(_jsObject.account!);

/// Set to provide a hint to the authorization endpoint about the
/// specific user to get a token for.
set account(Account value) => _jsObject.account = value._jsObject;
set account(Account? value) => _jsObject.account = value?._jsObject;

String get sid => _jsObject.sid;
String? get sid => _jsObject.sid;

/// Sets the session ID claim which allows the application to identify the
/// user's Azure AD session independent of the user's account name or username
Expand All @@ -84,32 +86,32 @@ class AuthRequest {
///
/// See https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-sso
/// for more information.
set sid(String value) => _jsObject.sid = value;
set sid(String? value) => _jsObject.sid = value;

String get loginHint => _jsObject.loginHint;
String? get loginHint => _jsObject.loginHint;

/// Sets a hint to bypass the account selection prompt. Should be set to
/// the ID token's `preferred_username` claim.
///
/// See https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-sso
/// for more information.
set loginHint(String value) => _jsObject.loginHint = value;
set loginHint(String? value) => _jsObject.loginHint = value;

bool get forceRefresh => _jsObject.forceRefresh;
bool? get forceRefresh => _jsObject.forceRefresh;

/// Sets whether the cache should be skipped and a token request to the
/// authorization server should be made.
set forceRefresh(bool value) => _jsObject.forceRefresh = value;
set forceRefresh(bool? value) => _jsObject.forceRefresh = value;

String get redirectUri => _jsObject.redirectUri;
String? get redirectUri => _jsObject.redirectUri;

/// Set to override the redirect URI used when retrieving a token.
///
/// This can be used for example to redirect silent token requests to
/// a blank page instead of a page which normally loads the entire application.
set redirectUri(String value) => _jsObject.redirectUri = value;
set redirectUri(String? value) => _jsObject.redirectUri = value;

String get redirectStartPage => _jsObject.redirectStartPage;
String? get redirectStartPage => _jsObject.redirectStartPage;

/// Sets the page that should be returned to after `loginRedirect` or
/// `acquireTokenRedirect`.
Expand All @@ -118,9 +120,9 @@ class AuthRequest {
/// will default to the page that initiates the request. When the
/// `navigateToLoginRequestUrl` config option is set to false this parameter
/// will be ignored.
set redirectStartPage(String value) => _jsObject.redirectStartPage = value;
set redirectStartPage(String? value) => _jsObject.redirectStartPage = value;

interop.RedirectNavigateCallback get onRedirectNavigate =>
interop.RedirectNavigateCallback? get onRedirectNavigate =>
_jsObject.onRedirectNavigate;

/// Sets the callback that will be invoked before a redirect flow (e.g.
Expand All @@ -131,18 +133,18 @@ class AuthRequest {
///
/// Return false to prevent navigation. Return true or null to allow
/// navigation.
set onRedirectNavigate(interop.RedirectNavigateCallback value) =>
set onRedirectNavigate(interop.RedirectNavigateCallback? value) =>
_jsObject.onRedirectNavigate = value == null ? null : allowInterop(value);

String get authorityMetadata => _jsObject.authorityMetadata;
String? get authorityMetadata => _jsObject.authorityMetadata;

/// Sets the OpenID configuration metadata for the configured authority.
///
/// Must be passed as a JSON string.
///
/// See https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-core/docs/performance.md#bypass-authority-metadata-resolution
/// for more information.
set authorityMetadata(String value) => _jsObject.authorityMetadata = value;
set authorityMetadata(String? value) => _jsObject.authorityMetadata = value;

final _jsObject = interop.AuthenticationParameters();
}
20 changes: 11 additions & 9 deletions lib/src/auth_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@ part of '../msal_js.dart';
/// A response from an auth request.
class AuthResponse {
/// The unique user ID.
String get uniqueId => _jsObject.uniqueId;
String? get uniqueId => _jsObject.uniqueId;

/// The ID of the tenant where the user was found.
String get tenantId => _jsObject.tenantId;
String? get tenantId => _jsObject.tenantId;

/// The type of token that was requested.
///
/// Either `id_token` or `access_token`.
String get tokenType => _jsObject.tokenType;
String? get tokenType => _jsObject.tokenType;

/// The ID token of the user.
dynamic get idToken => _jsObject.idToken;
dynamic? get idToken => _jsObject.idToken;

/// The granted access token.
String get accessToken => _jsObject.accessToken;
String? get accessToken => _jsObject.accessToken;

/// The scopes that were issued for the token.
List<String> get scopes => jsDecodeList<String>(_jsObject.scopes);
List<String>? get scopes => jsDecodeList<String>(_jsObject.scopes);

/// The date/time when the token will expire.
DateTime get expiresOn => _jsObject.expiresOn;
DateTime? get expiresOn => _jsObject.expiresOn;

/// The authenticated account.
Account get account => Account._fromJsObject(_jsObject.account);
Account? get account => _jsObject.account == null
? null
: Account._fromJsObject(_jsObject.account!);

/// The OAuth 2.0 state that was specified in the auth request.
///
/// See [AuthRequest.state] for more information.
String get accountState => _jsObject.accountState;
String? get accountState => _jsObject.accountState;

final interop.AuthResponse _jsObject;

Expand Down
Loading

0 comments on commit 2c8aaa5

Please sign in to comment.