Skip to content

Commit 6277c30

Browse files
authored
Merge pull request #21 from DutchCodingCompany/feature/upgrade_chopper_v8
Release v1
2 parents a84c139 + 879fd83 commit 6277c30

13 files changed

+295
-275
lines changed

.github/workflows/dart.yml

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,17 @@ jobs:
1616
runs-on: ubuntu-latest
1717

1818
steps:
19-
- uses: actions/checkout@v3
20-
21-
# Note: This workflow uses the latest stable version of the Dart SDK.
22-
# You can specify other versions if desired, see documentation here:
23-
# https://github.com/dart-lang/setup-dart/blob/main/README.md
24-
# - uses: dart-lang/setup-dart@v1
25-
- uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd603
19+
- uses: actions/checkout@v4
20+
- uses: dart-lang/setup-dart@v1
2621

2722
- name: Install dependencies
2823
run: dart pub get
2924

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

33-
# Uncomment this step to verify the use of 'dart format' on each commit.
34-
# - name: Verify formatting
35-
# run: dart format --output=none --set-exit-if-changed .
36-
37-
# Consider passing '--fatal-infos' for slightly stricter analysis.
3828
- name: Analyze project source
39-
run: dart analyze
29+
run: dart analyze --fatal-infos .
4030

41-
# Your project will need to have tests in test/ and a dependency on
42-
# package:test for this step to succeed. Note that Flutter projects will
43-
# want to change this to 'flutter test'.
4431
- name: Run tests
4532
run: dart test

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.0
2+
- Updated chopper to v8.0.0
3+
- **BREAKING** Removed oauth_chopper authenticator. Now only the interceptor is needed.
4+
15
## 0.4.0
26
- ✨ Add very good analysis by @Guldem in https://github.com/DutchCodingCompany/oauth_chopper/pull/17
37
- ✨ Add custom client by @Guldem in https://github.com/DutchCodingCompany/oauth_chopper/pull/18

Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Makefile
2+
3+
help:
4+
@printf "%-20s %s\n" "Target" "Description"
5+
@printf "%-20s %s\n" "------" "-----------"
6+
@make -pqR : 2>/dev/null \
7+
| awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' \
8+
| sort \
9+
| egrep -v -e '^[^[:alnum:]]' -e '^$@$$' \
10+
| xargs -I _ sh -c 'printf "%-20s " _; make _ -nB | (grep -i "^# Help:" || echo "") | tail -1 | sed "s/^# Help: //g"'
11+
12+
analyze:
13+
@# Help: Analyze the project's Dart code.
14+
dart analyze --fatal-infos
15+
16+
check_format:
17+
@# Help: Check the formatting of one or more Dart files.
18+
dart format --output=none --set-exit-if-changed .
19+
20+
check_outdated:
21+
@# Help: Check which of the project's packages are outdated.
22+
dart pub outdated
23+
24+
check_style:
25+
@# Help: Analyze the project's Dart code and check the formatting one or more Dart files.
26+
make analyze && make check_format
27+
28+
format:
29+
@# Help: Format one or more Dart files.
30+
dart format .
31+
32+
install:
33+
@# Help: Install all the project's packages
34+
dart pub get
35+
36+
sure:
37+
@# Help: Analyze the project's Dart code, check the formatting one or more Dart files and run unit tests for the current project.
38+
make check_style && make tests
39+
40+
show_test_coverage:
41+
@# Help: Run Dart unit tests for the current project and show the coverage.
42+
dart pub global activate coverage && dart pub global run coverage:test_with_coverage
43+
lcov --remove coverage/lcov.info -o coverage/lcov_without_generated_code.info --ignore-errors unused
44+
genhtml coverage/lcov_without_generated_code.info -o coverage/html
45+
open coverage/html/index.html
46+
47+
tests:
48+
@# Help: Run Dart unit tests for the current project.
49+
dart test

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This can be override by providing a custom storage implementation.
3333
## Usage
3434

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

3939
Example:
@@ -49,9 +49,8 @@ Example:
4949
/// Add the oauth authenticator and interceptor to the chopper client.
5050
final chopperClient = ChopperClient(
5151
baseUrl: Uri.parse('https://example.com'),
52-
authenticator: oauthChopper.authenticator(),
5352
interceptors: [
54-
oauthChopper.interceptor,
53+
oauthChopper.interceptor(),
5554
],
5655
);
5756

example/oauth_chopper_example.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ void main() {
1717
secret: secret,
1818
);
1919

20-
/// Add the oauth authenticator and interceptor to the chopper client.
20+
/// Add the oauth_chopper interceptor to the chopper client.
2121
final chopperClient = ChopperClient(
2222
baseUrl: Uri.parse('https://example.com'),
23-
authenticator: oauthChopper.authenticator(),
2423
interceptors: [
25-
oauthChopper.interceptor,
24+
oauthChopper.interceptor(),
2625
],
2726
);
2827

