using Google.Apis.Auth; namespace XFEWarframe.Core.Services; public sealed record GoogleIdentity(string Subject, string Email); public interface IGoogleIdentityProvider { Task Verify(string token); } public sealed class GoogleIdentityProvider(ServerOptions options) : IGoogleIdentityProvider { public async Task Verify(string token) { if (options.GoogleClientIds.Length == 0) throw new ApiException(503, "Configure the Android OAuth client IDs in server.json GoogleClientIds"); if (token.Length is < 16 or > 16384) throw new ApiException(400, "incorrect login data"); try { // Google verifies signature, issuer, audience and expiry using its cached rotating public keys. var payload = await GoogleJsonWebSignature.ValidateAsync(token, new GoogleJsonWebSignature.ValidationSettings { Audience = options.GoogleClientIds }).WaitAsync(TimeSpan.FromSeconds(15)); if (string.IsNullOrEmpty(payload.Subject) || string.IsNullOrEmpty(payload.Email) || !payload.EmailVerified) throw new ApiException(400, "incorrect login data"); return new(payload.Subject, payload.Email.Trim().ToLowerInvariant()); } catch (InvalidJwtException) { throw new ApiException(400, "incorrect login data"); } catch (HttpRequestException) { throw new ApiException(503, "Google identity verification is temporarily unavailable"); } catch (TimeoutException) { throw new ApiException(503, "Google identity verification timed out"); } catch (TaskCanceledException) { throw new ApiException(503, "Google identity verification timed out"); } } }