add basic oauth methods
This commit is contained in:
parent
387d58dfa2
commit
beaa70fe8b
15 changed files with 351 additions and 9 deletions
53
lib/business_logic/auth/oauth.dart
Normal file
53
lib/business_logic/auth/oauth.dart
Normal file
|
@ -0,0 +1,53 @@
|
|||
import 'package:http/http.dart' as http;
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import '../../global.dart' as global;
|
||||
|
||||
class App {
|
||||
late String clientSecret;
|
||||
late String clientId;
|
||||
late String id;
|
||||
late String name;
|
||||
late String website;
|
||||
late String redirectUri;
|
||||
App(
|
||||
{required this.clientSecret,
|
||||
required this.clientId,
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.website,
|
||||
required this.redirectUri});
|
||||
factory App.fromJson(Map<String, dynamic> json) {
|
||||
return App(
|
||||
id: json["id"].toString(),
|
||||
name: json["name"].toString(),
|
||||
website: json["website"].toString(),
|
||||
redirectUri: json["redirect_uri"].toString(),
|
||||
clientId: json["client_id"].toString(),
|
||||
clientSecret: json["client_secret"].toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<http.Response> registerApp(String baseurl) async {
|
||||
//String url = baseurl Uri."api/v1/apps";
|
||||
Uri url = Uri.https(baseurl, "/api/v1/apps");
|
||||
var response = await http.post(url, headers: global.defaultHeaders, body: {
|
||||
'client_name': global.name,
|
||||
'redirect_uris': "ietf:wg:oauth:2.0:oob",
|
||||
'scopes': "read write",
|
||||
'website': global.website
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
void openBrowserForAuthCode(String baseurl, App app) {
|
||||
Uri url = Uri.https(
|
||||
baseurl,
|
||||
// ignore: prefer_interpolation_to_compose_strings
|
||||
"/oauth/authorize" +
|
||||
"?client_id=" +
|
||||
app.clientId +
|
||||
"&scope=read+write" +
|
||||
"&redirect_uri=urn:ietf:wg:oauth:2.0:oob" +
|
||||
"&response_type=code");
|
||||
launchUrl(url);
|
||||
}
|
9
lib/global.dart
Normal file
9
lib/global.dart
Normal file
|
@ -0,0 +1,9 @@
|
|||
const String name = "slothmu";
|
||||
const String version = "v0.1 'not even alpha'";
|
||||
const String useragent = "$name/$version";
|
||||
const String website = "https://git.kittycat.homes/zoe/slothmu";
|
||||
const Map<String, String> defaultHeaders = {
|
||||
"User-Agent": useragent,
|
||||
"accept": "application/json",
|
||||
"Content-Type": "application/json"
|
||||
};
|
|
@ -1,10 +1,15 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:localization/localization.dart';
|
||||
import 'package:slothmu/partials/main_scaffold.dart';
|
||||
import 'pages/login.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
|
||||
void main() => runApp(const Slothmu());
|
||||
void main() {
|
||||
Intl.defaultLocale = 'en_US';
|
||||
|
||||
runApp(const Slothmu());
|
||||
}
|
||||
|
||||
class Slothmu extends StatefulWidget {
|
||||
const Slothmu({Key? key}) : super(key: key);
|
||||
|
@ -14,15 +19,15 @@ class Slothmu extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _SlothmuState extends State<Slothmu> {
|
||||
List<Locale> supported = const [
|
||||
Locale("en", "US"),
|
||||
Locale("de", "DE"),
|
||||
];
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
LocalJsonLocalization.delegate.directories = ['lib/i18n'];
|
||||
return MaterialApp(
|
||||
supportedLocales: const [
|
||||
Locale("en", "US"),
|
||||
Locale("de", "DE"),
|
||||
],
|
||||
locale: const Locale("de", "DE"),
|
||||
supportedLocales: supported,
|
||||
localizationsDelegates: [
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:slothmu/api/user.dart';
|
||||
import 'package:slothmu/business_logic/user.dart';
|
||||
import 'package:localization/localization.dart';
|
||||
import '../business_logic/auth/oauth.dart' as oauth;
|
||||
|
||||
class Login extends StatefulWidget {
|
||||
const Login({Key? key}) : super(key: key);
|
||||
|
@ -64,6 +65,13 @@ class _LoginFormState extends State<LoginForm> {
|
|||
if (!isValid) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text("login-failed-snackbar-text".i18n())));
|
||||
} else {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
const AuthPage(baseurl: "kittycat.homes"),
|
||||
));
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.login),
|
||||
|
@ -73,3 +81,21 @@ class _LoginFormState extends State<LoginForm> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthPage extends StatefulWidget {
|
||||
const AuthPage({Key? key, required String baseurl}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AuthPage> createState() => _AuthPageState();
|
||||
}
|
||||
|
||||
class _AuthPageState extends State<AuthPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Text("hello"),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue