credential_manager 0.0.2 copy "credential_manager: ^0.0.2" to clipboard
credential_manager: ^0.0.2 copied to clipboard

Credential Manager plugin,Help one-tap Login functionality and store credentials in Google service account of user.

Credential Manager #

Credential Manager is a Jetpack API that supports multiple sign-in methods, such as username and password, passkeys, and federated sign-in solutions (such as Sign-in with Google) in a single API, thus simplifying the integration for developers.

Furthermore, for users, Credential Manager unifies the sign-in interface across authentication methods, making it clearer and easier for users to sign into apps, regardless of the method they choose

Note this package is only supported for android

Getting Started #

  • Add the dependency to your pubspec.yaml file

    dependencies:
       credential_manager: <latest_version>
    

    --------------------------------------------OR------------------------------------------------

    flutter pub get credential_manager
    

Setup android #

  • Add proguard rules if proguard-rules.pro is not exist in android/app then create new file with same name
 -if class androidx.credentials.CredentialManager
-keep class androidx.credentials.playservices.** {
  *;
}

  • Update android\app\build.gradle
buildTypes {
        release {
            //add these lines 
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

Usage in flutter #

  • import the package

    import 'package:credential_manager/credential_manager dart';
    
  • Create object for CredentialManager()

    CredentialManager credentialManager = CredentialManager();
    
  • Check if target platform is supported or not

if(credentialManager.isSupportedPlatform){
    // if supported
}
  • initialize credential manager
if(credentialManager.isSupportedPlatform){
    // if supported
    await credentialManager.init(preferImmediatelyAvailableCredentials: true);

}

Note: If the call to Credential Manager was triggered by an explicit user action, credential will be available immediately after saving if true(by default) or user will not able to get credential as soon as possible(May throw error if fetched just after saving credentials)

If you want to encrypted credential manager encrypted_credential_manager

  • Save credentials in credential manager
await credentialManager.savePasswordCredentials(
          PasswordCredential(username: username, password: password));
Save Credential 1 Save Credential 2 Save Credential 3 Save Credential 4

Note: It is not nesscessary to be username and password,you can send any string you want to store but you will get credentials as PasswordCredential which has username and password properties.

  • Get the saved credentials
 PasswordCredential credential =await credentialManager.getPasswordCredentials();
Get Credential 1 Get Credential 2

Encrypt Your credentials #

To ensure the security of credentials, we will encrypt the password field using the encrypt library and store the encrypted information in a credential manager. This approach reduces the risk of exposing sensitive information, such as passwords.

To encrypt value and decrypt the encrypted data, you need 128 bit-key of secret_key and 128 bit-key of iv_key.

you need to provide a 16 character string as a key.


  final secretKey = "<Secret-key>"; // Use a secure key here for example "1234567812345678"
  final ivKey = "<16-bit-iv-key>" //for e.g: "xfpkDQJXIfb3mcnb"; 
  • Save encrypted credentials
await credentialManager.saveEncryptedCredentials(
        credential: PasswordCredential(username: username, password: password),
        secretKey: secretKey,
        ivKey: ivKey,
      );
  • Get encrypted credentials and decrypted the sensitive data
 PasswordCredential credential = await credentialManager
          .getEncryptedCredentials(secretKey: secretKey, ivKey: ivKey);

Properties and Methods #

Method Type Description
isSupportedPlatform boolean Check if targeted platform supported or not(Only Android supported)
init(bool preferImmediatelyAvailableCredentials) Future(void) To initialize credential Manager,preferImmediatelyAvailableCredentials,If the call to Credential Manager was triggered by an explicit user action, credential will be available immediately after saving if true(by default) or user will not able to get credential as soon as possible(May throw error if fetched just after saving credentials)
savePasswordCredentials(PasswordCredential credential) Future(void) To save credentials in credential Manager
saveEncryptedCredentials(PasswordCredential credential,String secretKey,String ivKey) Future(void) To save credentials in credential Manager with encryption
getPasswordCredentials() Future(PasswordCredential) return PasswordCredential object which has username,password fields
getEncryptedCredentials(String secretKey,String ivKey) Future(PasswordCredential) return PasswordCredential object which has username,password fields and decrypt the encrypted data

About PasswordCredential() #

Field Type Description
username String? User's username for authentication.
password String? User's password for authentication.
Constructor:
PasswordCredential({String? username, String? password}) Creates a new instance of PasswordCredential.
Properties:
String? get username => _username; Retrieves the username.
set username(String? username) => _username = username; Sets the username.
String? get password => _password; Retrieves the password.
set password(String? password) => _password = password; Sets the password.
JSON Serialization/Deserialization:
PasswordCredential.fromJson(Map<String, dynamic> json) Creates an instance from a JSON map.
Map<String, dynamic> toJson() Converts the instance to a JSON map.

Error Handling #

if any exception occure it throws CredentialException which has field int code and String message.

Code Message Description
101 Initialization failure The initialization process encountered an error.
102 Plugin exception An exception occurred within the plugin.
103 Not implemented The functionality is not implemented.
201 Login cancelled The login process was cancelled by the user.
202 No credentials found No valid credentials were found for authentication.
203 Mismatched credentials The provided credentials do not match the expected format.
204 Login failed The login attempt was unsuccessful.
301 Save Credentials cancelled The process of saving credentials was cancelled by the user.
302 Create Credentials failed Failed to create new credentials.
401 Encryption failed Failed to encrypt value.
402 Decryption failed Failed to decrypt value.

Upcoming #

  • iOS Support
  • Passkey extenstion for flowless integration with web

Contributing #

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request
  • If you have any questions, please open an issue.
  • If you found a bug, please open an issue.
  • If you have a feature request, please open an issue.
  • If you want to contribute, please submit a pull request.
  • If you use this package, please add a star on GitHub.
  • If you use this package, please tweet about it.
  • If you use this package, please mention it in a blog post.
19
likes
0
pub points
83%
popularity

Publisher

verified publishersmkwinner.live

Credential Manager plugin,Help one-tap Login functionality and store credentials in Google service account of user.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

encrypt, flutter, plugin_platform_interface

More

Packages that depend on credential_manager