Files
server/src/Identity/IdentityServer/ApiClient.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
3.6 KiB
C#
Raw Normal View History

// FIXME: Update this file to be null safe and then delete the line below
#nullable disable
using Bit.Core.Settings;
using Bit.Identity.IdentityServer.RequestValidators;
using Duende.IdentityServer.Models;
2020-08-28 13:32:15 -04: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;
[PM-2032] Server endpoints to support authentication with a passkey (#3361) * [PM-2032] feat: add assertion options tokenable * [PM-2032] feat: add request and response models * [PM-2032] feat: implement `assertion-options` identity endpoint * [PM-2032] feat: implement authentication with passkey * [PM-2032] chore: rename to `WebAuthnGrantValidator` * [PM-2032] fix: add missing subsitute * [PM-2032] feat: start adding builder * [PM-2032] feat: add support for KeyConnector * [PM-2032] feat: add first version of TDE * [PM-2032] chore: refactor WithSso * [PM-2023] feat: add support for TDE feature flag * [PM-2023] feat: add support for approving devices * [PM-2023] feat: add support for hasManageResetPasswordPermission * [PM-2032] feat: add support for hasAdminApproval * [PM-2032] chore: don't supply device if not necessary * [PM-2032] chore: clean up imports * [PM-2023] feat: extract interface * [PM-2023] chore: add clarifying comment * [PM-2023] feat: use new builder in production code * [PM-2032] feat: add support for PRF * [PM-2032] chore: clean-up todos * [PM-2023] chore: remove token which is no longer used * [PM-2032] chore: remove todo * [PM-2032] feat: improve assertion error handling * [PM-2032] fix: linting issues * [PM-2032] fix: revert changes to `launchSettings.json` * [PM-2023] chore: clean up assertion endpoint * [PM-2032] feat: bypass 2FA * [PM-2032] fix: rename prf option to singular * [PM-2032] fix: lint * [PM-2032] fix: typo * [PM-2032] chore: improve builder tests Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com> * [PM-2032] chore: clarify why we don't require 2FA * [PM-2023] feat: move `identityProvider` constant to common class * [PM-2032] fix: lint * [PM-2023] fix: move `IdentityProvider` to core.Constants * [PM-2032] fix: missing import * [PM-2032] chore: refactor token timespan to use `TimeSpan` * [PM-2032] chore: make `StartWebAuthnLoginAssertion` sync * [PM-2032] chore: use `FromMinutes` * [PM-2032] fix: change to 17 minutes to cover webauthn assertion * [PM-2032] chore: do not use `async void` * [PM-2032] fix: comment saying wrong amount of minutes * [PM-2032] feat: put validator behind feature flag * [PM-2032] fix: lint --------- Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>
2023-11-20 15:55:31 +01:00
AllowedGrantTypes = new[] { GrantType.ResourceOwnerPassword, GrantType.AuthorizationCode, WebAuthnGrantValidator.GrantType };
// Use global setting: false = Sliding (default), true = Absolute
RefreshTokenExpiration = globalSettings.IdentityServer.ApplyAbsoluteExpirationOnRefreshToken
? TokenExpiration.Absolute
: TokenExpiration.Sliding;
2020-08-28 13:32:15 -04:00
RefreshTokenUsage = TokenUsage.ReUse;
// 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;
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")
{
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" };
}
2020-08-28 13:32:15 -04:00
else if (id == "connector")
{
2020-08-28 13:32:15 -04:00
var connectorUris = new List<string>();
for (var port = 8065; port <= 8070; port++)
{
2020-08-28 13:32:15 -04:00
connectorUris.Add(string.Format("http://localhost:{0}", port));
}
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++)
{
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;
}
}