2025-07-08 10:25:41 -04:00
|
|
|
|
// FIXME: Update this file to be null safe and then delete the line below
|
|
|
|
|
|
#nullable disable
|
|
|
|
|
|
|
|
|
|
|
|
using Bit.Core.Settings;
|
2024-10-24 10:41:25 -07:00
|
|
|
|
using Bit.Identity.IdentityServer.RequestValidators;
|
2023-11-20 16:32:23 -05:00
|
|
|
|
using Duende.IdentityServer.Models;
|
2020-08-28 13:32:15 -04:00
|
|
|
|
|
2022-09-27 18:30:37 +02:00
|
|
|
|
namespace Bit.Identity.IdentityServer;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2020-08-28 13:32:15 -04:00
|
|
|
|
public class ApiClient : Client
|
|
|
|
|
|
{
|
|
|
|
|
|
public ApiClient(
|
|
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
string id,
|
|
|
|
|
|
int refreshTokenSlidingDays,
|
|
|
|
|
|
int accessTokenLifetimeHours,
|
|
|
|
|
|
string[] scopes = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ClientId = id;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
AllowedGrantTypes = new[] { GrantType.ResourceOwnerPassword, GrantType.AuthorizationCode, WebAuthnGrantValidator.GrantType };
|
2025-09-09 12:30:58 -07:00
|
|
|
|
|
|
|
|
|
|
// Use global setting: false = Sliding (default), true = Absolute
|
2025-09-24 18:23:15 -04:00
|
|
|
|
RefreshTokenExpiration = globalSettings.IdentityServer.ApplyAbsoluteExpirationOnRefreshToken
|
2025-09-09 12:30:58 -07:00
|
|
|
|
? TokenExpiration.Absolute
|
|
|
|
|
|
: TokenExpiration.Sliding;
|
|
|
|
|
|
|
2020-08-28 13:32:15 -04:00
|
|
|
|
RefreshTokenUsage = TokenUsage.ReUse;
|
2025-09-09 12:30:58 -07:00
|
|
|
|
|
|
|
|
|
|
// Use global setting if provided, otherwise use constructor parameter
|
|
|
|
|
|
SlidingRefreshTokenLifetime = globalSettings.IdentityServer.SlidingRefreshTokenLifetimeSeconds ?? (86400 * refreshTokenSlidingDays);
|
|
|
|
|
|
AbsoluteRefreshTokenLifetime = globalSettings.IdentityServer.AbsoluteRefreshTokenLifetimeSeconds ?? 0; // forever
|
|
|
|
|
|
|
2020-08-28 13:32:15 -04:00
|
|
|
|
UpdateAccessTokenClaimsOnRefresh = true;
|
|
|
|
|
|
AccessTokenLifetime = 3600 * accessTokenLifetimeHours;
|
|
|
|
|
|
AllowOfflineAccess = true;
|
2022-08-29 15:53:48 -04:00
|
|
|
|
|
2020-08-28 13:32:15 -04:00
|
|
|
|
RequireConsent = false;
|
|
|
|
|
|
RequirePkce = true;
|
|
|
|
|
|
RequireClientSecret = false;
|
|
|
|
|
|
if (id == "web")
|
|
|
|
|
|
{
|
|
|
|
|
|
RedirectUris = new[] { $"{globalSettings.BaseServiceUri.Vault}/sso-connector.html" };
|
|
|
|
|
|
PostLogoutRedirectUris = new[] { globalSettings.BaseServiceUri.Vault };
|
|
|
|
|
|
AllowedCorsOrigins = new[] { globalSettings.BaseServiceUri.Vault };
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (id == "desktop")
|
|
|
|
|
|
{
|
2024-08-26 15:11:48 +02:00
|
|
|
|
var desktopUris = new List<string>();
|
|
|
|
|
|
desktopUris.Add("bitwarden://sso-callback");
|
|
|
|
|
|
for (var port = 8065; port <= 8070; port++)
|
|
|
|
|
|
{
|
|
|
|
|
|
desktopUris.Add(string.Format("http://localhost:{0}", port));
|
|
|
|
|
|
}
|
|
|
|
|
|
RedirectUris = desktopUris;
|
2020-08-28 13:32:15 -04:00
|
|
|
|
PostLogoutRedirectUris = new[] { "bitwarden://logged-out" };
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2020-08-28 13:32:15 -04:00
|
|
|
|
else if (id == "connector")
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2020-08-28 13:32:15 -04:00
|
|
|
|
var connectorUris = new List<string>();
|
|
|
|
|
|
for (var port = 8065; port <= 8070; port++)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2020-08-28 13:32:15 -04:00
|
|
|
|
connectorUris.Add(string.Format("http://localhost:{0}", port));
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2020-08-28 13:32:15 -04:00
|
|
|
|
RedirectUris = connectorUris.Append("bwdc://sso-callback").ToList();
|
|
|
|
|
|
PostLogoutRedirectUris = connectorUris.Append("bwdc://logged-out").ToList();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-08-28 13:32:15 -04:00
|
|
|
|
else if (id == "browser")
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-08-28 13:32:15 -04:00
|
|
|
|
RedirectUris = new[] { $"{globalSettings.BaseServiceUri.Vault}/sso-connector.html" };
|
|
|
|
|
|
PostLogoutRedirectUris = new[] { globalSettings.BaseServiceUri.Vault };
|
|
|
|
|
|
AllowedCorsOrigins = new[] { globalSettings.BaseServiceUri.Vault };
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-08-28 13:32:15 -04:00
|
|
|
|
else if (id == "cli")
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-08-28 13:32:15 -04:00
|
|
|
|
var cliUris = new List<string>();
|
|
|
|
|
|
for (var port = 8065; port <= 8070; port++)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2020-08-28 13:32:15 -04:00
|
|
|
|
cliUris.Add(string.Format("http://localhost:{0}", port));
|
|
|
|
|
|
}
|
|
|
|
|
|
RedirectUris = cliUris;
|
|
|
|
|
|
PostLogoutRedirectUris = cliUris;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-08-28 13:32:15 -04:00
|
|
|
|
else if (id == "mobile")
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-08-28 13:32:15 -04:00
|
|
|
|
RedirectUris = new[] { "bitwarden://sso-callback" };
|
|
|
|
|
|
PostLogoutRedirectUris = new[] { "bitwarden://logged-out" };
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-08-28 13:32:15 -04:00
|
|
|
|
|
|
|
|
|
|
if (scopes == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
scopes = new string[] { "api" };
|
|
|
|
|
|
}
|
|
|
|
|
|
AllowedScopes = scopes;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|