2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/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';
|
|
|
|
|
2024-08-28 16:30:06 +00:00
|
|
|
// Redirect URL = app.immich:///oauth-callback
|
2022-11-20 17:43:10 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-01-04 20:44:40 +00:00
|
|
|
Future<String?> getOAuthServerUrl(
|
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);
|
2024-08-28 16:30:06 +00:00
|
|
|
final redirectUri = '$callbackUrlScheme:///oauth-callback';
|
|
|
|
log.info(
|
|
|
|
"Starting OAuth flow with redirect URI: $redirectUri",
|
|
|
|
);
|
2022-11-20 17:43:10 +00:00
|
|
|
|
2024-01-04 20:44:40 +00:00
|
|
|
final dto = await _apiService.oAuthApi.startOAuth(
|
2024-08-28 16:30:06 +00:00
|
|
|
OAuthConfigDto(redirectUri: redirectUri),
|
2022-11-20 17:43:10 +00:00
|
|
|
);
|
2024-08-28 16:30:06 +00:00
|
|
|
|
|
|
|
final authUrl = dto?.url;
|
|
|
|
log.info('Received Authorization URL: $authUrl');
|
|
|
|
|
|
|
|
return authUrl;
|
2022-11-20 17:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<LoginResponseDto?> oAuthLogin(String oauthUrl) async {
|
2024-08-28 16:30:06 +00:00
|
|
|
String result = await FlutterWebAuth.authenticate(
|
|
|
|
url: oauthUrl,
|
|
|
|
callbackUrlScheme: callbackUrlScheme,
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info('Received OAuth callback: $result');
|
2022-11-20 17:43:10 +00:00
|
|
|
|
2024-08-28 16:30:06 +00:00
|
|
|
if (result.startsWith('app.immich:/oauth-callback')) {
|
|
|
|
result = result.replaceAll(
|
|
|
|
'app.immich:/oauth-callback',
|
|
|
|
'app.immich:///oauth-callback',
|
2022-11-20 17:43:10 +00:00
|
|
|
);
|
|
|
|
}
|
2024-08-28 16:30:06 +00:00
|
|
|
|
|
|
|
return await _apiService.oAuthApi.finishOAuth(
|
|
|
|
OAuthCallbackDto(
|
|
|
|
url: result,
|
|
|
|
),
|
|
|
|
);
|
2022-11-20 17:43:10 +00:00
|
|
|
}
|
|
|
|
}
|