2025-03-09 20:26:37 -05:00
|
|
|
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
2024-05-02 15:59:14 -05:00
|
|
|
import 'package:immich_mobile/services/api.service.dart';
|
2023-06-25 18:59:35 -05:00
|
|
|
import 'package:logging/logging.dart';
|
2022-11-20 11:43:10 -06:00
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
|
|
2024-08-28 12:30:06 -04:00
|
|
|
// Redirect URL = app.immich:///oauth-callback
|
2022-11-20 11:43:10 -06:00
|
|
|
|
|
|
|
|
class OAuthService {
|
|
|
|
|
final ApiService _apiService;
|
|
|
|
|
final callbackUrlScheme = 'app.immich';
|
2023-06-25 18:59:35 -05:00
|
|
|
final log = Logger('OAuthService');
|
2022-11-20 11:43:10 -06:00
|
|
|
OAuthService(this._apiService);
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<String?> getOAuthServerUrl(String serverUrl, String state, String codeChallenge) async {
|
2023-01-19 07:45:37 -08:00
|
|
|
// Resolve API server endpoint from user provided serverUrl
|
|
|
|
|
await _apiService.resolveAndSetEndpoint(serverUrl);
|
2024-08-28 12:30:06 -04:00
|
|
|
final redirectUri = '$callbackUrlScheme:///oauth-callback';
|
2025-07-29 00:34:03 +05:30
|
|
|
log.info("Starting OAuth flow with redirect URI: $redirectUri");
|
2022-11-20 11:43:10 -06:00
|
|
|
|
2024-01-04 20:44:40 +00:00
|
|
|
final dto = await _apiService.oAuthApi.startOAuth(
|
2025-07-29 00:34:03 +05:30
|
|
|
OAuthConfigDto(redirectUri: redirectUri, state: state, codeChallenge: codeChallenge),
|
2022-11-20 11:43:10 -06:00
|
|
|
);
|
2024-08-28 12:30:06 -04:00
|
|
|
|
|
|
|
|
final authUrl = dto?.url;
|
|
|
|
|
log.info('Received Authorization URL: $authUrl');
|
|
|
|
|
|
|
|
|
|
return authUrl;
|
2022-11-20 11:43:10 -06:00
|
|
|
}
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<LoginResponseDto?> oAuthLogin(String oauthUrl, String state, String codeVerifier) async {
|
|
|
|
|
String result = await FlutterWebAuth2.authenticate(url: oauthUrl, callbackUrlScheme: callbackUrlScheme);
|
2024-08-28 12:30:06 -04:00
|
|
|
|
|
|
|
|
log.info('Received OAuth callback: $result');
|
2022-11-20 11:43:10 -06:00
|
|
|
|
2024-08-28 12:30:06 -04:00
|
|
|
if (result.startsWith('app.immich:/oauth-callback')) {
|
2025-07-29 00:34:03 +05:30
|
|
|
result = result.replaceAll('app.immich:/oauth-callback', 'app.immich:///oauth-callback');
|
2022-11-20 11:43:10 -06:00
|
|
|
}
|
2024-08-28 12:30:06 -04:00
|
|
|
|
|
|
|
|
return await _apiService.oAuthApi.finishOAuth(
|
2025-07-29 00:34:03 +05:30
|
|
|
OAuthCallbackDto(url: result, state: state, codeVerifier: codeVerifier),
|
2024-08-28 12:30:06 -04:00
|
|
|
);
|
2022-11-20 11:43:10 -06:00
|
|
|
}
|
|
|
|
|
}
|