Skip to content

feat(auth): Add LinkDomain to ActionCodeSettings and deprecate DynamicLinkDomain #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ internal static ActionCodeSettings InitActionCodeSettings()
AndroidPackageName = "com.example.android",
AndroidInstallApp = true,
AndroidMinimumVersion = "12",
DynamicLinkDomain = "coolapp.page.link",
LinkDomain = "coolapp.page.link",
};
// [END init_action_code_settings]
return actionCodeSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public class AuthErrorHandlerTest
ErrorCode.NotFound,
AuthErrorCode.EmailNotFound,
},
new object[]
{
"INVALID_HOSTING_LINK_DOMAIN",
ErrorCode.InvalidArgument,
AuthErrorCode.InvalidHostingLinkDomain,
},
};

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ public class EmailActionRequestTest
new ActionCodeSettings()
{
Url = "https://example.dynamic.link",
#pragma warning disable CS0618
DynamicLinkDomain = string.Empty,
#pragma warning restore CS0618
},
},
new object[]
{
new ActionCodeSettings()
{
Url = "https://example.dynamic.link",
LinkDomain = string.Empty,
},
},
new object[]
Expand Down Expand Up @@ -84,7 +94,10 @@ public class EmailActionRequestTest
{
Url = "https://example.dynamic.link",
HandleCodeInApp = true,
#pragma warning disable CS0618
DynamicLinkDomain = "custom.page.link",
#pragma warning restore CS0618
LinkDomain = "custom.page.link",
IosBundleId = "com.example.ios",
AndroidPackageName = "com.example.android",
AndroidMinimumVersion = "6",
Expand Down Expand Up @@ -163,14 +176,17 @@ public async Task EmailVerificationLinkWithSettings()

var request = NewtonsoftJsonSerializer.Instance.Deserialize<Dictionary<string, object>>(
handler.LastRequestBody);
Assert.Equal(10, request.Count);
Assert.Equal(11, request.Count);
Assert.Equal("[email protected]", request["email"]);
Assert.Equal("VERIFY_EMAIL", request["requestType"]);
Assert.True((bool)request["returnOobLink"]);

Assert.Equal(ActionCodeSettings.Url, request["continueUrl"]);
Assert.True((bool)request["canHandleCodeInApp"]);
#pragma warning disable CS0618
Assert.Equal(ActionCodeSettings.DynamicLinkDomain, request["dynamicLinkDomain"]);
#pragma warning restore CS0618
Assert.Equal(ActionCodeSettings.LinkDomain, request["linkDomain"]);
Assert.Equal(ActionCodeSettings.IosBundleId, request["iOSBundleId"]);
Assert.Equal(ActionCodeSettings.AndroidPackageName, request["androidPackageName"]);
Assert.Equal(
Expand Down Expand Up @@ -229,14 +245,17 @@ public async Task PasswordResetLinkWithSettings()

var request = NewtonsoftJsonSerializer.Instance.Deserialize<Dictionary<string, object>>(
handler.LastRequestBody);
Assert.Equal(10, request.Count);
Assert.Equal(11, request.Count);
Assert.Equal("[email protected]", request["email"]);
Assert.Equal("PASSWORD_RESET", request["requestType"]);
Assert.True((bool)request["returnOobLink"]);

Assert.Equal(ActionCodeSettings.Url, request["continueUrl"]);
Assert.True((bool)request["canHandleCodeInApp"]);
#pragma warning disable CS0618
Assert.Equal(ActionCodeSettings.DynamicLinkDomain, request["dynamicLinkDomain"]);
#pragma warning restore CS0618
Assert.Equal(ActionCodeSettings.LinkDomain, request["linkDomain"]);
Assert.Equal(ActionCodeSettings.IosBundleId, request["iOSBundleId"]);
Assert.Equal(ActionCodeSettings.AndroidPackageName, request["androidPackageName"]);
Assert.Equal(
Expand Down Expand Up @@ -287,14 +306,17 @@ public async Task SignInWithEmailLink()

var request = NewtonsoftJsonSerializer.Instance.Deserialize<Dictionary<string, object>>(
handler.LastRequestBody);
Assert.Equal(10, request.Count);
Assert.Equal(11, request.Count);
Assert.Equal("[email protected]", request["email"]);
Assert.Equal("EMAIL_SIGNIN", request["requestType"]);
Assert.True((bool)request["returnOobLink"]);

Assert.Equal(ActionCodeSettings.Url, request["continueUrl"]);
Assert.True((bool)request["canHandleCodeInApp"]);
#pragma warning disable CS0618
Assert.Equal(ActionCodeSettings.DynamicLinkDomain, request["dynamicLinkDomain"]);
#pragma warning restore CS0618
Assert.Equal(ActionCodeSettings.LinkDomain, request["linkDomain"]);
Assert.Equal(ActionCodeSettings.IosBundleId, request["iOSBundleId"]);
Assert.Equal(ActionCodeSettings.AndroidPackageName, request["androidPackageName"]);
Assert.Equal(
Expand Down Expand Up @@ -351,6 +373,39 @@ public async Task InvalidDynamicLinkDomain()
Assert.Null(exception.InnerException);
}

[Fact]
public async Task InvalidHostingLinkDomain()
{
var json = $@"{{
""error"": {{
""message"": ""INVALID_HOSTING_LINK_DOMAIN"",
}}
}}";
var handler = new MockMessageHandler()
{
StatusCode = HttpStatusCode.InternalServerError,
Response = json,
};
var auth = this.CreateFirebaseAuth(handler);
var settings = new ActionCodeSettings()
{
Url = "https://example.dynamic.link",
LinkDomain = "custom.page.link",
};

var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
async () => await auth.GenerateSignInWithEmailLinkAsync(
"[email protected]", settings));

Assert.Equal(ErrorCode.InvalidArgument, exception.ErrorCode);
Assert.Equal(AuthErrorCode.InvalidHostingLinkDomain, exception.AuthErrorCode);
Assert.Equal(
"The provided hosting link domain is not configured in Firebase Hosting or is not owned by the current project (INVALID_HOSTING_LINK_DOMAIN).",
exception.Message);
Assert.NotNull(exception.HttpResponse);
Assert.Null(exception.InnerException);
}

