resolveAuthenticationOrigin function

String resolveAuthenticationOrigin({
  1. required String publicAddress,
  2. required String host,
  3. required int port,
  4. required bool tlsEnabled,
})

Implementation

String resolveAuthenticationOrigin({
  required String publicAddress,
  required String host,
  required int port,
  required bool tlsEnabled,
}) {
  if (publicAddress.isEmpty && _isWildcardHost(host)) {
    throw StateError(
      'A public authentication origin is required when binding to $host. '
      'Set publicAddress to the address players use, for example '
      'wss://play.example.com:$port.',
    );
  }
  final origin = publicAddress.isNotEmpty
      ? canonicalAuthenticationOrigin(Uri.parse(publicAddress))
      : canonicalAuthenticationOrigin(
          Uri(scheme: tlsEnabled ? 'wss' : 'ws', host: host, port: port),
        );
  if (tlsEnabled && !origin.startsWith('wss://')) {
    throw StateError('The authentication origin must use WSS with TLS.');
  }
  return origin;
}