Skip to content

Commit

Permalink
Merge pull request #21 from DutchCodingCompany/feature/upgrade_choppe…
Browse files Browse the repository at this point in the history
…r_v8

Release v1
  • Loading branch information
Guldem authored May 15, 2024
2 parents a84c139 + 879fd83 commit 6277c30
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 275 deletions.
19 changes: 3 additions & 16 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,17 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

# Note: This workflow uses the latest stable version of the Dart SDK.
# You can specify other versions if desired, see documentation here:
# https://github.com/dart-lang/setup-dart/blob/main/README.md
# - uses: dart-lang/setup-dart@v1
- uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd603
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1

- name: Install dependencies
run: dart pub get

- name: Check code format
run: dart format . -o none --set-exit-if-changed

# Uncomment this step to verify the use of 'dart format' on each commit.
# - name: Verify formatting
# run: dart format --output=none --set-exit-if-changed .

# Consider passing '--fatal-infos' for slightly stricter analysis.
- name: Analyze project source
run: dart analyze
run: dart analyze --fatal-infos .

# Your project will need to have tests in test/ and a dependency on
# package:test for this step to succeed. Note that Flutter projects will
# want to change this to 'flutter test'.
- name: Run tests
run: dart test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0
- Updated chopper to v8.0.0
- **BREAKING** Removed oauth_chopper authenticator. Now only the interceptor is needed.

## 0.4.0
- ✨ Add very good analysis by @Guldem in https://github.com/DutchCodingCompany/oauth_chopper/pull/17
- ✨ Add custom client by @Guldem in https://github.com/DutchCodingCompany/oauth_chopper/pull/18
Expand Down
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Makefile

help:
@printf "%-20s %s\n" "Target" "Description"
@printf "%-20s %s\n" "------" "-----------"
@make -pqR : 2>/dev/null \
| awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' \
| sort \
| egrep -v -e '^[^[:alnum:]]' -e '^$@$$' \
| xargs -I _ sh -c 'printf "%-20s " _; make _ -nB | (grep -i "^# Help:" || echo "") | tail -1 | sed "s/^# Help: //g"'

analyze:
@# Help: Analyze the project's Dart code.
dart analyze --fatal-infos

check_format:
@# Help: Check the formatting of one or more Dart files.
dart format --output=none --set-exit-if-changed .

check_outdated:
@# Help: Check which of the project's packages are outdated.
dart pub outdated

check_style:
@# Help: Analyze the project's Dart code and check the formatting one or more Dart files.
make analyze && make check_format

format:
@# Help: Format one or more Dart files.
dart format .

install:
@# Help: Install all the project's packages
dart pub get

sure:
@# Help: Analyze the project's Dart code, check the formatting one or more Dart files and run unit tests for the current project.
make check_style && make tests

show_test_coverage:
@# Help: Run Dart unit tests for the current project and show the coverage.
dart pub global activate coverage && dart pub global run coverage:test_with_coverage
lcov --remove coverage/lcov.info -o coverage/lcov_without_generated_code.info --ignore-errors unused
genhtml coverage/lcov_without_generated_code.info -o coverage/html
open coverage/html/index.html

tests:
@# Help: Run Dart unit tests for the current project.
dart test
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This can be override by providing a custom storage implementation.
## Usage

Create a `oauth_chopper` client with the needed authorizationEndpoint, identifier and secret.
Add the `oauth_chopper_authenticator` + `oauth_chopper_interceptor` to your chopper client.
Add the `oauth_chopper_interceptor` to your chopper client.
Request a OAuthGrant on the `oauth_chopper` client.

Example:
Expand All @@ -49,9 +49,8 @@ Example:
/// Add the oauth authenticator and interceptor to the chopper client.
final chopperClient = ChopperClient(
baseUrl: Uri.parse('https://example.com'),
authenticator: oauthChopper.authenticator(),
interceptors: [
oauthChopper.interceptor,
oauthChopper.interceptor(),
],
);
Expand Down
5 changes: 2 additions & 3 deletions example/oauth_chopper_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ void main() {
secret: secret,
);

/// Add the oauth authenticator and interceptor to the chopper client.
/// Add the oauth_chopper interceptor to the chopper client.
final chopperClient = ChopperClient(
baseUrl: Uri.parse('https://example.com'),
authenticator: oauthChopper.authenticator(),
interceptors: [
oauthChopper.interceptor,
oauthChopper.interceptor(),
],
);

