2022-11-20 17:43:10 +00:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-06-25 23:59:35 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2022-11-20 17:43:10 +00:00
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
import 'package:flutter_web_auth/flutter_web_auth.dart';
|
|
|
|
|
|
|
|
// Redirect URL = app.immich://
|
|
|
|
|
|
|
|
class OAuthService {
|
|
|
|
final ApiService _apiService;
|
|
|
|
final callbackUrlScheme = 'app.immich';
|
2023-06-25 23:59:35 +00:00
|
|
|
final log = Logger('OAuthService');
|
2022-11-20 17:43:10 +00:00
|
|
|
OAuthService(this._apiService);
|
|
|
|
|
|
|
|
Future<OAuthConfigResponseDto?> getOAuthServerConfig(
|
2023-01-19 15:45:37 +00:00
|
|
|
String serverUrl,
|
2022-11-20 17:43:10 +00:00
|
|
|
) async {
|
2023-01-19 15:45:37 +00:00
|
|
|
// Resolve API server endpoint from user provided serverUrl
|
|
|
|
await _apiService.resolveAndSetEndpoint(serverUrl);
|
2022-11-20 17:43:10 +00:00
|
|
|
|
2023-11-04 01:33:15 +00:00
|
|
|
return await _apiService.oAuthApi.generateOAuthConfig(
|
2022-11-20 17:43:10 +00:00
|
|
|
OAuthConfigDto(redirectUri: '$callbackUrlScheme:/'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<LoginResponseDto?> oAuthLogin(String oauthUrl) async {
|
|
|
|
try {
|
|
|
|
var result = await FlutterWebAuth.authenticate(
|
|
|
|
url: oauthUrl,
|
|
|
|
callbackUrlScheme: callbackUrlScheme,
|
|
|
|
);
|
|
|
|
|
2023-11-04 01:33:15 +00:00
|
|
|
return await _apiService.oAuthApi.finishOAuth(
|
2022-11-20 17:43:10 +00:00
|
|
|
OAuthCallbackDto(
|
|
|
|
url: result,
|
|
|
|
),
|
|
|
|
);
|
2023-06-25 23:59:35 +00:00
|
|
|
} catch (e, stack) {
|
|
|
|
log.severe("Error performing oAuthLogin: ${e.toString()}", e, stack);
|
2022-11-20 17:43:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|