Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c41a8805d | ||
|
|
45de2591a7 | ||
|
|
2fe6067085 |
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [1.0.5] - 2026-01-04
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- **Web authentication now uses OIDC** instead of NPM forward auth
|
||||||
|
- Added `OidcServiceWeb` for browser redirect-based Authorization Code flow with PKCE
|
||||||
|
- Added `/callback` route to handle Authentik redirect after login
|
||||||
|
- Login page now shows "Sign in with Authentik" button for both web and mobile
|
||||||
|
- Tokens stored in SharedPreferences and synced with core-api via `/auth/sync`
|
||||||
|
- Added web utility functions (`web_utils.dart`) with conditional imports for non-web platforms
|
||||||
|
- Added `crypto` and `web` packages for PKCE SHA-256 and browser API access
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Removed cross-origin cookie dependency that caused authentication failures on web
|
||||||
|
|
||||||
|
## [1.0.4] - 2026-01-03
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- `health.json` generated at build time with app version info
|
||||||
|
- `health.html` now displays version, title, and status from health.json
|
||||||
|
|
||||||
## [1.0.3] - 2026-01-03
|
## [1.0.3] - 2026-01-03
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ COPY . .
|
|||||||
# Generate code with build_runner
|
# Generate code with build_runner
|
||||||
RUN dart run build_runner build --delete-conflicting-outputs
|
RUN dart run build_runner build --delete-conflicting-outputs
|
||||||
|
|
||||||
|
# Generate health.json with version info
|
||||||
|
RUN dart run tool/generate_health_json.dart
|
||||||
|
|
||||||
# Build for web release with production configuration
|
# Build for web release with production configuration
|
||||||
# These URLs enable authentication (requiresAuth = true when URL contains schweitz.net)
|
# These URLs enable authentication (requiresAuth = true when URL contains schweitz.net)
|
||||||
RUN flutter build web --release \
|
RUN flutter build web --release \
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:developer' as developer;
|
import 'dart:developer' as developer;
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
@@ -10,19 +9,21 @@ import '../config/app_config.dart';
|
|||||||
import 'auth_datasource.dart';
|
import 'auth_datasource.dart';
|
||||||
import 'auth_state.dart';
|
import 'auth_state.dart';
|
||||||
import 'oidc_service.dart';
|
import 'oidc_service.dart';
|
||||||
|
import 'oidc_service_web.dart';
|
||||||
import 'permissions.dart';
|
import 'permissions.dart';
|
||||||
import 'user_preferences.dart';
|
import 'user_preferences.dart';
|
||||||
|
import 'web_utils.dart' as web_utils;
|
||||||
|
|
||||||
part 'auth_provider.g.dart';
|
part 'auth_provider.g.dart';
|
||||||
|
|
||||||
/// Provides authentication state and operations.
|
/// Provides authentication state and operations.
|
||||||
///
|
///
|
||||||
/// Supports two authentication flows:
|
/// Supports OIDC Authorization Code flow with PKCE on all platforms:
|
||||||
/// - **Web**: NPM forward auth with Authentik (cookies handled by proxy)
|
/// - **Web**: Browser redirect to Authentik, callback via /callback route
|
||||||
/// - **Mobile**: OIDC Authorization Code flow with flutter_appauth
|
/// - **Mobile**: flutter_appauth with custom URL scheme
|
||||||
///
|
///
|
||||||
/// On web, the app calls GET /auth/me to check if user is authenticated
|
/// After OIDC authentication, syncs with core-api via POST /auth/sync
|
||||||
/// via NPM forward auth headers. On mobile, uses OIDC flow then POST /auth/sync.
|
/// to get user profile, roles, and preferences.
|
||||||
@riverpod
|
@riverpod
|
||||||
class AuthNotifier extends _$AuthNotifier {
|
class AuthNotifier extends _$AuthNotifier {
|
||||||
// Storage keys
|
// Storage keys
|
||||||
@@ -39,71 +40,10 @@ class AuthNotifier extends _$AuthNotifier {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<AuthState> build() async {
|
Future<AuthState> build() async {
|
||||||
// On web with auth required, try to get user from NPM forward auth
|
// Load stored auth on all platforms
|
||||||
if (kIsWeb && AppConfig.requiresAuth) {
|
|
||||||
final webAuth = await _tryWebAuth();
|
|
||||||
if (webAuth != null) {
|
|
||||||
return webAuth;
|
|
||||||
}
|
|
||||||
// If web auth failed, user needs to refresh to trigger NPM login
|
|
||||||
developer.log('Web auth not available - user may need to login via proxy', name: 'auth');
|
|
||||||
return const AuthState();
|
|
||||||
}
|
|
||||||
|
|
||||||
// On mobile or LAN, load from stored auth
|
|
||||||
return _loadStoredAuth();
|
return _loadStoredAuth();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to authenticate via NPM forward auth (web only).
|
|
||||||
///
|
|
||||||
/// Returns AuthState if authenticated, null if not.
|
|
||||||
Future<AuthState?> _tryWebAuth() async {
|
|
||||||
try {
|
|
||||||
developer.log('Attempting web auth via /auth/me', name: 'auth');
|
|
||||||
final authDatasource = ref.read(authDatasourceProvider);
|
|
||||||
final syncResponse = await authDatasource.getCurrentUser();
|
|
||||||
|
|
||||||
developer.log(
|
|
||||||
'Web auth successful: ${syncResponse.name} with ${syncResponse.roles.length} roles',
|
|
||||||
name: 'auth',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Store auth data for offline/cached access
|
|
||||||
await _storeAuth(
|
|
||||||
accessToken: 'web-session', // Placeholder - web uses cookies
|
|
||||||
userId: syncResponse.userId,
|
|
||||||
authentikId: syncResponse.authentikId,
|
|
||||||
userName: syncResponse.name,
|
|
||||||
userEmail: syncResponse.email,
|
|
||||||
avatarUrl: syncResponse.avatarUrl,
|
|
||||||
roles: syncResponse.roles,
|
|
||||||
preferences: syncResponse.preferences,
|
|
||||||
);
|
|
||||||
|
|
||||||
return AuthState(
|
|
||||||
isAuthenticated: true,
|
|
||||||
accessToken: 'web-session',
|
|
||||||
userId: syncResponse.userId,
|
|
||||||
authentikId: syncResponse.authentikId,
|
|
||||||
userName: syncResponse.name,
|
|
||||||
userEmail: syncResponse.email,
|
|
||||||
avatarUrl: syncResponse.avatarUrl,
|
|
||||||
roles: syncResponse.roles,
|
|
||||||
preferences: syncResponse.preferences,
|
|
||||||
);
|
|
||||||
} on DioException catch (e) {
|
|
||||||
if (e.response?.statusCode == 401) {
|
|
||||||
developer.log('Web auth: not authenticated via proxy', name: 'auth');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
developer.log('Web auth error: $e', name: 'auth');
|
|
||||||
return null;
|
|
||||||
} catch (e) {
|
|
||||||
developer.log('Web auth unexpected error: $e', name: 'auth');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<AuthState> _loadStoredAuth() async {
|
Future<AuthState> _loadStoredAuth() async {
|
||||||
try {
|
try {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
@@ -224,7 +164,7 @@ class AuthNotifier extends _$AuthNotifier {
|
|||||||
|
|
||||||
/// Sign in with the appropriate method for the platform.
|
/// Sign in with the appropriate method for the platform.
|
||||||
///
|
///
|
||||||
/// - **Web**: Triggers page reload to go through NPM forward auth
|
/// - **Web**: Redirects to Authentik for OIDC authentication
|
||||||
/// - **Mobile**: Opens Authentik login via OIDC, then syncs with core-api
|
/// - **Mobile**: Opens Authentik login via OIDC, then syncs with core-api
|
||||||
Future<void> signIn() async {
|
Future<void> signIn() async {
|
||||||
if (!AppConfig.requiresAuth) {
|
if (!AppConfig.requiresAuth) {
|
||||||
@@ -234,25 +174,25 @@ class AuthNotifier extends _$AuthNotifier {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// On web, auth is handled by NPM forward auth
|
// Web: Use OIDC flow with browser redirect
|
||||||
// User needs to access via the authenticated proxy URL
|
|
||||||
if (kIsWeb) {
|
if (kIsWeb) {
|
||||||
developer.log('Web sign-in: user should access via authenticated proxy', name: 'auth');
|
developer.log('Web sign-in: starting OIDC flow', name: 'auth');
|
||||||
// Try to refresh auth state from /auth/me
|
|
||||||
state = const AsyncLoading();
|
state = const AsyncLoading();
|
||||||
final webAuth = await _tryWebAuth();
|
|
||||||
if (webAuth != null) {
|
try {
|
||||||
state = AsyncData(webAuth);
|
final oidcService = OidcServiceWeb();
|
||||||
} else {
|
final authUrl = await oidcService.getAuthorizationUrl();
|
||||||
state = AsyncError(
|
developer.log('Redirecting to: $authUrl', name: 'auth');
|
||||||
Exception('Not authenticated - please access via the authenticated URL'),
|
web_utils.redirectTo(authUrl);
|
||||||
StackTrace.current,
|
// Browser will redirect, so we don't update state here
|
||||||
);
|
} catch (e, stack) {
|
||||||
|
developer.log('Failed to start OIDC flow: $e', name: 'auth');
|
||||||
|
state = AsyncError(e, stack);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mobile: Use OIDC flow
|
// Mobile: Use OIDC flow with flutter_appauth
|
||||||
state = const AsyncLoading();
|
state = const AsyncLoading();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -307,6 +247,73 @@ class AuthNotifier extends _$AuthNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handle OIDC callback after Authentik redirects back (web only).
|
||||||
|
///
|
||||||
|
/// [code] is the authorization code from the callback URL.
|
||||||
|
/// [state] is the state parameter for CSRF verification.
|
||||||
|
Future<void> handleOidcCallback(String code, String callbackState) async {
|
||||||
|
if (!kIsWeb) {
|
||||||
|
developer.log('handleOidcCallback called on non-web platform', name: 'auth');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
developer.log('Handling OIDC callback', name: 'auth');
|
||||||
|
state = const AsyncLoading();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Step 1: Exchange code for tokens
|
||||||
|
final oidcService = OidcServiceWeb();
|
||||||
|
final tokens = await oidcService.exchangeCode(code, callbackState);
|
||||||
|
|
||||||
|
// Step 2: Sync with core-api to get user profile and roles
|
||||||
|
developer.log('Syncing with core-api', name: 'auth');
|
||||||
|
final authDatasource = ref.read(authDatasourceProvider);
|
||||||
|
final syncResponse = await authDatasource.syncUser(tokens.accessToken);
|
||||||
|
|
||||||
|
// Step 3: Store credentials and user data
|
||||||
|
await _storeAuth(
|
||||||
|
accessToken: tokens.accessToken,
|
||||||
|
refreshToken: tokens.refreshToken,
|
||||||
|
expiresAt: tokens.expiresAt,
|
||||||
|
userId: syncResponse.userId,
|
||||||
|
authentikId: syncResponse.authentikId,
|
||||||
|
userName: syncResponse.name,
|
||||||
|
userEmail: syncResponse.email,
|
||||||
|
avatarUrl: syncResponse.avatarUrl,
|
||||||
|
roles: syncResponse.roles,
|
||||||
|
preferences: syncResponse.preferences,
|
||||||
|
);
|
||||||
|
|
||||||
|
state = AsyncData(AuthState(
|
||||||
|
isAuthenticated: true,
|
||||||
|
accessToken: tokens.accessToken,
|
||||||
|
refreshToken: tokens.refreshToken,
|
||||||
|
expiresAt: tokens.expiresAt,
|
||||||
|
userId: syncResponse.userId,
|
||||||
|
authentikId: syncResponse.authentikId,
|
||||||
|
userName: syncResponse.name,
|
||||||
|
userEmail: syncResponse.email,
|
||||||
|
avatarUrl: syncResponse.avatarUrl,
|
||||||
|
roles: syncResponse.roles,
|
||||||
|
preferences: syncResponse.preferences,
|
||||||
|
));
|
||||||
|
|
||||||
|
developer.log(
|
||||||
|
'Authenticated as ${syncResponse.name} with ${syncResponse.roles.length} roles',
|
||||||
|
name: 'auth',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Clean up the URL by removing the query parameters
|
||||||
|
web_utils.replaceUrl('/');
|
||||||
|
} on OidcException catch (e) {
|
||||||
|
developer.log('OIDC callback failed: $e', name: 'auth');
|
||||||
|
state = AsyncError(e, StackTrace.current);
|
||||||
|
} catch (e, stack) {
|
||||||
|
developer.log('Callback handling failed: $e', name: 'auth');
|
||||||
|
state = AsyncError(e, stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Sign out and clear stored credentials.
|
/// Sign out and clear stored credentials.
|
||||||
Future<void> signOut() async {
|
Future<void> signOut() async {
|
||||||
await _clearStoredAuth();
|
await _clearStoredAuth();
|
||||||
|
|||||||
@@ -0,0 +1,196 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:developer' as developer;
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:crypto/crypto.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
|
||||||
|
import '../config/app_config.dart';
|
||||||
|
import 'oidc_service.dart';
|
||||||
|
|
||||||
|
/// Web implementation of OIDC service using browser redirect flow.
|
||||||
|
///
|
||||||
|
/// Uses Authorization Code flow with PKCE for secure authentication.
|
||||||
|
/// On web, we can't use flutter_appauth, so we implement the flow manually
|
||||||
|
/// using browser redirects and URL parsing.
|
||||||
|
class OidcServiceWeb implements OidcService {
|
||||||
|
OidcServiceWeb({Dio? dio}) : _dio = dio ?? Dio();
|
||||||
|
|
||||||
|
final Dio _dio;
|
||||||
|
|
||||||
|
/// OIDC scopes to request.
|
||||||
|
static const _scopes = ['openid', 'profile', 'email', 'offline_access'];
|
||||||
|
|
||||||
|
/// Redirect URI for web.
|
||||||
|
static String get _redirectUri => '${AppConfig.webBaseUrl}/callback';
|
||||||
|
|
||||||
|
// PKCE state stored during auth flow (in-memory for single-page app)
|
||||||
|
static String? _codeVerifier;
|
||||||
|
static String? _state;
|
||||||
|
|
||||||
|
/// Get the authorization URL to redirect the browser to.
|
||||||
|
///
|
||||||
|
/// Returns a URL that the browser should navigate to for authentication.
|
||||||
|
/// The [codeVerifier] and [state] are stored for later verification.
|
||||||
|
Future<String> getAuthorizationUrl() async {
|
||||||
|
// Fetch OIDC discovery document
|
||||||
|
final discovery = await _fetchDiscovery();
|
||||||
|
final authEndpoint = discovery['authorization_endpoint'] as String;
|
||||||
|
|
||||||
|
// Generate PKCE code verifier and challenge
|
||||||
|
_codeVerifier = _generateCodeVerifier();
|
||||||
|
final codeChallenge = _generateCodeChallenge(_codeVerifier!);
|
||||||
|
|
||||||
|
// Generate state for CSRF protection
|
||||||
|
_state = _generateRandomString(32);
|
||||||
|
|
||||||
|
// Build authorization URL
|
||||||
|
final params = {
|
||||||
|
'client_id': AppConfig.authClientId,
|
||||||
|
'redirect_uri': _redirectUri,
|
||||||
|
'response_type': 'code',
|
||||||
|
'scope': _scopes.join(' '),
|
||||||
|
'code_challenge': codeChallenge,
|
||||||
|
'code_challenge_method': 'S256',
|
||||||
|
'state': _state,
|
||||||
|
};
|
||||||
|
|
||||||
|
final uri = Uri.parse(authEndpoint).replace(queryParameters: params);
|
||||||
|
developer.log('Authorization URL: $uri', name: 'oidc_web');
|
||||||
|
return uri.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exchange authorization code for tokens.
|
||||||
|
///
|
||||||
|
/// Call this after the browser redirects back with the authorization code.
|
||||||
|
/// [code] is the authorization code from the callback URL.
|
||||||
|
/// [state] is the state parameter from the callback URL (verified for CSRF).
|
||||||
|
Future<OidcTokens> exchangeCode(String code, String state) async {
|
||||||
|
// Verify state matches
|
||||||
|
if (_state == null || state != _state) {
|
||||||
|
throw OidcException('State mismatch - possible CSRF attack');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_codeVerifier == null) {
|
||||||
|
throw OidcException('No code verifier - flow not started properly');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Fetch token endpoint from discovery
|
||||||
|
final discovery = await _fetchDiscovery();
|
||||||
|
final tokenEndpoint = discovery['token_endpoint'] as String;
|
||||||
|
|
||||||
|
developer.log('Exchanging code at: $tokenEndpoint', name: 'oidc_web');
|
||||||
|
|
||||||
|
// Exchange code for tokens
|
||||||
|
final response = await _dio.post<Map<String, dynamic>>(
|
||||||
|
tokenEndpoint,
|
||||||
|
data: {
|
||||||
|
'grant_type': 'authorization_code',
|
||||||
|
'client_id': AppConfig.authClientId,
|
||||||
|
'redirect_uri': _redirectUri,
|
||||||
|
'code': code,
|
||||||
|
'code_verifier': _codeVerifier,
|
||||||
|
},
|
||||||
|
options: Options(
|
||||||
|
contentType: Headers.formUrlEncodedContentType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final data = response.data!;
|
||||||
|
developer.log('Token exchange successful', name: 'oidc_web');
|
||||||
|
|
||||||
|
// Clear stored PKCE state
|
||||||
|
_codeVerifier = null;
|
||||||
|
_state = null;
|
||||||
|
|
||||||
|
return OidcTokens(
|
||||||
|
accessToken: data['access_token'] as String,
|
||||||
|
refreshToken: data['refresh_token'] as String?,
|
||||||
|
expiresAt: DateTime.now().add(
|
||||||
|
Duration(seconds: data['expires_in'] as int? ?? 3600),
|
||||||
|
),
|
||||||
|
idToken: data['id_token'] as String?,
|
||||||
|
);
|
||||||
|
} on DioException catch (e) {
|
||||||
|
developer.log('Token exchange failed: $e', name: 'oidc_web');
|
||||||
|
throw OidcException('Token exchange failed: ${e.message}');
|
||||||
|
} finally {
|
||||||
|
// Clear PKCE state on error too
|
||||||
|
_codeVerifier = null;
|
||||||
|
_state = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Not used on web - use [getAuthorizationUrl] and [exchangeCode] instead.
|
||||||
|
@override
|
||||||
|
Future<OidcTokens> signIn() async {
|
||||||
|
throw OidcException(
|
||||||
|
'signIn() not supported on web. Use getAuthorizationUrl() and exchangeCode() instead.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Refresh the access token using a refresh token.
|
||||||
|
@override
|
||||||
|
Future<OidcTokens> refreshToken(String refreshToken) async {
|
||||||
|
try {
|
||||||
|
final discovery = await _fetchDiscovery();
|
||||||
|
final tokenEndpoint = discovery['token_endpoint'] as String;
|
||||||
|
|
||||||
|
final response = await _dio.post<Map<String, dynamic>>(
|
||||||
|
tokenEndpoint,
|
||||||
|
data: {
|
||||||
|
'grant_type': 'refresh_token',
|
||||||
|
'client_id': AppConfig.authClientId,
|
||||||
|
'refresh_token': refreshToken,
|
||||||
|
},
|
||||||
|
options: Options(
|
||||||
|
contentType: Headers.formUrlEncodedContentType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final data = response.data!;
|
||||||
|
|
||||||
|
return OidcTokens(
|
||||||
|
accessToken: data['access_token'] as String,
|
||||||
|
refreshToken: data['refresh_token'] as String? ?? refreshToken,
|
||||||
|
expiresAt: DateTime.now().add(
|
||||||
|
Duration(seconds: data['expires_in'] as int? ?? 3600),
|
||||||
|
),
|
||||||
|
idToken: data['id_token'] as String?,
|
||||||
|
);
|
||||||
|
} on DioException catch (e) {
|
||||||
|
throw OidcException('Token refresh failed: ${e.message}');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch OIDC discovery document.
|
||||||
|
Future<Map<String, dynamic>> _fetchDiscovery() async {
|
||||||
|
final response = await _dio.get<Map<String, dynamic>>(
|
||||||
|
AppConfig.authDiscoveryUrl,
|
||||||
|
);
|
||||||
|
return response.data!;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a random code verifier for PKCE.
|
||||||
|
String _generateCodeVerifier() {
|
||||||
|
return _generateRandomString(64);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate code challenge from verifier using S256.
|
||||||
|
String _generateCodeChallenge(String verifier) {
|
||||||
|
final bytes = utf8.encode(verifier);
|
||||||
|
final digest = sha256.convert(bytes);
|
||||||
|
return base64Url.encode(digest.bytes).replaceAll('=', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a random string of given length.
|
||||||
|
String _generateRandomString(int length) {
|
||||||
|
const chars =
|
||||||
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
|
||||||
|
final random = Random.secure();
|
||||||
|
return List.generate(length, (_) => chars[random.nextInt(chars.length)])
|
||||||
|
.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
/// Web utilities with conditional imports.
|
||||||
|
///
|
||||||
|
/// Uses stub implementation on non-web platforms.
|
||||||
|
library;
|
||||||
|
|
||||||
|
export 'web_utils_stub.dart'
|
||||||
|
if (dart.library.js_interop) 'web_utils_web.dart';
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
/// Stub for non-web platforms.
|
||||||
|
library;
|
||||||
|
|
||||||
|
/// Redirect to a URL (no-op on non-web).
|
||||||
|
void redirectTo(String url) {
|
||||||
|
throw UnsupportedError('redirectTo is only supported on web');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get current URL (no-op on non-web).
|
||||||
|
String getCurrentUrl() {
|
||||||
|
throw UnsupportedError('getCurrentUrl is only supported on web');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace current URL without navigation (no-op on non-web).
|
||||||
|
void replaceUrl(String url) {
|
||||||
|
throw UnsupportedError('replaceUrl is only supported on web');
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/// Web-specific utilities for browser operations.
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'package:web/web.dart' as web;
|
||||||
|
|
||||||
|
/// Redirect the browser to a URL.
|
||||||
|
void redirectTo(String url) {
|
||||||
|
web.window.location.href = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current browser URL.
|
||||||
|
String getCurrentUrl() {
|
||||||
|
return web.window.location.href;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace the current URL in history without navigation.
|
||||||
|
void replaceUrl(String url) {
|
||||||
|
web.window.history.replaceState(null, '', url);
|
||||||
|
}
|
||||||
@@ -45,12 +45,18 @@ class AppConfig {
|
|||||||
defaultValue: 'tatlock-ui',
|
defaultValue: 'tatlock-ui',
|
||||||
);
|
);
|
||||||
|
|
||||||
/// Authentik redirect URI scheme
|
/// Authentik redirect URI scheme (for mobile/native)
|
||||||
static const authRedirectScheme = String.fromEnvironment(
|
static const authRedirectScheme = String.fromEnvironment(
|
||||||
'AUTH_REDIRECT_SCHEME',
|
'AUTH_REDIRECT_SCHEME',
|
||||||
defaultValue: 'net.schweitz.tatlock',
|
defaultValue: 'net.schweitz.tatlock',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// Web app base URL (for OIDC redirect URI on web)
|
||||||
|
static const webBaseUrl = String.fromEnvironment(
|
||||||
|
'WEB_BASE_URL',
|
||||||
|
defaultValue: 'https://home.schweitz.net',
|
||||||
|
);
|
||||||
|
|
||||||
/// Whether running in debug mode
|
/// Whether running in debug mode
|
||||||
static const isDebug = bool.fromEnvironment('DEBUG', defaultValue: false);
|
static const isDebug = bool.fromEnvironment('DEBUG', defaultValue: false);
|
||||||
|
|
||||||
|
|||||||
+148
-30
@@ -1,4 +1,3 @@
|
|||||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
@@ -18,6 +17,7 @@ abstract class AppRoutes {
|
|||||||
static const parlor = '/parlor';
|
static const parlor = '/parlor';
|
||||||
static const settings = '/settings';
|
static const settings = '/settings';
|
||||||
static const login = '/login';
|
static const login = '/login';
|
||||||
|
static const callback = '/callback';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provides the GoRouter instance.
|
/// Provides the GoRouter instance.
|
||||||
@@ -47,6 +47,11 @@ GoRouter appRouter(Ref ref) {
|
|||||||
return AppRoutes.frontHall;
|
return AppRoutes.frontHall;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allow callback route through without auth check
|
||||||
|
if (state.matchedLocation == AppRoutes.callback) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
routes: [
|
routes: [
|
||||||
@@ -56,6 +61,17 @@ GoRouter appRouter(Ref ref) {
|
|||||||
name: 'login',
|
name: 'login',
|
||||||
builder: (context, state) => const _LoginPage(),
|
builder: (context, state) => const _LoginPage(),
|
||||||
),
|
),
|
||||||
|
// OIDC callback route (handles auth code exchange)
|
||||||
|
GoRoute(
|
||||||
|
path: AppRoutes.callback,
|
||||||
|
name: 'callback',
|
||||||
|
builder: (context, state) => _OidcCallbackPage(
|
||||||
|
code: state.uri.queryParameters['code'],
|
||||||
|
callbackState: state.uri.queryParameters['state'],
|
||||||
|
error: state.uri.queryParameters['error'],
|
||||||
|
errorDescription: state.uri.queryParameters['error_description'],
|
||||||
|
),
|
||||||
|
),
|
||||||
// Main app routes (inside shell with app scaffold)
|
// Main app routes (inside shell with app scaffold)
|
||||||
ShellRoute(
|
ShellRoute(
|
||||||
builder: (context, state, child) => AppScaffold(child: child),
|
builder: (context, state, child) => AppScaffold(child: child),
|
||||||
@@ -185,32 +201,7 @@ class _LoginPage extends ConsumerWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildSignInContent(BuildContext context, WidgetRef ref) {
|
Widget _buildSignInContent(BuildContext context, WidgetRef ref) {
|
||||||
if (kIsWeb) {
|
// Same sign in button for both web and mobile
|
||||||
// Web: User needs to access via authenticated proxy
|
|
||||||
return Column(
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
'Please access Tatlock via the authenticated URL.\n'
|
|
||||||
'If you see this page, the proxy authentication may not be configured.',
|
|
||||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
||||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
||||||
),
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
const SizedBox(height: 16),
|
|
||||||
OutlinedButton.icon(
|
|
||||||
onPressed: () => ref.read(authProvider.notifier).signIn(),
|
|
||||||
icon: const Icon(Icons.refresh),
|
|
||||||
label: const Text('Retry'),
|
|
||||||
style: OutlinedButton.styleFrom(
|
|
||||||
minimumSize: const Size(double.infinity, 48),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mobile: Show sign in button
|
|
||||||
return FilledButton.icon(
|
return FilledButton.icon(
|
||||||
onPressed: () => ref.read(authProvider.notifier).signIn(),
|
onPressed: () => ref.read(authProvider.notifier).signIn(),
|
||||||
icon: const Icon(Icons.login),
|
icon: const Icon(Icons.login),
|
||||||
@@ -266,9 +257,136 @@ class _LoginPage extends ConsumerWidget {
|
|||||||
if (message.contains('network')) {
|
if (message.contains('network')) {
|
||||||
return 'Network error. Please check your connection.';
|
return 'Network error. Please check your connection.';
|
||||||
}
|
}
|
||||||
if (message.contains('authenticated URL')) {
|
|
||||||
return 'Not authenticated - please access via the authenticated URL';
|
|
||||||
}
|
|
||||||
return 'Authentication failed. Please try again.';
|
return 'Authentication failed. Please try again.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// OIDC callback page that handles the authorization code exchange.
|
||||||
|
class _OidcCallbackPage extends ConsumerStatefulWidget {
|
||||||
|
const _OidcCallbackPage({
|
||||||
|
this.code,
|
||||||
|
this.callbackState,
|
||||||
|
this.error,
|
||||||
|
this.errorDescription,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String? code;
|
||||||
|
final String? callbackState;
|
||||||
|
final String? error;
|
||||||
|
final String? errorDescription;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ConsumerState<_OidcCallbackPage> createState() => _OidcCallbackPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _OidcCallbackPageState extends ConsumerState<_OidcCallbackPage> {
|
||||||
|
bool _isProcessing = true;
|
||||||
|
String? _error;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_processCallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _processCallback() async {
|
||||||
|
// Check for error from Authentik
|
||||||
|
if (widget.error != null) {
|
||||||
|
setState(() {
|
||||||
|
_isProcessing = false;
|
||||||
|
_error = widget.errorDescription ?? widget.error;
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for required parameters
|
||||||
|
if (widget.code == null || widget.callbackState == null) {
|
||||||
|
setState(() {
|
||||||
|
_isProcessing = false;
|
||||||
|
_error = 'Invalid callback - missing code or state parameter';
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exchange code for tokens
|
||||||
|
try {
|
||||||
|
await ref.read(authProvider.notifier).handleOidcCallback(
|
||||||
|
widget.code!,
|
||||||
|
widget.callbackState!,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Navigate to home on success
|
||||||
|
if (mounted) {
|
||||||
|
context.go(AppRoutes.frontHall);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_isProcessing = false;
|
||||||
|
_error = e.toString();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final colorScheme = Theme.of(context).colorScheme;
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: ConstrainedBox(
|
||||||
|
constraints: const BoxConstraints(maxWidth: 400),
|
||||||
|
child: Card(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(32),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
_error != null ? Icons.error_outline : Icons.home_work_outlined,
|
||||||
|
size: 64,
|
||||||
|
color: _error != null ? colorScheme.error : colorScheme.primary,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
Text(
|
||||||
|
_error != null ? 'Authentication Failed' : 'Signing in...',
|
||||||
|
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
if (_isProcessing)
|
||||||
|
const CircularProgressIndicator()
|
||||||
|
else if (_error != null) ...[
|
||||||
|
Container(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: colorScheme.errorContainer,
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
|
child: Text(
|
||||||
|
_error!,
|
||||||
|
style: TextStyle(color: colorScheme.onErrorContainer),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
OutlinedButton.icon(
|
||||||
|
onPressed: () => context.go(AppRoutes.login),
|
||||||
|
icon: const Icon(Icons.arrow_back),
|
||||||
|
label: const Text('Back to Login'),
|
||||||
|
style: OutlinedButton.styleFrom(
|
||||||
|
minimumSize: const Size(double.infinity, 48),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+2
-2
@@ -7,7 +7,7 @@ class AppVersion {
|
|||||||
|
|
||||||
static const String name = 'tatlock_ui';
|
static const String name = 'tatlock_ui';
|
||||||
static const String description = 'Tatlock - a Home Lab AI';
|
static const String description = 'Tatlock - a Home Lab AI';
|
||||||
static const String version = '0.3.3';
|
static const String version = '1.0.4';
|
||||||
static const int buildNumber = 1;
|
static const int buildNumber = 1;
|
||||||
static const String fullVersion = '0.3.3+1';
|
static const String fullVersion = '1.0.4+1';
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
|||||||
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
||||||
# In Windows, build-name is used as the major, minor, and patch parts
|
# In Windows, build-name is used as the major, minor, and patch parts
|
||||||
# of the product and file versions while build-number is used as the build suffix.
|
# of the product and file versions while build-number is used as the build suffix.
|
||||||
version: 1.0.3+1
|
version: 1.0.5+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.10.4
|
sdk: ^3.10.4
|
||||||
@@ -52,6 +52,8 @@ dependencies:
|
|||||||
|
|
||||||
# Authentication (OIDC/OAuth2)
|
# Authentication (OIDC/OAuth2)
|
||||||
flutter_appauth: ^8.0.0
|
flutter_appauth: ^8.0.0
|
||||||
|
crypto: ^3.0.3
|
||||||
|
web: ^1.1.0
|
||||||
|
|
||||||
# UI
|
# UI
|
||||||
flex_color_scheme: ^8.1.0
|
flex_color_scheme: ^8.1.0
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env dart
|
||||||
|
// Generates web/health.json from pubspec.yaml
|
||||||
|
// Run: dart run tool/generate_health_json.dart
|
||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:yaml/yaml.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final pubspecFile = File('pubspec.yaml');
|
||||||
|
if (!pubspecFile.existsSync()) {
|
||||||
|
stderr.writeln('Error: pubspec.yaml not found');
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
final pubspecContent = pubspecFile.readAsStringSync();
|
||||||
|
final pubspec = loadYaml(pubspecContent) as YamlMap;
|
||||||
|
|
||||||
|
final name = pubspec['name'] as String;
|
||||||
|
final description = pubspec['description'] as String? ?? '';
|
||||||
|
final versionString = pubspec['version'] as String;
|
||||||
|
|
||||||
|
// Parse version: "1.0.3+1" -> version="1.0.3", buildNumber=1
|
||||||
|
final versionParts = versionString.split('+');
|
||||||
|
final version = versionParts[0];
|
||||||
|
final buildNumber = versionParts.length > 1 ? int.parse(versionParts[1]) : 0;
|
||||||
|
|
||||||
|
final health = {
|
||||||
|
'status': 'healthy',
|
||||||
|
'name': name,
|
||||||
|
'title': description,
|
||||||
|
'version': version,
|
||||||
|
'buildNumber': buildNumber,
|
||||||
|
'fullVersion': '$version+$buildNumber',
|
||||||
|
};
|
||||||
|
|
||||||
|
final webDir = Directory('web');
|
||||||
|
if (!webDir.existsSync()) {
|
||||||
|
webDir.createSync(recursive: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
final healthFile = File('web/health.json');
|
||||||
|
healthFile.writeAsStringSync(
|
||||||
|
const JsonEncoder.withIndent(' ').convert(health),
|
||||||
|
);
|
||||||
|
|
||||||
|
print('Generated web/health.json with version $version+$buildNumber');
|
||||||
|
}
|
||||||
+25
-2
@@ -1,7 +1,30 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>OK</title>
|
<title>Health Check</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: monospace; padding: 20px; background: #1a1a1a; color: #0f0; }
|
||||||
|
.healthy { color: #0f0; }
|
||||||
|
.error { color: #f00; }
|
||||||
|
pre { background: #222; padding: 15px; border-radius: 5px; }
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>OK</body>
|
<body>
|
||||||
|
<h1 id="status">Loading...</h1>
|
||||||
|
<pre id="data"></pre>
|
||||||
|
<script>
|
||||||
|
fetch('/health.json')
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(data => {
|
||||||
|
document.getElementById('status').textContent = data.status?.toUpperCase() || 'OK';
|
||||||
|
document.getElementById('status').className = data.status === 'healthy' ? 'healthy' : 'error';
|
||||||
|
document.getElementById('data').textContent = JSON.stringify(data, null, 2);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
document.getElementById('status').textContent = 'ERROR';
|
||||||
|
document.getElementById('status').className = 'error';
|
||||||
|
document.getElementById('data').textContent = err.message;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"status": "healthy",
|
||||||
|
"name": "tatlock_ui",
|
||||||
|
"title": "Tatlock - a Home Lab AI",
|
||||||
|
"version": "1.0.4",
|
||||||
|
"buildNumber": 1,
|
||||||
|
"fullVersion": "1.0.4+1"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user