Expand Down
51 changes: 0 additions & 51 deletions lib/src/oauth_authenticator.dart

This file was deleted.

11 changes: 3 additions & 8 deletions lib/src/oauth_chopper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:oauth2/oauth2.dart' as oauth2;
import 'package:oauth_chopper/src/oauth_authenticator.dart';
import 'package:oauth_chopper/src/oauth_grant.dart';
import 'package:oauth_chopper/src/oauth_interceptor.dart';
import 'package:oauth_chopper/src/oauth_token.dart';
Expand Down Expand Up @@ -67,17 +66,13 @@ class OAuthChopper {
: null;
}

/// Provides an [OAuthAuthenticator] instance.
/// The authenticator can throw exceptions when OAuth authentication fails.
/// Provides an [OAuthInterceptor] instance.
/// If [onError] is provided exceptions will be passed to [onError] and not be
/// thrown.
OAuthAuthenticator authenticator({
OAuthInterceptor interceptor({
OnErrorCallback? onError,
}) =>
OAuthAuthenticator(this, onError);

/// Provides an [OAuthInterceptor] instance.
OAuthInterceptor get interceptor => OAuthInterceptor(this);
OAuthInterceptor(this, onError);

/// Tries to refresh the available credentials and returns a new [OAuthToken]
/// instance.
Expand Down
6 changes: 6 additions & 0 deletions lib/src/oauth_grant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import 'package:oauth2/oauth2.dart' as oauth;
/// {@template oauth_grant}
/// Interface for a OAuth grant.
/// Grants are used to obtain credentials from an authorization server.
///
/// Currently available grants:
/// - [ResourceOwnerPasswordGrant]
/// - [ClientCredentialsGrant]
/// - [AuthorizationCodeGrant]
///
/// {@endtemplate}
abstract interface class OAuthGrant {
/// {@macro oauth_grant}
Expand Down
51 changes: 46 additions & 5 deletions lib/src/oauth_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,65 @@ import 'package:chopper/chopper.dart';
import 'package:oauth_chopper/oauth_chopper.dart';
import 'package:oauth_chopper/src/extensions/request.dart';

/// Callback for error handling.
typedef OnErrorCallback = void Function(Object, StackTrace);

/// {@template oauth_interceptor}
/// OAuthInterceptor is responsible for adding 'Authorization' header to
/// requests.
/// The header is only added if there is a token available. When no token is
/// available no header is added.
/// Its added as a Bearer token.
///
/// When the provided credentials are invalid it tries to refresh them.
/// Can throw a exceptions if no [onError] is passed. When [onError] is passed
/// exception will be passed to [onError]
/// {@endtemplate}
class OAuthInterceptor implements RequestInterceptor {
class OAuthInterceptor implements Interceptor {
/// {@macro oauth_interceptor}
OAuthInterceptor(this.oauthChopper);
OAuthInterceptor(this.oauthChopper, this.onError);

/// Callback for error handling.
final OnErrorCallback? onError;

/// The [OAuthChopper] instance to get the token from.
final OAuthChopper oauthChopper;

@override
FutureOr<Request> onRequest(Request request) async {
FutureOr<Response<BodyType>> intercept<BodyType>(
Chain<BodyType> chain,
) async {
// Add oauth token to the request.
final token = await oauthChopper.token;
if (token == null) return request;
return request.addAuthorizationHeader(token.accessToken);

final Request request;
if (token == null) {
request = chain.request;
} else {
request = chain.request.addAuthorizationHeader(token.accessToken);
}

final response = await chain.proceed(request);

// If the response is unauthorized and a token is available try to
// refresh 1 time.
if (response.statusCode == 401 && token != null) {
try {
final credentials = await oauthChopper.refresh();
if (credentials != null) {
final request =
chain.request.addAuthorizationHeader(credentials.accessToken);
return chain.proceed(request);
}
} catch (e, s) {
if (onError != null) {
onError?.call(e, s);
} else {
rethrow;
}
}
}

return response;
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: oauth_chopper
description: Add and manage OAuth2 authentication for your Chopper client.
version: 0.4.0
version: 1.0.0
homepage: https://github.com/DutchCodingCompany/oauth_chopper

environment:
sdk: '>=3.3.0 <4.0.0'

dependencies:
chopper: ^7.2.0
chopper: ^8.0.0
http: ^1.2.1
oauth2: ^2.0.2

Expand Down
Loading

0 comments on commit 6277c30

Please sign in to comment.