/// <summary> /// Sets the specified custom claims on an existing user account. A null claims value /// removes any claims currently set on the user account. The claims should serialize into /// a valid JSON string. The serialized claims must not be larger than 1000 characters. /// </summary> /// <returns>A task that completes when the claims have been set.</returns> /// <exception cref="ArgumentException">If <paramref name="uid"/> is null, empty or longer /// than 128 characters. Or, if the serialized <paramref name="claims"/> is larger than 1000 /// characters.</exception> /// <param name="uid">The user ID string for the custom claims will be set. Must not be null /// or longer than 128 characters. /// </param> /// <param name="claims">The claims to be stored on the user account, and made /// available to Firebase security rules. These must be serializable to JSON, and after /// serialization it should not be larger than 1000 characters.</param> /// <param name="cancellationToken">A cancellation token to monitor the asynchronous /// operation.</param> public async Task SetCustomUserClaimsAsync( string uid, IReadOnlyDictionary <string, object> claims, CancellationToken cancellationToken) { var userManager = this.IfNotDeleted(() => this.userManager.Value); var user = new UserRecord(uid) { CustomClaims = claims, }; await userManager.UpdateUserAsync(user, cancellationToken).ConfigureAwait(false); }
/// <summary> /// Update an existing user. /// </summary> /// <exception cref="FirebaseException">If the server responds that cannot update the user.</exception> /// <param name="user">The user which we want to update.</param> /// <param name="cancellationToken">A cancellation token to monitor the asynchronous /// operation.</param> public async Task UpdateUserAsync( UserRecord user, CancellationToken cancellationToken = default(CancellationToken)) { const string updatePath = "accounts:update"; var response = await this.PostAndDeserializeAsync <JObject>( updatePath, user, cancellationToken).ConfigureAwait(false); if (user.Uid != (string)response["localId"]) { throw new FirebaseException($"Failed to update user: {user.Uid}"); } }
/// <summary> /// Initializes a new instance of the <see cref="UserRecord"/> class from an existing /// instance of the <see cref="GetAccountInfoResponse.User"/> class. /// </summary> /// <param name="user">The <see cref="GetAccountInfoResponse.User"/> instance to copy /// the user's data from.</param> internal UserRecord(GetAccountInfoResponse.User user) { if (user == null) { throw new ArgumentException("User object must not be null or empty."); } else if (string.IsNullOrEmpty(user.UserId)) { throw new ArgumentException("User ID must not be null or empty."); } this.Uid = user.UserId; this.Email = user.Email; this.PhoneNumber = user.PhoneNumber; this.EmailVerified = user.EmailVerified; this.DisplayName = user.DisplayName; this.PhotoUrl = user.PhotoUrl; this.Disabled = user.Disabled; if (user.Providers == null || user.Providers.Count == 0) { this.ProviderData = new IUserInfo[0]; } else { var count = user.Providers.Count; this.ProviderData = new IUserInfo[count]; for (int i = 0; i < count; i++) { this.ProviderData[i] = new ProviderUserInfo(user.Providers[i]); } } this.validSinceTimestampInSeconds = user.ValidSince; // newtonsoft's json deserializer will convert an iso8601 format // string to a (non-null) DateTime, returning 0001-01-01 if it's not // present in the proto. We'll compare against the epoch and only // use the deserialized value if it's bigger. DateTime?lastRefreshAt = null; if (user.LastRefreshAt > UnixEpoch) { lastRefreshAt = user.LastRefreshAt; } this.UserMetaData = new UserMetadata(user.CreatedAt, user.LastLoginAt, lastRefreshAt); this.CustomClaims = UserRecord.ParseCustomClaims(user.CustomClaims); this.TenantId = user.TenantId; }
/// <summary> /// Sets the specified custom claims on an existing user account. A null claims value /// removes any claims currently set on the user account. The claims must serialize into /// a valid JSON string. The serialized claims must not be larger than 1000 characters. /// </summary> /// <exception cref="ArgumentException">If <paramref name="uid"/> is null, empty or longer /// than 128 characters. Or, if the serialized <paramref name="claims"/> is larger than 1000 /// characters.</exception> /// <param name="uid">The user ID string for the custom claims will be set. Must not be null /// or longer than 128 characters. /// </param> /// <param name="claims">The claims to be stored on the user account, and made /// available to Firebase security rules. These must be serializable to JSON, and the /// serialized claims should not be larger than 1000 characters.</param> public async Task SetCustomUserClaimsAsync(string uid, IReadOnlyDictionary <string, object> claims) { lock (_lock) { if (_deleted) { throw new InvalidOperationException("Cannot invoke after deleting the app."); } } var user = new UserRecord(uid) { CustomClaims = claims }; await _userManager.Value.UpdateUserAsync(user); }
/// <summary> /// Update an existing user. /// </summary> /// <exception cref="FirebaseException">If the server responds that cannot update the user.</exception> /// <param name="user">The user which we want to update.</param> public async Task UpdateUserAsync(UserRecord user) { var updatePath = "/accounts:update"; var resopnse = await PostAsync(updatePath, user); try { var userResponse = resopnse.ToObject <UserRecord>(); if (userResponse.Uid != user.Uid) { throw new FirebaseException($"Failed to update user: {user.Uid}"); } } catch (Exception e) { throw new FirebaseException("Error while calling Firebase Auth service", e); } }
/// <summary> /// Update an existing user. /// </summary> /// <exception cref="FirebaseException">If the server responds that cannot update the user.</exception> /// <param name="user">The user which we want to update.</param> /// <param name="cancellationToken">A cancellation token to monitor the asynchronous /// operation.</param> public async Task UpdateUserAsync( UserRecord user, CancellationToken cancellationToken = default(CancellationToken)) { const string updatePath = "accounts:update"; var response = await this.PostAndDeserializeAsync <GetAccountInfoResponse>( updatePath, user, cancellationToken).ConfigureAwait(false); if (response == null || response.Users == null || response.Users.Count == 0) { throw new FirebaseException($"Failed to get user: {user.Uid}"); } var updatedUser = response.Users[0]; if (updatedUser == null || updatedUser.UserId != user.Uid) { throw new FirebaseException($"Failed to update user: {user.Uid}"); } }
/// <summary> /// Initializes a new instance of the <see cref="UserRecord"/> class from an existing instance of the <see cref="GetAccountInfoResponse.User"/> class. /// </summary> /// <param name="user">The <see cref="GetAccountInfoResponse.User"/> instance to copy the user's data from.</param> internal UserRecord(GetAccountInfoResponse.User user) { if (user == null) { throw new ArgumentException("User object must not be null or empty."); } else if (string.IsNullOrEmpty(user.UserId)) { throw new ArgumentException("User ID must not be null or empty."); } this.uid = user.UserId; this.email = user.Email; this.phoneNumber = user.PhoneNumber; this.emailVerified = user.EmailVerified; this.displayName = user.DisplayName; this.photoUrl = user.PhotoUrl; this.disabled = user.Disabled; if (user.Providers == null || user.Providers.Count == 0) { this.providers = new List <ProviderUserInfo>(); } else { var count = user.Providers.Count; this.providers = new List <ProviderUserInfo>(count); for (int i = 0; i < count; i++) { this.providers.Add(new ProviderUserInfo(user.Providers[i])); } } this.tokensValidAfterTimestamp = user.ValidSince * 1000; this.userMetaData = new UserMetadata(user.CreatedAt, user.LastLoginAt); this.customClaims = UserRecord.ParseCustomClaims(user.CustomClaims); }
/// <summary> /// Initializes a new instance of the <see cref="UserRecord"/> class from an existing instance of the /// <see cref="GetAccountInfoResponse.User"/> class. /// </summary> /// <param name="user">The <see cref="GetAccountInfoResponse.User"/> instance to copy the user's data /// from.</param> internal UserRecord(GetAccountInfoResponse.User user) { if (user == null) { throw new ArgumentException("User object must not be null or empty."); } else if (string.IsNullOrEmpty(user.UserId)) { throw new ArgumentException("User ID must not be null or empty."); } this.Uid = user.UserId; this.Email = user.Email; this.PhoneNumber = user.PhoneNumber; this.EmailVerified = user.EmailVerified; this.DisplayName = user.DisplayName; this.PhotoUrl = user.PhotoUrl; this.Disabled = user.Disabled; if (user.Providers == null || user.Providers.Count == 0) { this.ProviderData = new IUserInfo[0]; } else { var count = user.Providers.Count; this.ProviderData = new IUserInfo[count]; for (int i = 0; i < count; i++) { this.ProviderData[i] = new ProviderUserInfo(user.Providers[i]); } } this.validSinceTimestampInSeconds = user.ValidSince; this.UserMetaData = new UserMetadata(user.CreatedAt, user.LastLoginAt); this.CustomClaims = UserRecord.ParseCustomClaims(user.CustomClaims); }
internal override bool Matches(UserRecord userRecord) { return(this.uid == userRecord.Uid); }
internal override bool Matches(UserRecord userRecord) { return(userRecord.ProviderData.Any( userInfo => this.providerId == userInfo.ProviderId && this.providerUid == userInfo.Uid)); }
internal override bool Matches(UserRecord userRecord) { return(this.phoneNumber == userRecord.PhoneNumber); }
internal override bool Matches(UserRecord userRecord) { return(this.email == userRecord.Email); }
internal abstract bool Matches(UserRecord userRecord);