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 12:43:10 -05:00
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
import 'package:flutter_web_auth/flutter_web_auth.dart';
|
|
|
|
|
2024-08-28 11:30:06 -05:00
|
|
|
// Redirect URL = app.immich:///oauth-callback
|
2022-11-20 12:43:10 -05: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 12:43:10 -05:00
|
|
|
OAuthService(this._apiService);
|
|
|
|
|
2024-01-04 15:44:40 -05:00
|
|
|
Future<String?> getOAuthServerUrl(
|
2023-01-19 10:45:37 -05:00
|
|
|
String serverUrl,
|
2022-11-20 12:43:10 -05:00
|
|
|
) async {
|
2023-01-19 10:45:37 -05:00
|
|
|
// Resolve API server endpoint from user provided serverUrl
|
|
|
|
await _apiService.resolveAndSetEndpoint(serverUrl);
|
2024-08-28 11:30:06 -05:00
|
|
|
final redirectUri = '$callbackUrlScheme:///oauth-callback';
|
|
|
|
log.info(
|
|
|
|
"Starting OAuth flow with redirect URI: $redirectUri",
|
|
|
|
);
|
2022-11-20 12:43:10 -05:00
|
|
|
|
2024-01-04 15:44:40 -05:00
|
|
|
final dto = await _apiService.oAuthApi.startOAuth(
|
2024-08-28 11:30:06 -05:00
|
|
|
OAuthConfigDto(redirectUri: redirectUri),
|
2022-11-20 12:43:10 -05:00
|
|
|
);
|
2024-08-28 11:30:06 -05:00
|
|
|
|
|
|
|
final authUrl = dto?.url;
|
|
|
|
log.info('Received Authorization URL: $authUrl');
|
|
|
|
|
|
|
|
return authUrl;
|
2022-11-20 12:43:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<LoginResponseDto?> oAuthLogin(String oauthUrl) async {
|
2024-08-28 11:30:06 -05:00
|
|
|
String result = await FlutterWebAuth.authenticate(
|
|
|
|
url: oauthUrl,
|
|
|
|
callbackUrlScheme: callbackUrlScheme,
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info('Received OAuth callback: $result');
|
2022-11-20 12:43:10 -05:00
|
|
|
|
2024-08-28 11:30:06 -05:00
|
|
|
if (result.startsWith('app.immich:/oauth-callback')) {
|
|
|
|
result = result.replaceAll(
|
|
|
|
'app.immich:/oauth-callback',
|
|
|
|
'app.immich:///oauth-callback',
|
2022-11-20 12:43:10 -05:00
|
|
|
);
|
|
|
|
}
|
2024-08-28 11:30:06 -05:00
|
|
|
|
|
|
|
return await _apiService.oAuthApi.finishOAuth(
|
|
|
|
OAuthCallbackDto(
|
|
|
|
url: result,
|
|
|
|
),
|
|
|
|
);
|
2022-11-20 12:43:10 -05:00
|
|
|
}
|
|
|
|
}
|