Skip to content

Commit

Permalink
Merge pull request #205 from gmpassos/codeVerifier
Browse files Browse the repository at this point in the history
`SpotifyApi.authorizationCodeGrant`: add parameter `codeVerifier`.
  • Loading branch information
rinukkusu authored Mar 1, 2024
2 parents daefb6d + 9fcbcff commit cef576d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
8 changes: 5 additions & 3 deletions lib/src/spotify_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class SpotifyApi extends SpotifyApiBase {

static oauth2.AuthorizationCodeGrant authorizationCodeGrant(
SpotifyApiCredentials credentials,
{Function(SpotifyApiCredentials)? onCredentialsRefreshed}) {
return SpotifyApiBase.authorizationCodeGrant(
credentials, http.Client(), onCredentialsRefreshed);
{String? codeVerifier,
Function(SpotifyApiCredentials)? onCredentialsRefreshed}) {
return SpotifyApiBase.authorizationCodeGrant(credentials, http.Client(),
codeVerifier: codeVerifier,
onCredentialsRefreshed: onCredentialsRefreshed);
}
}
36 changes: 28 additions & 8 deletions lib/src/spotify_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,57 @@ abstract class SpotifyApiBase {
'https://accounts.spotify.com/authorize';

bool _shouldWait = false;
late FutureOr<oauth2.Client> _client;

late FutureOr<oauth2.Client> _client;
FutureOr<oauth2.Client> get client => _client;

late Artists _artists;
Artists get artists => _artists;

late Albums _albums;
Albums get albums => _albums;

late Browse _browse;
Browse get browse => _browse;

late Tracks _tracks;
Tracks get tracks => _tracks;

late Playlists _playlists;
Playlists get playlists => _playlists;

late Episodes _episodes;
Episodes get episodes => _episodes;

late RecommendationsEndpoint _recommendations;
RecommendationsEndpoint get recommendations => _recommendations;

late Markets _markets;
Markets get markets => _markets;

late Users _users;
Users get users => _users;

late Search _search;
Search get search => _search;

late AudioFeatures _audioFeatures;
AudioFeatures get audioFeatures => _audioFeatures;

late AudioAnalysisEndpoint _audioAnalysis;
AudioAnalysisEndpoint get audioAnalysis => _audioAnalysis;

late Categories _categories;
Categories get categories => _categories;

late Me _me;
Me get me => _me;

late PlayerEndpoint _player;
PlayerEndpoint get player => _player;

late Shows _shows;
Shows get shows => _shows;
FutureOr<oauth2.Client> get client => _client;

SpotifyApiBase.fromClient(FutureOr<http.BaseClient> client) {
_client = client as FutureOr<oauth2.Client>;
Expand Down Expand Up @@ -94,22 +111,24 @@ abstract class SpotifyApiBase {

static oauth2.AuthorizationCodeGrant authorizationCodeGrant(
SpotifyApiCredentials credentials, http.Client httpClient,
[Function(SpotifyApiCredentials)? callBack]) {
{String? codeVerifier,
Function(SpotifyApiCredentials)? onCredentialsRefreshed}) {
return oauth2.AuthorizationCodeGrant(
credentials.clientId!,
Uri.parse(SpotifyApiBase._authorizationUrl),
Uri.parse(SpotifyApiBase._tokenUrl),
secret: credentials.clientSecret,
codeVerifier: codeVerifier,
httpClient: httpClient,
onCredentialsRefreshed: callBack != null
onCredentialsRefreshed: onCredentialsRefreshed != null
? (oauth2.Credentials cred) {
final newCredentials = SpotifyApiCredentials(
credentials.clientId, credentials.clientSecret,
accessToken: cred.accessToken,
expiration: cred.expiration,
refreshToken: cred.refreshToken,
scopes: cred.scopes);
callBack(newCredentials);
onCredentialsRefreshed(newCredentials);
}
: null);
}
Expand Down Expand Up @@ -159,7 +178,8 @@ abstract class SpotifyApiBase {
}

/// Expands shortened spotify [url]
Future<String> expandLink(String url) async => _streamedHeadImpl(url, const {});
Future<String> expandLink(String url) async =>
_streamedHeadImpl(url, const {});

Future<String> _get(String path) {
return _getImpl('$_baseUrl/$path', const {});
Expand Down Expand Up @@ -225,13 +245,13 @@ abstract class SpotifyApiBase {
}
try {
var response = await request();

// distinguish between url redirect responses and body responses
// note, that any response that also contains a redirect url
// will be chosen instead of its body contents
// FIXME: in future releases of http2, the url is a part of the [http.Response] type
if (response case http.BaseResponseWithUrl(:final url)) {
return url.toString();
return url.toString();
}
return handleResponseWithBody(response as http.Response);
} on ApiRateException catch (ex) {
Expand Down

0 comments on commit cef576d

Please sign in to comment.