getAuthorizationUrl static method

Future<String> getAuthorizationUrl(
  1. OAuthProvider provider
)

Generates an authorization URL for the OAuth flow.

Includes PKCE parameters and state for security.

Implementation

static Future<String> getAuthorizationUrl(OAuthProvider provider) async {
  try {
    _state = CryptoUtils.generateRandomString(32);
    _codeVerifier = CryptoUtils.generateRandomString(128);
    _codeChallenge = CryptoUtils.generateCodeChallenge(_codeVerifier);

    final discoveryData = await _fetchDiscoveryDocument(provider);
    final authorizationEndpoint = discoveryData['authorization_endpoint'];

    if (authorizationEndpoint == null) {
      throw const OAuthException(
        message: 'Authorization endpoint not found in discovery document',
        code: 'missing_endpoint',
      );
    }

    return Uri.parse(authorizationEndpoint).replace(
      queryParameters: {
        'response_type': 'code',
        'client_id': provider.clientId,
        'redirect_uri': provider.redirectUrl,
        'scope': provider.scopes.join(' '),
        'state': _state,
        'code_challenge': _codeChallenge,
        'code_challenge_method': 'S256',
      },
    ).toString();
  } catch (e, stackTrace) {
    dev.log(
      'Error generating authorization URL',
      error: e,
      stackTrace: stackTrace,
    );
    rethrow;
  }
}