示例#1
0
        public virtual Task <string> GetTokenAsync(IFirebaseAppPlatform app, bool forceRefresh)
        {
            if (app == null)
            {
                app = FirebaseHandler.AppUtils.GetDefaultInstance();
            }
            TaskCompletionSource <string> taskCompletionSource = new TaskCompletionSource <string>();

            if (this.GetIsServiceAccountAuth(app))
            {
                string editorP12FileName = Services.AppConfig.GetEditorP12FileName(app);
                string text = Services.AppConfig.GetEditorP12Password(app);
                string editorServiceAccountEmail = Services.AppConfig.GetEditorServiceAccountEmail(app);
                if (string.IsNullOrEmpty(text))
                {
                    text = "notasecret";
                }
                X509Certificate2         certificate = new X509Certificate2(editorP12FileName, text, X509KeyStorageFlags.Exportable);
                ServiceAccountCredential serviceAccountCredential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(editorServiceAccountEmail)
                {
                    Scopes = new List <string>(new string[]
                    {
                        "https://www.googleapis.com/auth/firebase.database",
                        "https://www.googleapis.com/auth/userinfo.email"
                    })
                }.FromCertificate(certificate));
                string accessTokenForRequest = serviceAccountCredential.GetAccessTokenForRequest();
                if (string.IsNullOrEmpty(accessTokenForRequest))
                {
                    Services.Logging.LogMessage(PlatformLogLevel.Debug, "FirebaseDatabase: Error obtaining service credential. Attempting unauthenticated access");
                    taskCompletionSource.SetResult(string.Empty);
                }
                else
                {
                    string editorAuthUserId = Services.AppConfig.GetEditorAuthUserId(app);
                    Dictionary <string, object> dictionary = null;
                    if (!string.IsNullOrEmpty(editorAuthUserId))
                    {
                        dictionary        = new Dictionary <string, object>();
                        dictionary["uid"] = editorAuthUserId;
                    }
                    GAuthToken gauthToken = new GAuthToken(accessTokenForRequest, dictionary);
                    taskCompletionSource.SetResult(gauthToken.SerializeToString());
                }
            }
            else
            {
                taskCompletionSource.SetResult(string.Empty);
            }
            return(taskCompletionSource.Task);
        }