Skip to content

Commit

Permalink
custom claim is in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
romme86 committed May 27, 2022
1 parent 3243d28 commit af67fa2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
3 changes: 2 additions & 1 deletion vidaia/lib/models/auth0_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Auth0User {
required this.picture,
required this.updatedAt,
required this.sub,
this.walletAddress = '',
});
final String nickname;
final String name;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vidaia/lib/models/auth0_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vidaia/lib/pages/home/home_page_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _HomePage2State extends State<HomePage2> {
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(
Expand Down
3 changes: 2 additions & 1 deletion vidaia/lib/pages/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class _LoginPageState extends State<LoginPage> {

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;
}
Expand Down
35 changes: 28 additions & 7 deletions vidaia/lib/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -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: '');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -162,22 +166,39 @@ class AuthService {
}
}

Future<bool> updateUserWalletAddress(String walletAddress) async {
Future<bool> 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;
}
}
11 changes: 9 additions & 2 deletions vidaia/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions vidaia/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit af67fa2

Please sign in to comment.