Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/access_token_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class AccessTokenRequest {
final String code;
final String clientId;
final String redirectUri;
final String tokenUrl;
final String? clientSecret;
final String? codeVerifier;
List<String>? scopes;
Map<String, dynamic>? customParams;
dynamic httpClient;

AccessTokenRequest({
required this.code,
required this.clientId,
required this.redirectUri,
required this.tokenUrl,
this.clientSecret,
this.codeVerifier,
this.scopes,
this.customParams,
this.httpClient,
});
}
142 changes: 89 additions & 53 deletions lib/oauth2_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:oauth2_client/access_token_response.dart';
import 'package:oauth2_client/access_token_request.dart';
import 'package:oauth2_client/authorization_response.dart';
import 'package:oauth2_client/oauth2_response.dart';
import 'package:oauth2_client/src/oauth2_utils.dart';
Expand Down Expand Up @@ -128,6 +129,61 @@ class OAuth2Client {
}
}

Future<AccessTokenRequest?> getTokenRequestWithAuthCodeFlow({
required String clientId,
List<String>? scopes,
String? clientSecret,
bool enablePKCE = true,
bool enableState = true,
String? state,
String? codeVerifier,
Function? afterAuthorizationCodeCb,
Map<String, dynamic>? authCodeParams,
Map<String, dynamic>? accessTokenParams,
httpClient,
BaseWebAuth? webAuthClient,
Map<String, dynamic>? webAuthOpts,
}) async {
String? codeChallenge;

if (enablePKCE) {
codeVerifier ??= randomAlphaNumeric(80);

codeChallenge = OAuth2Utils.generateCodeChallenge(codeVerifier);
}

try {
final authResp = await requestAuthorization(
webAuthClient: webAuthClient,
clientId: clientId,
scopes: scopes,
codeChallenge: codeChallenge,
enableState: enableState,
state: state,
webAuthOpts: webAuthOpts,
);

if (authResp.isAccessGranted()) {
if (afterAuthorizationCodeCb != null) {
afterAuthorizationCodeCb(authResp);
}

return AccessTokenRequest(
code: authResp.code!,
clientId: clientId,
scopes: scopes,
tokenUrl: tokenUrl,
redirectUri: redirectUri,
codeVerifier: codeVerifier,
customParams: accessTokenParams,
);
}
} catch (e) {
return null;
}
return null;
}

/// Requests an Access Token to the OAuth2 endpoint using the Authorization Code Flow.
Future<AccessTokenResponse> getTokenWithAuthCodeFlow(
{required String clientId,
Expand All @@ -145,40 +201,25 @@ class OAuth2Client {
Map<String, dynamic>? webAuthOpts}) async {
AccessTokenResponse? tknResp;

String? codeChallenge;

if (enablePKCE) {
codeVerifier ??= randomAlphaNumeric(80);

codeChallenge = OAuth2Utils.generateCodeChallenge(codeVerifier);
}

try {
var authResp = await requestAuthorization(
webAuthClient: webAuthClient,
clientId: clientId,
scopes: scopes,
codeChallenge: codeChallenge,
enableState: enableState,
state: state,
customParams: authCodeParams,
webAuthOpts: webAuthOpts);

if (authResp.isAccessGranted()) {
if (afterAuthorizationCodeCb != null) {
afterAuthorizationCodeCb(authResp);
}

tknResp = await requestAccessToken(
httpClient: httpClient,
//If the authorization request was successfull, the code must be set
//otherwise an exception is raised in the OAuth2Response constructor
code: authResp.code!,
clientId: clientId,
scopes: scopes,
clientSecret: clientSecret,
codeVerifier: codeVerifier,
customParams: accessTokenParams);
final tknReq = await getTokenRequestWithAuthCodeFlow(
clientId: clientId,
scopes: scopes,
clientSecret: clientSecret,
enablePKCE: enablePKCE,
enableState: enableState,
state: state,
codeVerifier: codeVerifier,
afterAuthorizationCodeCb: afterAuthorizationCodeCb,
authCodeParams: authCodeParams,
accessTokenParams: accessTokenParams,
httpClient: httpClient,
webAuthClient: webAuthClient,
webAuthOpts: webAuthOpts,
);

if (tknReq != null) {
tknResp = await requestAccessToken(tknReq);
} else {
tknResp = AccessTokenResponse.errorResponse();
}
Expand Down Expand Up @@ -247,29 +288,24 @@ class OAuth2Client {
}

/// Requests and Access Token using the provided Authorization [code].
Future<AccessTokenResponse> requestAccessToken(
{required String code,
required String clientId,
String? clientSecret,
String? codeVerifier,
List<String>? scopes,
Map<String, dynamic>? customParams,
httpClient}) async {
Future<AccessTokenResponse> requestAccessToken(AccessTokenRequest req) async {
final params = getTokenUrlParams(
code: code,
redirectUri: redirectUri,
codeVerifier: codeVerifier,
customParams: customParams);
code: req.code,
redirectUri: redirectUri,
codeVerifier: req.codeVerifier,
customParams: req.customParams,
);

var response = await _performAuthorizedRequest(
url: tokenUrl,
clientId: clientId,
clientSecret: clientSecret,
params: params,
headers: _accessTokenRequestHeaders,
httpClient: httpClient);

return http2TokenResponse(response, requestedScopes: scopes);
url: tokenUrl,
clientId: req.clientId,
clientSecret: req.clientSecret,
params: params,
headers: _accessTokenRequestHeaders,
httpClient: req.httpClient,
);

return http2TokenResponse(response, requestedScopes: req.scopes);
}

/// Refreshes an Access Token issuing a refresh_token grant to the OAuth2 server.
Expand Down
29 changes: 22 additions & 7 deletions test/oauth2_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
import 'package:oauth2_client/access_token_request.dart';
import 'package:oauth2_client/access_token_response.dart';
import 'package:oauth2_client/oauth2_client.dart';
import 'package:oauth2_client/src/oauth2_utils.dart';
Expand Down Expand Up @@ -94,10 +95,15 @@ void main() {
200));

final tknResponse = await oauth2Client.requestAccessToken(
httpClient: httpClient,
AccessTokenRequest(
code: authCode,
tokenUrl: tokenUrl,
clientId: clientId,
codeVerifier: codeVerifier);
redirectUri: redirectUri,
httpClient: httpClient,
codeVerifier: codeVerifier,
),
);

expect(
verify(httpClient.post(Uri.parse(tokenUrl),
Expand Down Expand Up @@ -135,10 +141,15 @@ void main() {
200));

final tknResponse = await oauth2Client.requestAccessToken(
httpClient: httpClient,
AccessTokenRequest(
code: authCode,
tokenUrl: tokenUrl,
clientId: clientId,
codeVerifier: codeVerifier);
redirectUri: redirectUri,
httpClient: httpClient,
codeVerifier: codeVerifier,
),
);

expect(
verify(httpClient.post(Uri.parse(tokenUrl),
Expand Down Expand Up @@ -203,11 +214,15 @@ void main() {
.thenAnswer((_) async => http.Response('', 404));

final tknResponse = await oauth2Client.requestAccessToken(
httpClient: httpClient,
AccessTokenRequest(
code: authCode,
tokenUrl: tokenUrl,
clientId: clientId,
// clientSecret: clientSecret,
codeVerifier: codeVerifier);
redirectUri: redirectUri,
httpClient: httpClient,
codeVerifier: codeVerifier,
),
);

expect(tknResponse.isValid(), false);
});
Expand Down
Loading