buildOpenRouterAuthUrl function

Uri buildOpenRouterAuthUrl({
  1. required String codeChallenge,
  2. String? callbackUrl,
  3. String? keyLabel,
  4. String? state,
})

Builds the OpenRouter authorization URL.

If callbackUrl is provided, OpenRouter redirects back to it with a code query parameter. If omitted, OpenRouter shows the code on screen (headless/SSH/container mode) and uses keyLabel as the display label.

codeChallenge must be the S256 challenge derived from the verifier.

Implementation

Uri buildOpenRouterAuthUrl({
  required String codeChallenge,
  String? callbackUrl,
  String? keyLabel,
  String? state,
}) {
  final params = <String, String>{
    'code_challenge': codeChallenge,
    'code_challenge_method': 'S256',
  };
  if (callbackUrl != null && callbackUrl.isNotEmpty) {
    params['callback_url'] = callbackUrl;
  }
  if (keyLabel != null && keyLabel.isNotEmpty) {
    params['key_label'] = keyLabel;
  }
  if (state != null && state.isNotEmpty) {
    params['state'] = state;
  }
  return Uri.parse(openRouterAuthEndpoint).replace(queryParameters: params);
}