XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using Google.Apis.Auth;

namespace XFEWarframe.Core.Services;

public sealed record GoogleIdentity(string Subject, string Email);
public interface IGoogleIdentityProvider { Task<GoogleIdentity> Verify(string token); }
public sealed class GoogleIdentityProvider(ServerOptions options) : IGoogleIdentityProvider
{
    public async Task<GoogleIdentity> 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"); }
    }
}