mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 15:27:27 +00:00
feat: reverted lockout in login completely
This commit is contained in:
@@ -45,10 +45,10 @@ type TIdentityUaServiceFactoryDep = {
|
|||||||
|
|
||||||
export type TIdentityUaServiceFactory = ReturnType<typeof identityUaServiceFactory>;
|
export type TIdentityUaServiceFactory = ReturnType<typeof identityUaServiceFactory>;
|
||||||
|
|
||||||
type LockoutObject = {
|
// type LockoutObject = {
|
||||||
lockedOut: boolean;
|
// lockedOut: boolean;
|
||||||
failedAttempts: number;
|
// failedAttempts: number;
|
||||||
};
|
// };
|
||||||
|
|
||||||
export const identityUaServiceFactory = ({
|
export const identityUaServiceFactory = ({
|
||||||
identityUaDAL,
|
identityUaDAL,
|
||||||
@@ -62,8 +62,15 @@ export const identityUaServiceFactory = ({
|
|||||||
const login = async (clientId: string, clientSecret: string, ip: string) => {
|
const login = async (clientId: string, clientSecret: string, ip: string) => {
|
||||||
const identityUa = await identityUaDAL.findOne({ clientId });
|
const identityUa = await identityUaDAL.findOne({ clientId });
|
||||||
if (!identityUa) {
|
if (!identityUa) {
|
||||||
throw new UnauthorizedError({
|
throw new NotFoundError({
|
||||||
message: "Invalid credentials"
|
message: "No identity with specified client ID was found"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityUa.identityId });
|
||||||
|
if (!identityMembershipOrg) {
|
||||||
|
throw new NotFoundError({
|
||||||
|
message: "No identity with the org membership was found"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,119 +78,69 @@ export const identityUaServiceFactory = ({
|
|||||||
ipAddress: ip,
|
ipAddress: ip,
|
||||||
trustedIps: identityUa.clientSecretTrustedIps as TIp[]
|
trustedIps: identityUa.clientSecretTrustedIps as TIp[]
|
||||||
});
|
});
|
||||||
|
const clientSecretPrefix = clientSecret.slice(0, 4);
|
||||||
|
const clientSecrtInfo = await identityUaClientSecretDAL.find({
|
||||||
|
identityUAId: identityUa.id,
|
||||||
|
isClientSecretRevoked: false,
|
||||||
|
clientSecretPrefix
|
||||||
|
});
|
||||||
|
|
||||||
const LOCKOUT_KEY = `lockout:identity:${identityUa.identityId}:${IdentityAuthMethod.UNIVERSAL_AUTH}:${clientId}`;
|
let validClientSecretInfo: (typeof clientSecrtInfo)[0] | null = null;
|
||||||
|
for await (const info of clientSecrtInfo) {
|
||||||
|
const isMatch = await crypto.hashing().compareHash(clientSecret, info.clientSecretHash);
|
||||||
|
|
||||||
const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityUa.identityId });
|
if (isMatch) {
|
||||||
if (!identityMembershipOrg) {
|
validClientSecretInfo = info;
|
||||||
throw new UnauthorizedError({
|
break;
|
||||||
message: "Invalid credentials"
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const identityTx = await identityUaDAL.transaction(async (tx) => {
|
if (!validClientSecretInfo) throw new UnauthorizedError({ message: "Invalid credentials" });
|
||||||
// await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.IdentityLogin(identityUa.identityId, clientId)]);
|
|
||||||
|
|
||||||
// Lockout Check
|
const { clientSecretTTL, clientSecretNumUses, clientSecretNumUsesLimit } = validClientSecretInfo;
|
||||||
const lockoutRaw = await keyStore.getItem(LOCKOUT_KEY);
|
if (Number(clientSecretTTL) > 0) {
|
||||||
|
const clientSecretCreated = new Date(validClientSecretInfo.createdAt);
|
||||||
|
const ttlInMilliseconds = Number(clientSecretTTL) * 1000;
|
||||||
|
const currentDate = new Date();
|
||||||
|
const expirationTime = new Date(clientSecretCreated.getTime() + ttlInMilliseconds);
|
||||||
|
|
||||||
let lockout: LockoutObject | undefined;
|
if (currentDate > expirationTime) {
|
||||||
if (lockoutRaw) {
|
|
||||||
lockout = JSON.parse(lockoutRaw) as LockoutObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lockout && lockout.lockedOut) {
|
|
||||||
throw new UnauthorizedError({
|
|
||||||
message: "This identity auth method is temporarily locked, please try again later"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const clientSecretPrefix = clientSecret.slice(0, 4);
|
|
||||||
const clientSecretInfo = await identityUaClientSecretDAL.find({
|
|
||||||
identityUAId: identityUa.id,
|
|
||||||
isClientSecretRevoked: false,
|
|
||||||
clientSecretPrefix
|
|
||||||
});
|
|
||||||
|
|
||||||
let validClientSecretInfo: (typeof clientSecretInfo)[0] | null = null;
|
|
||||||
for await (const info of clientSecretInfo) {
|
|
||||||
const isMatch = await crypto.hashing().compareHash(clientSecret, info.clientSecretHash);
|
|
||||||
|
|
||||||
if (isMatch) {
|
|
||||||
validClientSecretInfo = info;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!validClientSecretInfo) {
|
|
||||||
if (identityUa.lockoutEnabled) {
|
|
||||||
if (!lockout) {
|
|
||||||
lockout = {
|
|
||||||
lockedOut: false,
|
|
||||||
failedAttempts: 0
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
lockout.failedAttempts += 1;
|
|
||||||
if (lockout.failedAttempts >= identityUa.lockoutThreshold) {
|
|
||||||
lockout.lockedOut = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
await keyStore.setItemWithExpiry(
|
|
||||||
LOCKOUT_KEY,
|
|
||||||
lockout.lockedOut ? identityUa.lockoutDurationSeconds : identityUa.lockoutCounterResetSeconds,
|
|
||||||
JSON.stringify(lockout)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UnauthorizedError({ message: "Invalid credentials" });
|
|
||||||
} else if (lockout) {
|
|
||||||
await keyStore.deleteItem(LOCKOUT_KEY);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { clientSecretTTL, clientSecretNumUses, clientSecretNumUsesLimit } = validClientSecretInfo;
|
|
||||||
if (Number(clientSecretTTL) > 0) {
|
|
||||||
const clientSecretCreated = new Date(validClientSecretInfo.createdAt);
|
|
||||||
const ttlInMilliseconds = Number(clientSecretTTL) * 1000;
|
|
||||||
const currentDate = new Date();
|
|
||||||
const expirationTime = new Date(clientSecretCreated.getTime() + ttlInMilliseconds);
|
|
||||||
|
|
||||||
if (currentDate > expirationTime) {
|
|
||||||
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
|
||||||
isClientSecretRevoked: true
|
|
||||||
});
|
|
||||||
|
|
||||||
throw new UnauthorizedError({
|
|
||||||
message: "Access denied due to expired client secret"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (clientSecretNumUsesLimit > 0 && clientSecretNumUses === clientSecretNumUsesLimit) {
|
|
||||||
// number of times client secret can be used for
|
|
||||||
// a login operation reached
|
|
||||||
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
||||||
isClientSecretRevoked: true
|
isClientSecretRevoked: true
|
||||||
});
|
});
|
||||||
|
|
||||||
throw new UnauthorizedError({
|
throw new UnauthorizedError({
|
||||||
message: "Access denied due to client secret usage limit reached"
|
message: "Access denied due to expired client secret"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const accessTokenTTLParams =
|
if (clientSecretNumUsesLimit > 0 && clientSecretNumUses === clientSecretNumUsesLimit) {
|
||||||
Number(identityUa.accessTokenPeriod) === 0
|
// number of times client secret can be used for
|
||||||
? {
|
// a login operation reached
|
||||||
accessTokenTTL: identityUa.accessTokenTTL,
|
await identityUaClientSecretDAL.updateById(validClientSecretInfo.id, {
|
||||||
accessTokenMaxTTL: identityUa.accessTokenMaxTTL
|
isClientSecretRevoked: true
|
||||||
}
|
});
|
||||||
: {
|
throw new UnauthorizedError({
|
||||||
accessTokenTTL: identityUa.accessTokenPeriod,
|
message: "Access denied due to client secret usage limit reached"
|
||||||
// We set a very large Max TTL for periodic tokens to ensure that clients (even outdated ones) can always renew their token
|
});
|
||||||
// without them having to update their SDKs, CLIs, etc. This workaround sets it to 30 years to emulate "forever"
|
}
|
||||||
accessTokenMaxTTL: 1000000000
|
|
||||||
};
|
|
||||||
|
|
||||||
const uaClientSecretDoc = await identityUaClientSecretDAL.incrementUsage(validClientSecretInfo.id, tx);
|
const accessTokenTTLParams =
|
||||||
|
Number(identityUa.accessTokenPeriod) === 0
|
||||||
|
? {
|
||||||
|
accessTokenTTL: identityUa.accessTokenTTL,
|
||||||
|
accessTokenMaxTTL: identityUa.accessTokenMaxTTL
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
accessTokenTTL: identityUa.accessTokenPeriod,
|
||||||
|
// We set a very large Max TTL for periodic tokens to ensure that clients (even outdated ones) can always renew their token
|
||||||
|
// without them having to update their SDKs, CLIs, etc. This workaround sets it to 30 years to emulate "forever"
|
||||||
|
accessTokenMaxTTL: 1000000000
|
||||||
|
};
|
||||||
|
|
||||||
|
const identityAccessToken = await identityUaDAL.transaction(async (tx) => {
|
||||||
|
const uaClientSecretDoc = await identityUaClientSecretDAL.incrementUsage(validClientSecretInfo!.id, tx);
|
||||||
await identityOrgMembershipDAL.updateById(
|
await identityOrgMembershipDAL.updateById(
|
||||||
identityMembershipOrg.id,
|
identityMembershipOrg.id,
|
||||||
{
|
{
|
||||||
@@ -206,33 +163,33 @@ export const identityUaServiceFactory = ({
|
|||||||
tx
|
tx
|
||||||
);
|
);
|
||||||
|
|
||||||
return { newToken, validClientSecretInfo, accessTokenTTLParams };
|
return newToken;
|
||||||
});
|
});
|
||||||
|
|
||||||
const appCfg = getConfig();
|
const appCfg = getConfig();
|
||||||
const accessToken = crypto.jwt().sign(
|
const accessToken = crypto.jwt().sign(
|
||||||
{
|
{
|
||||||
identityId: identityUa.identityId,
|
identityId: identityUa.identityId,
|
||||||
clientSecretId: identityTx.validClientSecretInfo.id,
|
clientSecretId: validClientSecretInfo.id,
|
||||||
identityAccessTokenId: identityTx.newToken.id,
|
identityAccessTokenId: identityAccessToken.id,
|
||||||
authTokenType: AuthTokenType.IDENTITY_ACCESS_TOKEN
|
authTokenType: AuthTokenType.IDENTITY_ACCESS_TOKEN
|
||||||
} as TIdentityAccessTokenJwtPayload,
|
} as TIdentityAccessTokenJwtPayload,
|
||||||
appCfg.AUTH_SECRET,
|
appCfg.AUTH_SECRET,
|
||||||
// akhilmhdh: for non-expiry tokens you should not even set the value, including undefined. Even for undefined jsonwebtoken throws error
|
// akhilmhdh: for non-expiry tokens you should not even set the value, including undefined. Even for undefined jsonwebtoken throws error
|
||||||
Number(identityTx.newToken.accessTokenTTL) === 0
|
Number(identityAccessToken.accessTokenTTL) === 0
|
||||||
? undefined
|
? undefined
|
||||||
: {
|
: {
|
||||||
expiresIn: Number(identityTx.newToken.accessTokenTTL)
|
expiresIn: Number(identityAccessToken.accessTokenTTL)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
accessToken,
|
accessToken,
|
||||||
identityUa,
|
identityUa,
|
||||||
validClientSecretInfo: identityTx.validClientSecretInfo,
|
validClientSecretInfo,
|
||||||
identityAccessToken: identityTx.newToken,
|
identityAccessToken,
|
||||||
identityMembershipOrg,
|
identityMembershipOrg,
|
||||||
...identityTx.accessTokenTTLParams
|
...accessTokenTTLParams
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user