From af67fa2366e14d337ad11ecda82e654eab9d1cf4 Mon Sep 17 00:00:00 2001 From: Francesco Romeo Date: Fri, 27 May 2022 13:03:20 +0200 Subject: [PATCH] custom claim is in progress --- vidaia/lib/models/auth0_user.dart | 3 +- vidaia/lib/models/auth0_user.g.dart | 4 +-- vidaia/lib/pages/home/home_page_loader.dart | 2 +- vidaia/lib/pages/login_page.dart | 3 +- vidaia/lib/services/auth_service.dart | 35 ++++++++++++++++----- vidaia/pubspec.lock | 11 +++++-- vidaia/pubspec.yaml | 8 ++--- 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/vidaia/lib/models/auth0_user.dart b/vidaia/lib/models/auth0_user.dart index 10cbd08..ebdd314 100644 --- a/vidaia/lib/models/auth0_user.dart +++ b/vidaia/lib/models/auth0_user.dart @@ -10,6 +10,7 @@ class Auth0User { required this.picture, required this.updatedAt, required this.sub, + this.walletAddress = '', }); final String nickname; final String name; @@ -18,7 +19,7 @@ class Auth0User { @JsonKey(name: 'updated_at') final String updatedAt; - @JsonKey(name: 'https://vidaia.saynode.ch/wallet_address') + @JsonKey(name: 'https://vidaia.saynode.ch/wallet_address', defaultValue: '', includeIfNull: true) String walletAddress = ''; // userID getter to understand it easier diff --git a/vidaia/lib/models/auth0_user.g.dart b/vidaia/lib/models/auth0_user.g.dart index c53fb10..ec14baa 100644 --- a/vidaia/lib/models/auth0_user.g.dart +++ b/vidaia/lib/models/auth0_user.g.dart @@ -13,8 +13,8 @@ Auth0User _$Auth0UserFromJson(Map json) => Auth0User( picture: json['picture'] as String, updatedAt: json['updated_at'] as String, sub: json['sub'] as String, - )..walletAddress = - json['https://vidaia.saynode.ch/wallet_address'] as String; + walletAddress: json['https://vidaia.saynode.ch/wallet_address'] as String? ?? '', + ); Map _$Auth0UserToJson(Auth0User instance) => { 'nickname': instance.nickname, diff --git a/vidaia/lib/pages/home/home_page_loader.dart b/vidaia/lib/pages/home/home_page_loader.dart index a0eb3b2..59659e0 100644 --- a/vidaia/lib/pages/home/home_page_loader.dart +++ b/vidaia/lib/pages/home/home_page_loader.dart @@ -51,7 +51,7 @@ class _HomePage2State extends State { style: TextStyle(fontSize: 20), ), ); - } else if (AuthService.instance.profile == null || AuthService.instance.profile!.id.isEmpty) { + } else if (AuthService.instance.profile == null || AuthService.instance.profile.id.isEmpty) { return const LoginPage(); } else { return Center( diff --git a/vidaia/lib/pages/login_page.dart b/vidaia/lib/pages/login_page.dart index cdbbbff..909310b 100644 --- a/vidaia/lib/pages/login_page.dart +++ b/vidaia/lib/pages/login_page.dart @@ -147,7 +147,8 @@ class _LoginPageState extends State { if (AuthService.instance.profile.walletAddress.isEmpty) { AuthService.instance.profile.walletAddress = await createNewWallet(); - await AuthService.instance.updateUserWalletAddress(AuthService.instance.profile.walletAddress); + debugPrint('created wallet address is: ' + AuthService.instance.profile.walletAddress); + await AuthService.instance.updateUserWalletAddress(AuthService.instance.profile); } else { global.address = AuthService.instance.profile.walletAddress; } diff --git a/vidaia/lib/services/auth_service.dart b/vidaia/lib/services/auth_service.dart index 35644c3..54b9502 100644 --- a/vidaia/lib/services/auth_service.dart +++ b/vidaia/lib/services/auth_service.dart @@ -1,12 +1,16 @@ import 'dart:convert'; +import 'dart:html'; +import 'dart:math'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import 'package:flutter/services.dart'; import 'package:flutter_appauth/flutter_appauth.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:thor_devkit_dart/utils.dart'; import 'package:vidaia/helpers/constants.dart'; import 'package:vidaia/models/auth0_id_token.dart'; import 'package:vidaia/models/auth0_user.dart'; +import 'package:pkce/pkce.dart'; class AuthService { Auth0User profile = Auth0User(nickname: '', name: '', email: '', picture: '', updatedAt: '', sub: ''); @@ -79,7 +83,7 @@ class AuthService { AUTH0_CLIENT_ID, AUTH0_REDIRECT_URI, issuer: AUTH0_ISSUER, - scopes: ['openid', 'profile', 'email', 'offline_access'], + scopes: ['openid', 'profile', 'email', 'offline_access', 'update:current_user_metadata', 'create:current_user_metadata'], promptValues: ['login'], /// possible values login, none, consent, select_account @@ -162,22 +166,39 @@ class AuthService { } } - Future updateUserWalletAddress(String walletAddress) async { + Future updateUserWalletAddress(Auth0User auth0user) async { + /// experiments to get the Management API token + final pkcePair = PkcePair.generate(length: 32); + final code_challenge = pkcePair.codeChallenge; + final code_verifier = pkcePair.codeVerifier; + final managementApiTokenUrl = '$AUTH0_DOMAIN/authorize?response_type=code&code_challenge=$code_challenge&code_challenge_method=S256&client_id=$AUTH0_CLIENT_ID&redirect_uri=$AUTH0_REDIRECT_URI&audience=dev-jp7b9rk6.us.auth0.com'; + + /// + final url = Uri.https( AUTH0_DOMAIN, - '/userinfo', + '/api/v2/users/' + auth0user.id, ); + debugPrint('update wallet request uri.https - ' + url.toString()); + debugPrint('update wallet auth0AccessToken - $auth0AccessToken'); + debugPrint('update wallet request uri.https - $idToken'); + final responseAuth0UserInfo = await http.patch(url, headers: { 'Authorization': 'Bearer $auth0AccessToken' }, body: { - "user_metadata": {"wallet_address": walletAddress} - }).catchError((onError) { - debugPrint(onError); + "user_metadata": jsonEncode({"wallet_address": auth0user.walletAddress}) }); + // .catchError((onError) { + // debugPrint('error while updating the user wallet address on Auth0'); + // }); if (responseAuth0UserInfo.statusCode == 200) { + debugPrint('update wallet response 200'); return true; + } else { + debugPrint('update wallet response ' + responseAuth0UserInfo.statusCode.toString()); + debugPrint('update wallet response body' + responseAuth0UserInfo.body); + return false; } - return false; } } diff --git a/vidaia/pubspec.lock b/vidaia/pubspec.lock index 5c6da40..eb48cd6 100644 --- a/vidaia/pubspec.lock +++ b/vidaia/pubspec.lock @@ -233,12 +233,12 @@ packages: source: hosted version: "1.0.3" crypto: - dependency: transitive + dependency: "direct main" description: name: crypto url: "https://pub.dartlang.org" source: hosted - version: "3.0.1" + version: "3.0.2" csslib: dependency: transitive description: @@ -644,6 +644,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.5" + pkce: + dependency: "direct main" + description: + name: pkce + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0+1" platform: dependency: transitive description: diff --git a/vidaia/pubspec.yaml b/vidaia/pubspec.yaml index 437053f..de6a496 100644 --- a/vidaia/pubspec.yaml +++ b/vidaia/pubspec.yaml @@ -36,15 +36,11 @@ dependencies: flutter_secure_storage: ^5.0.2 http: ^0.13.4 flutter_appauth: ^4.0.0 - - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 auto_route: ^3.2.4 google_fonts: ^2.3.1 - - - # Change this once the devkit is public + crypto: ^3.0.2 + pkce: ^1.1.0+1 thor_devkit_dart: path: assets/packages/thor_devkit_dart