Skip to content

Refactor/login #4583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions lib/src/command/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,62 @@ class LoginCommand extends PubCommand {
@override
Future<void> runProtected() async {
final credentials = oauth2.loadCredentials();
final userInfo = await _retrieveUserInfo();

if (credentials == null) {
final userInfo = await _retrieveUserInfo();
if (userInfo == null) {
log.warning(
'Could not retrieve your user-details.\n'
'You might have to run `$topLevelProgram pub logout` '
'to delete your credentials and try again.',
'Could not retrieve your user details.\n'
'Run `$topLevelProgram pub logout` to delete credentials and try '
'again.',
);
} else {
log.message('You are now logged in as $userInfo');
}
} else {
final userInfo = await _retrieveUserInfo();
if (userInfo == null) {
log.warning(
'Your credentials seems broken.\n'
'Run `$topLevelProgram pub logout` '
'to delete your credentials and try again.',
'Your credentials seem to be broken.\n'
'Run `$topLevelProgram pub logout` to delete credentials and try '
'again.',
);
} else {
log.message('You are already logged in as $userInfo');
}
log.warning(
'You are already logged in as $userInfo\n'
'Run `$topLevelProgram pub logout` to log out and try again.',
);
}
}

Future<_UserInfo?> _retrieveUserInfo() async {
return await oauth2.withClient((client) async {
final discovery = await oauth2.fetchOidcDiscoveryDocument();
final userInfoEndpoint = discovery['userinfo_endpoint'];

if (userInfoEndpoint is! String) {
log.fine('Bad discovery document. userinfo_endpoint not a String');
log.fine(
'Invalid discovery document: userinfo_endpoint is not a String',
);
return null;
}
final userInfoRequest = await client.get(Uri.parse(userInfoEndpoint));
if (userInfoRequest.statusCode != 200) return null;

final response = await client.get(Uri.parse(userInfoEndpoint));
if (response.statusCode != 200) {
log.fine('Failed to fetch user info: HTTP ${response.statusCode}');
return null;
}

try {
switch (json.decode(userInfoRequest.body)) {
case {'name': final String? name, 'email': final String email}:
return _UserInfo(name, email);
default:
log.fine(
'Bad response from $userInfoEndpoint: ${userInfoRequest.body}',
);
return null;
final decoded = json.decode(response.body);
if (decoded case {
'name': final String? name,
'email': final String email,
}) {
return _UserInfo(name, email);
} else {
log.fine('Unexpected user info format: ${response.body}');
return null;
}
} on FormatException catch (e) {
log.fine(
'Bad response from $userInfoEndpoint ($e): ${userInfoRequest.body}',
);
log.fine('Failed to decode user info: $e\nResponse: ${response.body}');
return null;
}
});
Expand Down