private async void FetchFirebaseData(string accessToken, FirebaseAuthType authType) { try { // Convert the access token to firebase token var auth = new FirebaseAuthProvider(new FirebaseConfig(FirebaseAppKey)); var data = await auth.SignInWithOAuthAsync(authType, accessToken); // Setup FirebaseClient to use the firebase token for data requests var db = new FirebaseClient( FirebaseAppUri, new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(data.FirebaseToken) }); // TODO: your path within your DB structure. var dbData = await db .Child("userGroups") .Child(data.User.LocalId) .OnceAsync <object>(); // TODO: custom class to represent your data instead of just object // TODO: present your data MessageBox.Show(string.Join("\n", dbData.Select(d => d.ToString()))); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public async Task <ResponseData <bool> > SignUp(FirebaseAuthType type, string accessToken, User user = null) { var response = new ResponseData <bool> { Msg = "success", statusCode = 200 }; switch (type) { case FirebaseAuthType.EmailAndPassword: { var signUpTask = _provider.CreateUserWithEmailAndPasswordAsync(user.Email, user.Password, user.DisplayName); await signUpTask.ContinueWith((task) => { if (!task.IsFaulted && task.Result != null) { var result = task.Result; response.data = true; } else { response.data = false; response.statusCode = CodeDao.MOV.Models.Constants.CODE_AUTH; // response.Msg =( (FirebaseAuthException) (task.Exception.InnerException) ).Reason.ToString(); } }); return(response); } } return(response); }
public async Task <FirebaseAuthLink> SignInWithOAuthTwitterAsync(FirebaseAuthType authType, string oauthAccessToken, string oauthVerifier, string secret) { var providerId = this.GetProviderId(authType); var content = $"{{\"postBody\":\"access_token={oauthAccessToken}&providerId={providerId}&oauth_verifier={oauthVerifier}&oauth_token={oauthAccessToken}&oauth_token_secret={secret}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true,\"returnIdpCredential\":true}}"; return(await this.ExecuteWithPostContentAsync(GoogleIdentityUrl, content).ConfigureAwait(false)); }
/// <summary> /// Links the given <see cref="firebaseToken"/> with an account from a third party provider. /// </summary> /// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> LinkAccountsAsync(string firebaseToken, FirebaseAuthType authType, string oauthAccessToken) { var providerId = this.GetProviderId(authType); var content = $"{{\"idToken\":\"{firebaseToken}\",\"postBody\":\"access_token={oauthAccessToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; return(await this.ExecuteWithPostContentAsync(GoogleIdentityUrl, content).ConfigureAwait(false)); }
/// <summary> /// Unlinks the user from the given <see cref="authType"/> (provider). /// </summary> /// <param name="authType"> The auth type. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> UnlinkFromAsync(FirebaseAuthType authType) { var auth = await this.AuthProvider.UnlinkAccountsAsync(this, authType).ConfigureAwait(false); this.CopyPropertiesLocally(auth.AuthProvider, auth); return(this); }
private IObservable <Unit> SignInWithOAuth(FirebaseAuthType authType, string authToken) { return(_authProvider .SignInWithOAuthAsync(authType, authToken) .ToObservable() .Do(authLink => AuthLink = authLink) .SelectMany(authLink => SaveAccount(authLink))); }
/// <summary> /// Links the user with an account from a third party provider. /// </summary> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> LinkToAsync(FirebaseAuthType authType, string oauthAccessToken) { var auth = await this.AuthProvider.LinkAccountsAsync(this, authType, oauthAccessToken).ConfigureAwait(false); this.CopyPropertiesLocally(auth.AuthProvider, auth); return(this); }
/// <summary> /// Using the provided access token from third party auth provider (google, facebook...), get the firebase auth with token and basic user credentials. /// </summary> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuth"/>. </returns> public async Task <FirebaseAuthLink> SignInWithOAuthAsync(FirebaseAuthType authType, string oauthAccessToken) { var providerId = this.GetProviderId(authType); var content = $"{{\"postBody\":\"access_token={oauthAccessToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; FirebaseAuthLink firebaseAuthLink = await this.ExecuteWithPostContentAsync(GoogleIdentityUrl, content).ConfigureAwait(false); firebaseAuthLink.User = await this.GetUserAsync(firebaseAuthLink.FirebaseToken).ConfigureAwait(false); return(firebaseAuthLink); }
Task <ResponseData <bool> > IAuthentication.SignUp(FirebaseAuthType type, string accessToken, User user) { var response = new ResponseData <bool> { data = true, statusCode = 200, Msg = "success" }; return(new Task <ResponseData <bool> >(() => response)); }
public async Task <ResponseData <User> > SignIn(FirebaseAuthType type, string accessToken, User user) { var response = new ResponseData <User> { data = user, statusCode = 200, Msg = "success" }; return(response); }
async Task <bool> LoginFirebase(string token, FirebaseAuthType firebaseAuthType) { var authProvider = new FirebaseAuthProvider(new FirebaseConfig(AppConfigurations.FirebaseApiKey)); var auth = await authProvider.SignInWithOAuthAsync(firebaseAuthType, token); if (auth != null) { Console.WriteLine($"Logged in as {auth.User.DisplayName ?? auth.User.FirstName ?? auth.User.LastName}"); } return(auth != null); }
private string GetProviderId(FirebaseAuthType authType) { switch (authType) { case FirebaseAuthType.Facebook: case FirebaseAuthType.Google: case FirebaseAuthType.Github: case FirebaseAuthType.Twitter: return(authType.ToEnumString()); case FirebaseAuthType.EmailAndPassword: throw new InvalidOperationException("Email auth type cannot be used like this. Use methods specific to email & password authentication."); default: throw new NotImplementedException(""); } }
public async Task <ResponseData <User> > SignIn(FirebaseAuthType type, User user = null, string accessToken = null) { if (user != null && !user.Email.Equals("") && !user.Password.Equals("")) { return(await _auth.SignIn(type, accessToken, user)); } else { var response = new ResponseData <User> { statusCode = MOV.Models.Constants.CODE_NOT_FOUND, Msg = "Missing Data", data = null }; var tsk = newMethod(); return(await tsk); } }
/// <summary> /// Links the authenticated user represented by<see cref="firebaseToken"/> with and account from a third party provider. /// </summary> /// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <param name="twitterTokenSecret">only when provider is twitter</param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> LinkAccountsAsync(string firebaseToken, FirebaseAuthType authType, string oauthAccessToken, string twitterTokenSecret = null) { string tokenStr = "access_token"; string providerId = GetProviderId(authType); if (authType == FirebaseAuthType.Google) { tokenStr = "id_token"; } string content = $"{{\"idToken\":\"{firebaseToken}\",\"postBody\":\"{tokenStr}={oauthAccessToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; if (authType == FirebaseAuthType.Twitter) { content = $"{{\"idToken\":\"{firebaseToken}\",\"postBody\":\"{tokenStr}={oauthAccessToken}&oauth_token_secret={twitterTokenSecret}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; } return(await ExecuteWithPostContentAsync(GoogleIdentityUrl, content).ConfigureAwait(false)); }
private string GetProviderId(FirebaseAuthType authType) { switch (authType) { case FirebaseAuthType.Facebook: return("facebook.com"); case FirebaseAuthType.Google: return("google.com"); case FirebaseAuthType.Github: return("github.com"); case FirebaseAuthType.Twitter: return("twitter.com"); default: throw new NotImplementedException(""); } }
public static async Task <FirebaseAuthLink> GetAuthLink(FirebaseAuthType authType, string oauthAccessToken) { if (CheckConnection()) { if (AuthLink != null) { return(AuthLink); } await Initialize(authType, oauthAccessToken); //Save(); return(AuthLink); } else { return(null); } }
/// <summary> /// Using the provided access token from third party auth provider (google, facebook...), or ID token (apple), get the firebase auth with token and basic user credentials. /// </summary> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token or ID token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuth"/>. </returns> public async Task <FirebaseAuthLink> SignInWithOAuthAsync(FirebaseAuthType authType, string oauthAccessToken) { var providerId = this.GetProviderId(authType); string content; switch (authType) { case FirebaseAuthType.Apple: content = $"{{\"postBody\":\"id_token={oauthAccessToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; break; default: content = $"{{\"postBody\":\"access_token={oauthAccessToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; break; } FirebaseAuthLink firebaseAuthLink = await this.ExecuteWithPostContentAsync(GoogleIdentityUrl, content).ConfigureAwait(false); return(firebaseAuthLink); }
public static async Task <bool> EntrarComOAuth(FirebaseAuthType authType, string accessToken) { try { App.UsuarioLogadoAuth = await ConnectionDB.Authentication .SignInWithOAuthAsync(authType, accessToken); if (App.UsuarioLogadoAuth == null) { return(false); } ConnectionDB.InitializeData(App.UsuarioLogadoAuth.FirebaseToken); App.UsuarioLogado = await BuscarPeloCodigo(App.UsuarioLogadoAuth.User.LocalId); if (App.UsuarioLogado == null) { Usuario usuario = new Usuario() { Codigo = App.UsuarioLogadoAuth.User.LocalId, }; //CADASTRA AS INFORMAÇÕES DO USUARIO NO BANCO await ConnectionDB.Database.Child("Usuario").Child(usuario.Codigo).PostAsync(usuario); await ConnectionDB.Database.Child("Usuario").Child(usuario.Codigo).PutAsync(usuario); App.UsuarioLogado = await BuscarPeloCodigo(App.UsuarioLogadoAuth.User.LocalId); } return(true); } catch (FirebaseException e) { throw new Exception(e.Message); } }
/// <summary> /// Sign in with oauth provided with <paramref name="authType"/> and <paramref name="oauthToken"/>. /// </summary> /// <param name="authType"> /// The <see cref="FirebaseAuthType"/> of the oauth used. /// </param> /// <param name="oauthToken"> /// The token of the provided <paramref name="authType"/> type. /// </param> /// <returns> /// The <see cref="CallResult"/> of the specified task. /// </returns> public async Task <CallResult> SignInWithOAuth(FirebaseAuthType authType, string oauthToken) { try { var providerId = GetProviderId(authType); string content; switch (authType) { case FirebaseAuthType.Apple: content = $"{{\"postBody\":\"id_token={oauthToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; break; default: content = $"{{\"postBody\":\"access_token={oauthToken}&providerId={providerId}\",\"requestUri\":\"http://localhost\",\"returnSecureToken\":true}}"; break; } var auth = await ExecuteWithPostContent(GoogleIdentityUrl, content).ConfigureAwait(false); var refreshResult = await RefreshUserInfo(auth).ConfigureAwait(false); if (!refreshResult.IsSuccess) { return(refreshResult); } OnAuthenticated(); return(CallResult.Success()); } catch (FirebaseException ex) { return(CallResult.Error(ex)); } }
private static async Task Initialize(FirebaseAuthType authType, string oauthAccessToken) { var _authProvider = new FirebaseAuthProvider(new FirebaseConfig(APIKEY)); AuthLink = await _authProvider.SignInWithOAuthAsync(authType, oauthAccessToken); }
public async Task <ResponseData <bool> > SignUp(FirebaseAuthType type, string accessToken, User user) { return(await _auth.SignUp(type, accessToken, user)); }
public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (remoteError != null) { ErrorMessage = $"Error from external provider: {remoteError}"; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } // profile claims // Sign in the user with this external login provider if the user already has a login. FirebaseAuthType authType; string email = "", photoUrl = ""; if (info.ProviderDisplayName == "Facebook") { authType = FirebaseAuthType.Facebook; var claims = info.Principal.Identities.First().Claims; email = claims.ElementAt(1).Value; } else if (info.ProviderDisplayName == "Google") { authType = FirebaseAuthType.Google; var claims = info.Principal.Identities.First().Claims; email = claims.ElementAt(4).Value; photoUrl = claims.ElementAt(5).Value; } else { authType = new FirebaseAuthType(); } var token = info.AuthenticationTokens.First().Value; TokkepediaApiClient apiClient = new TokkepediaApiClient(); FirebaseAuthLink link = null; try { link = await apiClient.LoginOAuthAsync(info.ProviderDisplayName, token); } catch { } TokketUser user = await apiClient.GetUserAsync(link.User.LocalId); // If the user does not have an account, then ask the user to create an account. if (user == null) { ReturnUrl = returnUrl; LoginProvider = info.LoginProvider; if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) { Input = new InputModel { FirebaseToken = link.FirebaseToken, Id = link.User.LocalId, UserName = link.User.LocalId, DisplayName = info.Principal.Identity.Name, Email = info.Principal.FindFirstValue(ClaimTypes.Email) }; } return(Page()); } //Sign user in else { if (user.IsLockedOut) { return(RedirectToPage("./Lockout")); } await _signInManager.SignInAsync(user, true); _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); return(LocalRedirect(returnUrl)); } //var result = new Microsoft.AspNetCore.Identity.SignInResult();// await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor : true); //if (result.Succeeded) //{ // _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); // return LocalRedirect(returnUrl); //} //if (result.IsLockedOut) //{ // return RedirectToPage("./Lockout"); //} //else //{ // // If the user does not have an account, then ask the user to create an account. // ReturnUrl = returnUrl; // LoginProvider = info.LoginProvider; // if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) // { // Input = new InputModel // { // Email = info.Principal.FindFirstValue(ClaimTypes.Email) // }; // } // return Page(); //} }
/// <summary> /// Unlinks the given <see cref="authType"/> from the account associated with <see cref="firebaseToken"/>. /// </summary> /// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param> /// <param name="authType"> The auth type. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> UnlinkAccountsAsync(string firebaseToken, FirebaseAuthType authType) { string providerId = null; if (authType == FirebaseAuthType.EmailAndPassword) { providerId = authType.ToEnumString(); } else { providerId = this.GetProviderId(authType); } var content = $"{{\"idToken\":\"{firebaseToken}\",\"deleteProvider\":[\"{providerId}\"]}}"; return(await this.ExecuteWithPostContentAsync(GoogleSetAccountUrl, content).ConfigureAwait(false)); }
/// <summary> /// Links the this user with and account from a third party provider. /// </summary> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public Task <FirebaseAuthLink> LinkToAsync(FirebaseAuthType authType, string oauthAccessToken) { return(this.AuthProvider.LinkAccountsAsync(this, authType, oauthAccessToken)); }
/// <summary> /// Links the authenticated user represented by <see cref="auth"/> with an account from a third party provider. /// </summary> /// <param name="auth"> The auth. </param> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> LinkAccountsAsync(FirebaseAuth auth, FirebaseAuthType authType, string oauthAccessToken) { return(await this.LinkAccountsAsync(auth.FirebaseToken, authType, oauthAccessToken).ConfigureAwait(false)); }
/// <summary> /// Links the authenticated user represented by <see cref="auth"/> with and account from a third party provider. /// </summary> /// <param name="auth"> The auth. </param> /// <param name="authType"> The auth type. </param> /// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> LinkAccountsAsync(FirebaseAuth auth, FirebaseAuthType authType, string oauthAccessToken) { return(await LinkAccountsAsync(auth.FirebaseToken, authType, oauthAccessToken)); }
/// <summary> /// Unlinks the given <see cref="authType"/> from the authenticated user represented by <see cref="auth"/>. /// </summary> /// <param name="auth"> The auth. </param> /// <param name="authType"> The auth type. </param> /// <returns> The <see cref="FirebaseAuthLink"/>. </returns> public async Task <FirebaseAuthLink> UnlinkAccountsAsync(FirebaseAuth auth, FirebaseAuthType authType) { return(await this.UnlinkAccountsAsync(auth.FirebaseToken, authType).ConfigureAwait(false)); }
public async Task <ResponseData <User> > SignIn(FirebaseAuthType type, string accessToken, User user = null) { var response = new ResponseData <User> { Msg = "success", statusCode = 200 }; switch (type) { case FirebaseAuthType.EmailAndPassword: { try { Task <FirebaseAuthLink> authTask = _provider.SignInWithEmailAndPasswordAsync(user.Email, user.Password); FirebaseAuthLink authLink = await authTask; if (authLink.FirebaseToken != null) { string idAuth = authLink.User.LocalId; var client = new FirebaseClient(Constants.FIREBASE_URL_ROOT); var userInfor = await client.Child("users") .Child(idAuth) .WithAuth(() => authLink.FirebaseToken) .OnceSingleAsync <User>(); response.data = userInfor; return(response); } response.statusCode = MOV.Models.Constants.CODE_NOT_FOUND; response.Msg = MOV.Models.Constants.MSG_AUTH; return(response); } catch (FirebaseException firebaseException) { Console.WriteLine(firebaseException.Message); response.Msg = firebaseException.Message; response.statusCode = firebaseException.GetHashCode(); return(response); } catch (FirebaseAuthException firebaseAuthException) { Console.WriteLine(firebaseAuthException.Reason); response.Msg = firebaseAuthException.Reason.ToString(); response.statusCode = firebaseAuthException.Reason.GetHashCode(); return(response); } } case FirebaseAuthType.Facebook: { return(response); } case FirebaseAuthType.Google: { return(response); } } return(response); }