-
Notifications
You must be signed in to change notification settings - Fork 50
CredentialsManager user info/ID token contents accessible via flutter SDK #607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.auth0.auth0_flutter.request_handlers.credentials_manager | ||
|
||
import android.content.Context | ||
import com.auth0.android.authentication.storage.CredentialsManagerException | ||
import com.auth0.android.authentication.storage.SecureCredentialsManager | ||
import com.auth0.auth0_flutter.request_handlers.MethodCallRequest | ||
import io.flutter.plugin.common.MethodChannel | ||
import java.io.Serializable | ||
import java.lang.Exception | ||
|
||
|
||
class GetIdTokenContentRequestHandler: CredentialsManagerRequestHandler { | ||
override val method: String = "credentialsManager#getUserInfo" | ||
override fun handle( | ||
credentialsManager: SecureCredentialsManager, | ||
context: Context, | ||
request: MethodCallRequest, | ||
result: MethodChannel.Result | ||
) { | ||
result.success( | ||
mapOf( | ||
"id" to credentialsManager.userProfile?.getId(), | ||
"name" to credentialsManager.userProfile?.name, | ||
"nickname" to credentialsManager.userProfile?.nickname, | ||
"pictureURL" to credentialsManager.userProfile?.pictureURL, | ||
"email" to credentialsManager.userProfile?.email, | ||
"isEmailVerified" to credentialsManager.userProfile?.isEmailVerified, | ||
"familyName" to credentialsManager.userProfile?.familyName, | ||
"createdAt" to credentialsManager.userProfile?.createdAt, | ||
"identities" to credentialsManager.userProfile?.getIdentities()?.map { | ||
mapOf( | ||
"provider" to it.provider, | ||
"id" to it.connection, | ||
"isSocial" to it.isSocial, | ||
"accessToken" to it.accessToken, | ||
"accessTokenSecret" to it.accessTokenSecret, | ||
"profileInfo" to it.getProfileInfo() | ||
) | ||
}, | ||
"extraInfo" to credentialsManager.userProfile?.getExtraInfo(), | ||
"userMetadata" to credentialsManager.userProfile?.getUserMetadata(), | ||
"appMetadata" to credentialsManager.userProfile?.getAppMetadata(), | ||
"givenName" to credentialsManager.userProfile?.givenName | ||
) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
import Auth0 | ||
|
||
#if os(iOS) | ||
import Flutter | ||
#else | ||
import FlutterMacOS | ||
#endif | ||
|
||
struct CredentialsManagerUserInfoMethodHandler: MethodHandler { | ||
let credentialsManager: CredentialsManager | ||
|
||
func handle(with arguments: [String: Any], callback: @escaping FlutterResult) { | ||
callback(credentialsManager.user) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
import Auth0 | ||
|
||
#if os(iOS) | ||
import Flutter | ||
#else | ||
import FlutterMacOS | ||
#endif | ||
|
||
struct CredentialsManagerUserInfoMethodHandler: MethodHandler { | ||
let credentialsManager: CredentialsManager | ||
|
||
func handle(with arguments: [String: Any], callback: @escaping FlutterResult) { | ||
callback(credentialsManager.user) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,9 @@ abstract class CredentialsManager { | |
final Map<String, String> parameters = const {}, | ||
}); | ||
|
||
Future<bool> storeCredentials(final Credentials credentials); | ||
Future<UserInfo> getIDTokenContents(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be of nullable type ,in the scneario if the credentials aren't available and someone invokes this api ? |
||
|
||
Future<bool> storeCredentials(final Credentials credentials); | ||
|
||
Future<bool> hasValidCredentials({ | ||
final int minTtl = 0, | ||
|
@@ -55,6 +57,10 @@ class DefaultCredentialsManager extends CredentialsManager { | |
parameters: parameters, | ||
))); | ||
|
||
@override | ||
Future<UserInfo> getIDTokenContents() => | ||
CredentialsManagerPlatform.instance.getIDTokenContents(_createApiRequest(null)); | ||
|
||
/// Stores the given credentials in the storage. Must have an `access_token` | ||
/// or `id_token` and a `expires_in` value. | ||
@override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
// coverage:ignore-file | ||
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; | ||
|
||
import '../credentials.dart'; | ||
import '../request/request.dart'; | ||
import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart'; | ||
import 'method_channel_credentials_manager.dart'; | ||
import 'options/get_credentials_options.dart'; | ||
import 'options/has_valid_credentials_options.dart'; | ||
|
@@ -36,6 +35,11 @@ abstract class CredentialsManagerPlatform extends PlatformInterface { | |
throw UnimplementedError('getCredentials() has not been implemented'); | ||
} | ||
|
||
/// Retrieves the credentials from the native storage. | ||
Future<UserInfo> getIDTokenContents(final CredentialsManagerRequest request) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
throw UnimplementedError('getIDTokenContents() has not been implemented'); | ||
} | ||
|
||
/// Removes the credentials from the native storage if present. | ||
Future<bool> clearCredentials(final CredentialsManagerRequest request) { | ||
throw UnimplementedError('clearCredentials() has not been implemented'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
class UserIdentity { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a User_Profile class defined . Please re use that |
||
final String id; | ||
final String connection; | ||
final String provider; | ||
final bool? isSocial; | ||
final String? accessToken; | ||
final String? accessTokenSecret; | ||
final Map<String, dynamic>? _profileInfo; | ||
|
||
UserIdentity({ | ||
required this.id, | ||
required this.connection, | ||
required this.provider, | ||
this.isSocial, | ||
this.accessToken, | ||
this.accessTokenSecret, | ||
Map<String, dynamic>? profileInfo}) | ||
: _profileInfo = profileInfo ; | ||
factory UserIdentity.fromJson(final Map<String, dynamic> json) | ||
=> UserIdentity( | ||
connection: json['connection'] as String, | ||
id: json['id'] as String, | ||
isSocial: json['isSocial'] as bool?, | ||
provider: json['provider'] as String, | ||
accessToken: json['accessToken'] as String?, | ||
accessTokenSecret: json['accessTokenSecret'] as String?, | ||
profileInfo: json['profileInfo'] as Map<String, dynamic>? | ||
); | ||
} | ||
|
||
class UserInfo { | ||
final String? _id; | ||
final List<UserIdentity>? _identities; | ||
final Map<String, dynamic>? _extraInfo; | ||
final Map<String, dynamic>? _userMetadata; | ||
final Map<String, dynamic>? _appMetadata; | ||
|
||
final String? name; | ||
final String? nickname; | ||
final String? pictureURL; | ||
final String? email; | ||
final bool? isEmailVerified; | ||
final String? familyName; | ||
final DateTime? createdAt; | ||
final String? givenName; | ||
|
||
|
||
UserInfo({ | ||
final String? id, | ||
this.name, | ||
this.nickname, | ||
this.pictureURL, | ||
this.email, | ||
this.isEmailVerified, | ||
this.familyName, | ||
this.createdAt, | ||
final List<UserIdentity>? identities, | ||
final Map<String, dynamic>? extraInfo, | ||
final Map<String, dynamic>? userMetadata, | ||
final Map<String, dynamic>? appMetadata, | ||
this.givenName, | ||
}) | ||
: _id = id, | ||
_identities = identities, | ||
_extraInfo = extraInfo, | ||
_userMetadata = userMetadata, | ||
_appMetadata = appMetadata; | ||
|
||
String? getId() { | ||
if (_id != null) { | ||
return _id; | ||
} | ||
return (_extraInfo != null && _extraInfo.containsKey('sub')) | ||
? _extraInfo['sub'] as String? | ||
: null; | ||
} | ||
|
||
Map<String, dynamic> getUserMetadata() => | ||
_userMetadata ?? {}; | ||
Map<String, dynamic> getAppMetadata() => | ||
_appMetadata ?? {}; | ||
|
||
List<UserIdentity> getIdentities() => | ||
_identities ?? []; | ||
|
||
Map<String, dynamic> getExtraInfo() => | ||
_extraInfo ?? {}; | ||
|
||
factory UserInfo.fromJson(final Map<String, dynamic> json) => | ||
UserInfo( | ||
id: json['id'] as String?, | ||
name: json['name'] as String?, | ||
nickname: json['nickname'] as String?, | ||
pictureURL: json['pictureURL'] as String?, | ||
email: json['email'] as String?, | ||
isEmailVerified: json['isEmailVerified'] as bool?, | ||
familyName: json['familyName'] as String?, | ||
// Handle date parsing | ||
createdAt: json['createdAt'] != null | ||
? DateTime.parse(json['createdAt'] as String) | ||
: null, | ||
// Handle list of UserIdentity | ||
identities: (json['identities'] as List<dynamic>?) | ||
?.map((e) => UserIdentity.fromJson(e as Map<String, dynamic>)) | ||
.toList(), | ||
extraInfo: Map<String, dynamic>.from(json['extraInfo'] as Map), | ||
userMetadata: Map<String, dynamic>.from(json['userMetadata'] as Map), | ||
appMetadata: Map<String, dynamic>.from(json['appMetadata'] as Map), | ||
givenName: json['givenName'] as String?, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of symlinks missing. See https://github.com/auth0/auth0-flutter/actions/runs/15893748250/job/44821016570?pr=607