private FirebaseAuth CreateFirebaseAuth(HttpMessageHandler handler)
{
var userManager = new FirebaseUserManager(new FirebaseUserManager.Args
Expand Down
9 changes: 9 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Auth/ActionCodeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ public sealed class ActionCodeSettings
/// </summary>
public bool HandleCodeInApp { get; set; }

/// <summary>
/// Gets or sets the Hosting Link domain to use for the current link if it is to be opened
/// using handleCodeInApp, as multiple hosting link domains can be configured per
/// project. This setting provides the ability to explicitly choose one. If none is provided,
/// the default hosting domain is used.
/// </summary>
public string LinkDomain { get; set; }

/// <summary>
/// Gets or sets the dynamic link domain to use for the current link if it is to be opened
/// using Firebase Dynamic Links, as multiple dynamic link domains can be configured per
/// project. This setting provides the ability to explicitly choose one. If none is provided,
/// the oldest domain is used by default.
/// </summary>
[System.Obsolete("DynamicLinkDomain is deprecated, use LinkDomain instead")]
public string DynamicLinkDomain { get; set; }

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public enum AuthErrorCode
/// </summary>
InvalidDynamicLinkDomain,

/// <summary>
/// The provided hosting link domain is not configured in Firebase Hosting or is not owned by the current project.
/// </summary>
InvalidHostingLinkDomain,

/// <summary>
/// The specified ID token has been revoked.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ internal sealed class AuthErrorHandler
AuthErrorCode.InvalidDynamicLinkDomain,
"Dynamic link domain specified in ActionCodeSettings is not authorized")
},
{
"INVALID_HOSTING_LINK_DOMAIN",
new ErrorInfo(
ErrorCode.InvalidArgument,
AuthErrorCode.InvalidHostingLinkDomain,
"The provided hosting link domain is not configured in Firebase Hosting or is not owned by the current project")
},
{
"PHONE_NUMBER_EXISTS",
new ErrorInfo(
Expand Down
11 changes: 11 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Auth/Users/EmailActionLinkRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ private EmailActionLinkRequest(string type, string email, ActionCodeSettings set
{
this.Url = settings.Url;
this.HandleCodeInApp = settings.HandleCodeInApp;
#pragma warning disable CS0618
this.DynamicLinkDomain = settings.DynamicLinkDomain;
#pragma warning restore CS0618
this.LinkDomain = settings.LinkDomain;
this.IosBundleId = settings.IosBundleId;
this.AndroidPackageName = settings.AndroidPackageName;
this.AndroidMinimumVersion = settings.AndroidMinimumVersion;
Expand Down Expand Up @@ -70,6 +73,9 @@ private EmailActionLinkRequest(string type, string email, ActionCodeSettings set
[JsonProperty("dynamicLinkDomain")]
internal string DynamicLinkDomain { get; }

[JsonProperty("linkDomain")]
internal string LinkDomain { get; }

[JsonProperty("iOSBundleId")]
internal string IosBundleId { get; }

Expand Down Expand Up @@ -125,6 +131,11 @@ private void ValidateSettings()
throw new ArgumentException("DynamicLinkDomain must not be empty");
}

if (this.LinkDomain == string.Empty)
{
throw new ArgumentException("LinkDomain must not be empty");
}

if (this.IosBundleId == string.Empty)
{
throw new ArgumentException("IosBundleId must not be empty");
Expand Down