lib/src/oauth_authenticator.dart

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/src/oauth_chopper.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22

33
import 'package:http/http.dart' as http;
44
import 'package:oauth2/oauth2.dart' as oauth2;
5-
import 'package:oauth_chopper/src/oauth_authenticator.dart';
65
import 'package:oauth_chopper/src/oauth_grant.dart';
76
import 'package:oauth_chopper/src/oauth_interceptor.dart';
87
import 'package:oauth_chopper/src/oauth_token.dart';
@@ -67,17 +66,13 @@ class OAuthChopper {
6766
: null;
6867
}
6968

70-
/// Provides an [OAuthAuthenticator] instance.
71-
/// The authenticator can throw exceptions when OAuth authentication fails.
69+
/// Provides an [OAuthInterceptor] instance.
7270
/// If [onError] is provided exceptions will be passed to [onError] and not be
7371
/// thrown.
74-
OAuthAuthenticator authenticator({
72+
OAuthInterceptor interceptor({
7573
OnErrorCallback? onError,
7674
}) =>
77-
OAuthAuthenticator(this, onError);
78-
79-
/// Provides an [OAuthInterceptor] instance.
80-
OAuthInterceptor get interceptor => OAuthInterceptor(this);
75+
OAuthInterceptor(this, onError);
8176

8277
/// Tries to refresh the available credentials and returns a new [OAuthToken]
8378
/// instance.

lib/src/oauth_grant.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import 'package:oauth2/oauth2.dart' as oauth;
44
/// {@template oauth_grant}
55
/// Interface for a OAuth grant.
66
/// Grants are used to obtain credentials from an authorization server.
7+
///
8+
/// Currently available grants:
9+
/// - [ResourceOwnerPasswordGrant]
10+
/// - [ClientCredentialsGrant]
11+
/// - [AuthorizationCodeGrant]
12+
///
713
/// {@endtemplate}
814
abstract interface class OAuthGrant {
915
/// {@macro oauth_grant}

lib/src/oauth_interceptor.dart

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,65 @@ import 'package:chopper/chopper.dart';
44
import 'package:oauth_chopper/oauth_chopper.dart';
55
import 'package:oauth_chopper/src/extensions/request.dart';
66

7+
/// Callback for error handling.
8+
typedef OnErrorCallback = void Function(Object, StackTrace);
9+
710
/// {@template oauth_interceptor}
811
/// OAuthInterceptor is responsible for adding 'Authorization' header to
912
/// requests.
1013
/// The header is only added if there is a token available. When no token is
1114
/// available no header is added.
1215
/// Its added as a Bearer token.
16+
///
17+
/// When the provided credentials are invalid it tries to refresh them.
18+
/// Can throw a exceptions if no [onError] is passed. When [onError] is passed
19+
/// exception will be passed to [onError]
1320
/// {@endtemplate}
14-
class OAuthInterceptor implements RequestInterceptor {
21+
class OAuthInterceptor implements Interceptor {
1522
/// {@macro oauth_interceptor}
16-
OAuthInterceptor(this.oauthChopper);
23+
OAuthInterceptor(this.oauthChopper, this.onError);
24+
25+
/// Callback for error handling.
26+
final OnErrorCallback? onError;
1727

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

2131
@override
22-
FutureOr<Request> onRequest(Request request) async {
32+
FutureOr<Response<BodyType>> intercept<BodyType>(
33+
Chain<BodyType> chain,
34+
) async {
35+
// Add oauth token to the request.
2336
final token = await oauthChopper.token;
24-
if (token == null) return request;
25-
return request.addAuthorizationHeader(token.accessToken);
37+
38+
final Request request;
39+
if (token == null) {
40+
request = chain.request;
41+
} else {
42+
request = chain.request.addAuthorizationHeader(token.accessToken);
43+
}
44+
45+
final response = await chain.proceed(request);
46+
47+
// If the response is unauthorized and a token is available try to
48+
// refresh 1 time.
49+
if (response.statusCode == 401 && token != null) {
50+
try {
51+
final credentials = await oauthChopper.refresh();
52+
if (credentials != null) {
53+
final request =
54+
chain.request.addAuthorizationHeader(credentials.accessToken);
55+
return chain.proceed(request);
56+
}
57+
} catch (e, s) {
58+
if (onError != null) {
59+
onError?.call(e, s);
60+
} else {
61+
rethrow;
62+
}
63+
}
64+
}
65+
66+
return response;
2667
}
2768
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: oauth_chopper
22
description: Add and manage OAuth2 authentication for your Chopper client.
3-
version: 0.4.0
3+
version: 1.0.0
44
homepage: https://github.com/DutchCodingCompany/oauth_chopper
55

66
environment:
77
sdk: '>=3.3.0 <4.0.0'
88

99
dependencies:
10-
chopper: ^7.2.0
10+
chopper: ^8.0.0
1111
http: ^1.2.1
1212
oauth2: ^2.0.2
1313

0 commit comments

Comments
 (0)