add basic oauth methods

This commit is contained in:
zoe 2022-06-28 22:05:24 +02:00
parent 387d58dfa2
commit beaa70fe8b
15 changed files with 351 additions and 9 deletions

View 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);
}

View file

View file

@ -0,0 +1,38 @@
bool isValidUsername({required String name}) {
if (name.isEmpty) {
return false;
}
return RegExp(r".+\@.+\..+").hasMatch(cleanUpUsername(name: name));
}
String cleanUpUsername({required String name}) {
name = name.replaceAll(" ", "");
if (name.isNotEmpty) {
if (name[0] == "@") {
name = name.substring(1);
}
}
return name;
}
Uri urlFromUsername({required String name}) {
name = cleanUpUsername(name: name);
return Uri.parse("https://${name.substring(name.indexOf("@") + 1)}");
}
String userFromUsername({required String name}) {
name = cleanUpUsername(name: name);
return name.substring(0, name.indexOf("@"));
}
// A fully qualified and valid username for example (@)hello@world.com
// The first @ is ignored
class Username {
late final String user;
late final Uri url;
Username(String username) {
assert(isValidUsername(name: username));
user = userFromUsername(name: username);
url = urlFromUsername(name: username);
}
}

View file

@ -0,0 +1 @@