-
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.
- Loading branch information
Showing
34 changed files
with
328 additions
and
211 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,103 @@ | ||
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. | ||
'30552215580-6kgm623s9l4te0rd63r18c5u6b6gkts5.apps.googleusercontent.com', | ||
|
||
/// Shorebird CLI's OAuth 2.0 secret. | ||
/// | ||
/// This isn't actually meant to be kept secret. | ||
'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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(); | ||
} | ||
|
||
final http.Client _httpClient; | ||
final ObtainAccessCredentials _obtainAccessCredentials; | ||
static const _credentialsFileName = 'credentials.json'; | ||
final credentialsFilePath = p.join(shorebirdConfigDir, _credentialsFileName); | ||
|
||
http.Client get client { | ||
if (credentials == null) return _httpClient; | ||
return autoRefreshingClient(_clientId, credentials!, _httpClient); | ||
} | ||
|
||
static const _sessionFileName = 'shorebird-session.json'; | ||
final sessionFilePath = p.join(shorebirdConfigDir, _sessionFileName); | ||
Future<void> login(void Function(String) prompt) async { | ||
if (credentials != null) return; | ||
|
||
void login({required String apiKey}) { | ||
_session = Session(apiKey: apiKey); | ||
_flushSession(_session!); | ||
final client = http.Client(); | ||
try { | ||
_credentials = await _obtainAccessCredentials( | ||
_clientId, | ||
_scopes, | ||
client, | ||
prompt, | ||
); | ||
_flushCredentials(_credentials!); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
void logout() => _clearSession(); | ||
void logout() => _clearCredentials(); | ||
|
||
Session? _session; | ||
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
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.