Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dutchcodingcompany/oauth_chopper
Add and manage OAuth2 authentication for your Chopper client.
https://github.com/dutchcodingcompany/oauth_chopper
Last synced: 2 months ago
JSON representation
Add and manage OAuth2 authentication for your Chopper client.
- Host: GitHub
- URL: https://github.com/dutchcodingcompany/oauth_chopper
- Owner: DutchCodingCompany
- License: mit
- Created: 2022-09-14T12:23:40.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-16T10:15:06.000Z (5 months ago)
- Last Synced: 2024-08-17T10:33:01.995Z (5 months ago)
- Language: Dart
- Size: 102 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Add and manage OAuth2 authentication for your Chopper client
## Features
Offers a `oauth_chopper` client to help manage your OAuth2 authentication
with [Choppper](https://pub.dev/packages/chopper). The `oauth_chopper` client
uses [oauth2](https://pub.dev/packages/oauth2) package from the dart team and combines this with
Chopper. It offers a Chopper Authenticator and HeaderInterceptor to manage the OAuth2
authorizations.By default it doesn't persist any credential information. It uses an in memory storage by default.
This can be override by providing a custom storage implementation.**Currently it supports the following grants:**
- ✅ ResourceOwnerPasswordGrant
- ✅ ClientCredentialsGrant
- ✅ AuthorizationCodeGrant## Usage
Create a `oauth_chopper` client with the needed authorizationEndpoint, identifier and secret.
Add the `oauth_chopper_interceptor` to your chopper client.
Request a OAuthGrant on the `oauth_chopper` client.Example:
```dart
/// Create OAuthChopper instance.
final oauthChopper = OAuthChopper(
authorizationEndpoint: authorizationEndpoint,
identifier: identifier,
secret: secret,
);/// Add the oauth authenticator and interceptor to the chopper client.
final chopperClient = ChopperClient(
baseUrl: Uri.parse('https://example.com'),
interceptors: [
oauthChopper.interceptor(),
],
);/// Request grant
oauthChopper.requestGrant(
ResourceOwnerPasswordGrant(
username: 'username',
password: 'password',
),
);// Authorization Cod eGrant
oauth_chopper.AuthorizationCodeGrant grant = oauth_chopper.AuthorizationCodeGrant(
tokenEndpoint: Uri.parse("tokenEndpoint"),
scopes: ["scope1", "scope2"],
redirectUrl: Uri.parse("redirectUrl"),
redirect: redirect,
listen: listen,
);
```If you want to persist the OAuth2 credential information you can provide a custom OAuthStorage
implementation for example
with [flutter_secure_storage](https://pub.dev/packages/flutter_secure_storage):```dart
const _storageKey = 'storage_key';
class OAuthCredentialsStorage implements OAuthStorage {
final FlutterSecureStorage _storage;
const OAuthCredentialsStorage(this._storage);
@override
FutureOr clear() async {
await _storage.delete(key: _storageKey);
}
@override
FutureOr fetchCredentials() async {
final credentialsJson = await _storage.read(key: _storageKey);
return credentialsJson;
}
@override
FutureOr saveCredentials(String? credentialsJson) async {
await _storage.write(key: _storageKey, value: credentialsJson);
} }
```## Additional information
- [OAuth2](https://oauth.net/2/)
- [OAuth2 package](https://pub.dev/packages/oauth2)
- [Chopper package](https://pub.dev/packages/chopper)Feel free to give me any feedback to improve this package.