/* Copyright 2013 Google Inc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using Google.Apis.Json; using System; using System.IO; using System.Threading; using System.Threading.Tasks; namespace Google.Apis.Auth.OAuth2 { /// /// OAuth 2.0 client secrets model as specified in https://cloud.google.com/console/. /// public sealed class GoogleClientSecrets { /// Gets or sets the details for installed applications. [Newtonsoft.Json.JsonProperty("installed")] private ClientSecrets Installed { get; set; } /// Gets or sets the details for web applications. [Newtonsoft.Json.JsonProperty("web")] private ClientSecrets Web { get; set; } /// Gets the client secrets which contains the client identifier and client secret. public ClientSecrets Secrets { get { if (Installed == null && Web == null) { throw new InvalidOperationException( "At least one client secrets (Installed or Web) should be set"); } return Installed ?? Web; } } /// Loads the Google client secret from the input stream. /// This method has been made obsolete in favour of /// which only differs in name. [Obsolete("Please use GoogleClientSecrets.FromStream which is an equivalent alternative.")] public static GoogleClientSecrets Load(Stream stream) => FromStream(stream); /// Loads the Google client secret from the input stream. public static GoogleClientSecrets FromStream(Stream stream) => NewtonsoftJsonSerializer.Instance.Deserialize(stream); /// Asynchronously loads the Google client secret from the input stream. public static Task FromStreamAsync(Stream stream, CancellationToken cancellationToken = default) => NewtonsoftJsonSerializer.Instance.DeserializeAsync(stream, cancellationToken); /// Loads the Google client secret from a JSON file. public static GoogleClientSecrets FromFile(string clientSecretsFilePath) { using var stream = new FileStream(clientSecretsFilePath, FileMode.Open, FileAccess.Read); return FromStream(stream); } /// Asynchronously loads the Google client secret from a JSON file. public static async Task FromFileAsync(string clientSecretsFilePath, CancellationToken cancellationToken = default) { using var stream = new FileStream(clientSecretsFilePath, FileMode.Open, FileAccess.Read); return await FromStreamAsync(stream, cancellationToken).ConfigureAwait(false); } } }