fromDomain static method
Creates a WebAuth instance by automatically discovering configuration from stellar.toml.
This is the recommended way to initialize WebAuth. It fetches the stellar.toml file from the specified domain and extracts the required configuration:
- WEB_AUTH_ENDPOINT: The authentication endpoint URL
- SIGNING_KEY: The server's public signing key for validating challenges
Parameters:
domainThe domain name (without protocol) hosting the stellar.toml filenetworkThe Stellar network (Network.PUBLIC or Network.TESTNET)httpClientOptional custom HTTP client for testing or proxy configurationhttpRequestHeadersOptional custom HTTP headers for requests
Returns: Future
Throws:
- NoWebAuthEndpointFoundException If WEB_AUTH_ENDPOINT is missing from stellar.toml
- NoWebAuthServerSigningKeyFoundException If SIGNING_KEY is missing from stellar.toml
- Exception If stellar.toml cannot be fetched or parsed
Example:
// Initialize from a domain's stellar.toml
final webAuth = await WebAuth.fromDomain(
'testanchor.stellar.org',
Network.TESTNET,
);
// Then use for authentication
final userKeyPair = KeyPair.fromSecretSeed('S...');
final token = await webAuth.jwtToken(userKeyPair.accountId, [userKeyPair]);
Example with error handling:
try {
final webAuth = await WebAuth.fromDomain('example.com', Network.PUBLIC);
} on NoWebAuthEndpointFoundException catch (e) {
print('Domain does not support WebAuth: ${e.domain}');
} catch (e) {
print('Failed to initialize WebAuth: $e');
}
Implementation
static Future<WebAuth> fromDomain(
String domain,
Network network, {
http.Client? httpClient,
Map<String, String>? httpRequestHeaders,
}) async {
final StellarToml toml = await StellarToml.fromDomain(
domain,
httpClient: httpClient,
httpRequestHeaders: httpRequestHeaders,
);
if (toml.generalInformation.webAuthEndpoint == null) {
throw NoWebAuthEndpointFoundException(domain);
}
if (toml.generalInformation.signingKey == null) {
throw NoWebAuthServerSigningKeyFoundException(domain);
}
return new WebAuth(
toml.generalInformation.webAuthEndpoint!,
network,
toml.generalInformation.signingKey!,
domain,
httpClient: httpClient,
httpRequestHeaders: httpRequestHeaders,
);
}