Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logout when OpenID service is unavailable #1361

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/uni_app/lib/session/flows/federated/client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:http/http.dart' as http;
import 'package:uni/http/client/timeout.dart';

class FederatedDefaultClient extends http.BaseClient {
FederatedDefaultClient()
: inner = TimeoutClient(
http.Client(),
timeout: const Duration(seconds: 5),
);

final http.Client inner;

@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
return inner.send(request);
}
}
33 changes: 30 additions & 3 deletions packages/uni_app/lib/session/flows/federated/initiator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:openid_client/openid_client.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uni/session/exception.dart';
import 'package:uni/session/flows/base/initiator.dart';
import 'package:uni/session/flows/federated/client.dart';
import 'package:uni/session/flows/federated/request.dart';

class FederatedSessionInitiator extends SessionInitiator {
Expand All @@ -14,11 +19,30 @@ class FederatedSessionInitiator extends SessionInitiator {
final String clientId;
final Future<Uri> Function(Flow flow) performAuthentication;

Future<T> _handleOpenIdExceptions<T>(
Future<T> future, {
required AuthenticationException onError,
}) {
T reportExceptionAndFail<E extends Object>(E error, StackTrace st) {
unawaited(Sentry.captureException(error, stackTrace: st));
throw onError;
}

return future
.onError<HttpRequestException>(reportExceptionAndFail)
.onError<OpenIdException>(reportExceptionAndFail);
}

@override
Future<FederatedSessionRequest> initiate([http.Client? httpClient]) async {
final issuer = await Issuer.discover(realm);
final client = Client(issuer, clientId, httpClient: httpClient);
httpClient ??= FederatedDefaultClient();

final issuer = await _handleOpenIdExceptions(
Issuer.discover(realm, httpClient: httpClient),
onError: const AuthenticationException('Failed to discover OIDC issuer'),
);

final client = Client(issuer, clientId, httpClient: httpClient);
final flow = Flow.authorizationCodeWithPKCE(
client,
scopes: [
Expand All @@ -32,7 +56,10 @@ class FederatedSessionInitiator extends SessionInitiator {
);

final uri = await performAuthentication(flow);
final credential = await flow.callback(uri.queryParameters);
final credential = await _handleOpenIdExceptions(
flow.callback(uri.queryParameters),
onError: const AuthenticationException('Failed to execute flow callback'),
);

return FederatedSessionRequest(credential: credential);
}
Expand Down
32 changes: 24 additions & 8 deletions packages/uni_app/lib/session/flows/federated/request.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:openid_client/openid_client.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uni/controller/fetchers/faculties_fetcher.dart';
import 'package:uni/session/exception.dart';
import 'package:uni/session/flows/base/request.dart';
import 'package:uni/session/flows/federated/client.dart';
import 'package:uni/session/flows/federated/session.dart';
import 'package:uni/sigarra/endpoints/oidc/oidc.dart';
import 'package:uni/sigarra/endpoints/oidc/token/response.dart';
import 'package:uni/sigarra/options.dart';

class FederatedSessionUserInfo {
Expand Down Expand Up @@ -38,18 +43,29 @@ class FederatedSessionRequest extends SessionRequest {

final Credential credential;

TokenFailedResponse _reportExceptionAndFail<E extends Object>(
E error,
StackTrace st,
) {
unawaited(Sentry.captureException(error, stackTrace: st));
return const TokenFailedResponse();
}

@override
Future<FederatedSession> perform([http.Client? httpClient]) async {
final client = httpClient ?? http.Client();
httpClient ??= FederatedDefaultClient();

final authorizedClient = credential.createHttpClient(client);
final authorizedClient = credential.createHttpClient(httpClient);

final oidc = SigarraOidc();
final response = await oidc.token.call(
options: BaseRequestOptions(
client: authorizedClient,
),
);
final response = await oidc.token
.call(
options: BaseRequestOptions(
client: authorizedClient,
),
)
.onError<OpenIdException>(_reportExceptionAndFail)
.onError<HttpRequestException>(_reportExceptionAndFail);

if (!response.success) {
throw const AuthenticationException('Failed to get OIDC token');
Expand All @@ -65,7 +81,7 @@ class FederatedSessionRequest extends SessionRequest {
credential: credential,
);

final faculties = await getStudentFaculties(tempSession, client);
final faculties = await getStudentFaculties(tempSession, httpClient);

return FederatedSession(
username: userInfo.username,
Expand Down