|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | + |
| 11 | +namespace FirebaseAdmin.Check |
| 12 | +{ |
| 13 | + internal class AppCheckApiClient |
| 14 | + { |
| 15 | + private const string ApiUrlFormat = "https://firebaseappcheck.googleapis.com/v1/projects/{projectId}/apps/{appId}:exchangeCustomToken"; |
| 16 | + private readonly FirebaseApp app; |
| 17 | + private string projectId; |
| 18 | + private string appId; |
| 19 | + |
| 20 | + public AppCheckApiClient(FirebaseApp value) |
| 21 | + { |
| 22 | + if (value == null || value.Options == null) |
| 23 | + { |
| 24 | + throw new ArgumentException("Argument passed to admin.appCheck() must be a valid Firebase app instance."); |
| 25 | + } |
| 26 | + |
| 27 | + this.app = value; |
| 28 | + this.projectId = this.app.Options.ProjectId; |
| 29 | + } |
| 30 | + |
| 31 | + public AppCheckApiClient(string appId) |
| 32 | + { |
| 33 | + this.appId = appId; |
| 34 | + } |
| 35 | + |
| 36 | + public async Task<AppCheckToken> ExchangeToken(string customToken) |
| 37 | + { |
| 38 | + if (customToken == null) |
| 39 | + { |
| 40 | + throw new ArgumentException("First argument passed to customToken must be a valid Firebase app instance."); |
| 41 | + } |
| 42 | + |
| 43 | + if (this.appId == null) |
| 44 | + { |
| 45 | + throw new ArgumentException("Second argument passed to appId must be a valid Firebase app instance."); |
| 46 | + } |
| 47 | + |
| 48 | + var url = this.GetUrl(this.appId); |
| 49 | + var request = new HttpRequestMessage() |
| 50 | + { |
| 51 | + Method = HttpMethod.Post, |
| 52 | + RequestUri = new Uri(url), |
| 53 | + Content = new StringContent(customToken), |
| 54 | + }; |
| 55 | + request.Headers.Add("X-Firebase-Client", "fire-admin-node/${utils.getSdkVersion()}"); |
| 56 | + var httpClient = new HttpClient(); |
| 57 | + var response = await httpClient.SendAsync(request).ConfigureAwait(false); |
| 58 | + if (!response.IsSuccessStatusCode) |
| 59 | + { |
| 60 | + throw new ArgumentException("Error exchanging token."); |
| 61 | + } |
| 62 | + |
| 63 | + var responseData = JObject.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); |
| 64 | + string tokenValue = responseData["data"]["token"].ToString(); |
| 65 | + int ttlValue = int.Parse(responseData["data"]["ttl"].ToString()); |
| 66 | + AppCheckToken appCheckToken = new (tokenValue, ttlValue); |
| 67 | + return appCheckToken; |
| 68 | + } |
| 69 | + |
| 70 | + private string GetUrl(string appId) |
| 71 | + { |
| 72 | + if (string.IsNullOrEmpty(this.projectId)) |
| 73 | + { |
| 74 | + this.projectId = this.app.GetProjectId(); |
| 75 | + } |
| 76 | + |
| 77 | + if (string.IsNullOrEmpty(this.projectId)) |
| 78 | + { |
| 79 | + string errorMessage = "Failed to determine project ID. Initialize the SDK with service account " + |
| 80 | + "credentials or set project ID as an app option. Alternatively, set the " + |
| 81 | + "GOOGLE_CLOUD_PROJECT environment variable."; |
| 82 | + throw new ArgumentException( |
| 83 | + "unknown-error", |
| 84 | + errorMessage); |
| 85 | + } |
| 86 | + |
| 87 | + var urlParams = new Dictionary<string, string> |
| 88 | + { |
| 89 | + { "projectId", this.projectId }, |
| 90 | + { "appId", appId }, |
| 91 | + }; |
| 92 | + string baseUrl = this.FormatString(ApiUrlFormat, urlParams); |
| 93 | + return baseUrl; |
| 94 | + } |
| 95 | + |
| 96 | + private string FormatString(string str, Dictionary<string, string> urlParams) |
| 97 | + { |
| 98 | + string formatted = str; |
| 99 | + foreach (var key in urlParams.Keys) |
| 100 | + { |
| 101 | + formatted = Regex.Replace(formatted, $"{{{key}}}", urlParams[key]); |
| 102 | + } |
| 103 | + |
| 104 | + return formatted; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments