-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shorebird_cli): use Google OAuth 2.0 (#223)
- Loading branch information
Showing
35 changed files
with
375 additions
and
227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,111 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:googleapis_auth/auth_io.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:path/path.dart' as p; | ||
import 'package:shorebird_cli/src/auth/session.dart'; | ||
import 'package:shorebird_cli/src/config/config.dart'; | ||
|
||
export 'package:googleapis_auth/googleapis_auth.dart' show AccessCredentials; | ||
|
||
final _clientId = ClientId( | ||
/// Shorebird CLI's OAuth 2.0 identifier. | ||
'523302233293-eia5antm0tgvek240t46orctktiabrek.apps.googleusercontent.com', | ||
|
||
/// Shorebird CLI's OAuth 2.0 secret. | ||
/// | ||
/// This isn't actually meant to be kept secret. | ||
/// There is no way to properly secure a secret for installed/console applications. | ||
/// Fortunately the OAuth2 flow used in this case assumes that the app cannot | ||
/// keep secrets so this particular secret DOES NOT need to be kept secret. | ||
/// You should however make sure not to re-use the same secret | ||
/// anywhere secrecy is required. | ||
/// | ||
/// For more info see: https://developers.google.com/identity/protocols/oauth2/native-app | ||
'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', | ||
); | ||
final _scopes = ['openid', 'https://www.googleapis.com/auth/userinfo.email']; | ||
|
||
typedef ObtainAccessCredentials = Future<AccessCredentials> Function( | ||
ClientId clientId, | ||
List<String> scopes, | ||
http.Client client, | ||
void Function(String) userPrompt, | ||
); | ||
|
||
class Auth { | ||
Auth() { | ||
_loadSession(); | ||
Auth({ | ||
http.Client? httpClient, | ||
ObtainAccessCredentials? obtainAccessCredentials, | ||
}) : _httpClient = httpClient ?? http.Client(), | ||
_obtainAccessCredentials = | ||
obtainAccessCredentials ?? obtainAccessCredentialsViaUserConsent { | ||
_loadCredentials(); | ||
} | ||
|
||
static const _sessionFileName = 'shorebird-session.json'; | ||
final sessionFilePath = p.join(shorebirdConfigDir, _sessionFileName); | ||
static const _credentialsFileName = 'credentials.json'; | ||
|
||
final http.Client _httpClient; | ||
final ObtainAccessCredentials _obtainAccessCredentials; | ||
final credentialsFilePath = p.join(shorebirdConfigDir, _credentialsFileName); | ||
|
||
void login({required String apiKey}) { | ||
_session = Session(apiKey: apiKey); | ||
_flushSession(_session!); | ||
http.Client get client { | ||
if (credentials == null) return _httpClient; | ||
return autoRefreshingClient(_clientId, credentials!, _httpClient); | ||
} | ||
|
||
void logout() => _clearSession(); | ||
Future<void> login(void Function(String) prompt) async { | ||
if (credentials != null) return; | ||
|
||
Session? _session; | ||
final client = http.Client(); | ||
try { | ||
_credentials = await _obtainAccessCredentials( | ||
_clientId, | ||
_scopes, | ||
client, | ||
prompt, | ||
); | ||
_flushCredentials(_credentials!); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
void logout() => _clearCredentials(); | ||
|
||
AccessCredentials? _credentials; | ||
|
||
Session? get currentSession => _session; | ||
AccessCredentials? get credentials => _credentials; | ||
|
||
void _loadSession() { | ||
final sessionFile = File(sessionFilePath); | ||
void _loadCredentials() { | ||
final credentialsFile = File(credentialsFilePath); | ||
|
||
if (sessionFile.existsSync()) { | ||
if (credentialsFile.existsSync()) { | ||
try { | ||
final contents = sessionFile.readAsStringSync(); | ||
_session = Session.fromJson( | ||
final contents = credentialsFile.readAsStringSync(); | ||
_credentials = AccessCredentials.fromJson( | ||
json.decode(contents) as Map<String, dynamic>, | ||
); | ||
} catch (_) {} | ||
} | ||
} | ||
|
||
void _flushSession(Session session) { | ||
File(sessionFilePath) | ||
void _flushCredentials(AccessCredentials credentials) { | ||
File(credentialsFilePath) | ||
..createSync(recursive: true) | ||
..writeAsStringSync(json.encode(session.toJson())); | ||
..writeAsStringSync(json.encode(credentials.toJson())); | ||
} | ||
|
||
void _clearSession() { | ||
_session = null; | ||
void _clearCredentials() { | ||
_credentials = null; | ||
|
||
final sessionFile = File(sessionFilePath); | ||
if (sessionFile.existsSync()) { | ||
sessionFile.deleteSync(recursive: true); | ||
final credentialsFile = File(credentialsFilePath); | ||
if (credentialsFile.existsSync()) { | ||
credentialsFile.deleteSync(recursive: true); | ||
} | ||
} | ||
|
||
void close() { | ||
_httpClient.close(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
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
Oops, something went wrong.