Skip to content

Commit da0d256

Browse files
✨ Throw CoreFcaApiSireneDownException on SIRENE connection failure
1 parent dffa09e commit da0d256

6 files changed

Lines changed: 87 additions & 10 deletions

File tree

back/_doc/erreurs.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@
7171

7272
## @fc/core-fca-low
7373

74-
| Code d'erreur | Classe | Code HTTP | error | error_description |
75-
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | --------- | ------------- | --------------------------------------------------------------------------- |
76-
| [<b>500001</b>](../apps/core-fca-low/src/exceptions/core-fca-no-idp.exception.ts) | [CoreFcaAgentNoIdpException](../apps/core-fca-low/src/exceptions/core-fca-no-idp.exception.ts) | 400 | server_error | authentication aborted due to a technical error on the authorization server |
77-
| [<b>500006</b>](../apps/core-fca-low/src/exceptions/core-fca-invalid-identity.exception.ts) | [CoreFcaInvalidIdentityException](../apps/core-fca-low/src/exceptions/core-fca-invalid-identity.exception.ts) | 400 | server_error | authentication aborted due to a technical error on the authorization server |
78-
| [<b>500015</b>](../apps/core-fca-low/src/exceptions/core-fca-agent-not-from-public-service.exception.ts) | [CoreFcaAgentNotFromPublicServiceException](../apps/core-fca-low/src/exceptions/core-fca-agent-not-from-public-service.exception.ts) | 400 | access_denied | authentication aborted due to invalid identity |
79-
| [<b>500024</b>](../apps/core-fca-low/src/exceptions/core-fca-account-blocked.exception.ts) | [CoreFcaAgentAccountBlockedException](../apps/core-fca-low/src/exceptions/core-fca-account-blocked.exception.ts) | 400 | access_denied | authentication aborted due to invalid identity |
80-
| [<b>500025</b>](../apps/core-fca-low/src/exceptions/core-fca-unauthorized-email.exception.ts) | [CoreFcaUnauthorizedEmailException](../apps/core-fca-low/src/exceptions/core-fca-unauthorized-email.exception.ts) | 400 | access_denied | authentication aborted due to a configuration limitation |
81-
| [<b>500027</b>](../apps/core-fca-low/src/exceptions/core-fca-invalid-email-domain.exception.ts) | [CoreFcaInvalidEmailDomainException](../apps/core-fca-low/src/exceptions/core-fca-invalid-email-domain.exception.ts) | 400 | server_error | authentication aborted due to a technical error on the authorization server |
74+
| Code d'erreur | Classe | Code HTTP | error | error_description |
75+
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | --------- | ----------------------- | --------------------------------------------------------------------------- |
76+
| [<b>500001</b>](../apps/core-fca-low/src/exceptions/core-fca-no-idp.exception.ts) | [CoreFcaAgentNoIdpException](../apps/core-fca-low/src/exceptions/core-fca-no-idp.exception.ts) | 400 | server_error | authentication aborted due to a technical error on the authorization server |
77+
| [<b>500006</b>](../apps/core-fca-low/src/exceptions/core-fca-invalid-identity.exception.ts) | [CoreFcaInvalidIdentityException](../apps/core-fca-low/src/exceptions/core-fca-invalid-identity.exception.ts) | 400 | server_error | authentication aborted due to a technical error on the authorization server |
78+
| [<b>500015</b>](../apps/core-fca-low/src/exceptions/core-fca-agent-not-from-public-service.exception.ts) | [CoreFcaAgentNotFromPublicServiceException](../apps/core-fca-low/src/exceptions/core-fca-agent-not-from-public-service.exception.ts) | 400 | access_denied | authentication aborted due to invalid identity |
79+
| [<b>500024</b>](../apps/core-fca-low/src/exceptions/core-fca-account-blocked.exception.ts) | [CoreFcaAgentAccountBlockedException](../apps/core-fca-low/src/exceptions/core-fca-account-blocked.exception.ts) | 400 | access_denied | authentication aborted due to invalid identity |
80+
| [<b>500025</b>](../apps/core-fca-low/src/exceptions/core-fca-unauthorized-email.exception.ts) | [CoreFcaUnauthorizedEmailException](../apps/core-fca-low/src/exceptions/core-fca-unauthorized-email.exception.ts) | 400 | access_denied | authentication aborted due to a configuration limitation |
81+
| [<b>500027</b>](../apps/core-fca-low/src/exceptions/core-fca-invalid-email-domain.exception.ts) | [CoreFcaInvalidEmailDomainException](../apps/core-fca-low/src/exceptions/core-fca-invalid-email-domain.exception.ts) | 400 | server_error | authentication aborted due to a technical error on the authorization server |
82+
| [<b>500028</b>](../apps/core-fca-low/src/exceptions/core-fca-api-sirene-down.exception.ts) | [CoreFcaApiSireneDownException](../apps/core-fca-low/src/exceptions/core-fca-api-sirene-down.exception.ts) | 503 | temporarily_unavailable | authentication temporarily unavailable due to sirene api failure |

back/apps/core-fca-low/src/enums/error-code.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export enum ErrorCode {
55
BLOCKED_ACCOUNT = 24,
66
UNAUTHORIZED_EMAIL = 25,
77
INVALID_EMAIL_DOMAIN = 27,
8+
API_SIRENE_DOWN = 28,
89
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { HttpStatus } from "@nestjs/common";
2+
import { ErrorCode } from "../enums";
3+
import { CoreFcaBaseException } from "./core-fca-base.exception";
4+
5+
export class CoreFcaApiSireneDownException extends CoreFcaBaseException {
6+
public code = ErrorCode.API_SIRENE_DOWN;
7+
public http_status_code = HttpStatus.SERVICE_UNAVAILABLE;
8+
9+
public error = "temporarily_unavailable";
10+
public error_description =
11+
"authentication temporarily unavailable due to sirene api failure";
12+
13+
public illustration = "default-error";
14+
public title =
15+
"Vérification de votre organisation temporairement indisponible";
16+
public description =
17+
"Nous n'avons pas pu vérifier le SIRET associé à votre organisation car le service utilisé pour cette vérification est actuellement indisponible. Cela ne signifie pas que votre accès est refusé : merci de réessayer dans quelques minutes.";
18+
19+
public displayContact = false;
20+
}

back/apps/core-fca-low/src/exceptions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./core-fca-account-blocked.exception";
22
export * from "./core-fca-agent-not-from-public-service.exception";
3+
export * from "./core-fca-api-sirene-down.exception";
34
export * from "./core-fca-base.exception";
45
export * from "./core-fca-invalid-email-domain.exception";
56
export * from "./core-fca-invalid-identity.exception";

back/apps/core-fca-low/src/services/identity.sanitizer.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import { CachedOrganizationService } from "@fc/cached-organization";
22
import { ConfigService } from "@fc/config";
33
import { IdentityProviderAdapterMongoService } from "@fc/identity-provider-adapter-mongo";
44
import { LoggerService } from "@fc/logger";
5+
import { ApiEntrepriseConnectionError } from "@proconnect-gouv/proconnect.api_entreprise/types";
56
import { IdentityFromIdpDto } from "../dto/identity-from-idp.dto";
6-
import { CoreFcaInvalidIdentityException } from "../exceptions";
7+
import {
8+
CoreFcaApiSireneDownException,
9+
CoreFcaInvalidIdentityException,
10+
} from "../exceptions";
711
import { IdentitySanitizer } from "./identity.sanitizer";
812

913
jest.mock("@fc/logger");
@@ -251,6 +255,47 @@ describe("IdentitySanitizer", () => {
251255
expect(result.roles).toEqual([]);
252256
});
253257

258+
it("should throw CoreFcaApiSireneDownException when getCachedOrganizationBySiret throws an ApiEntrepriseConnectionError", async () => {
259+
const idpId = "idp1";
260+
const sub = "sub123";
261+
const acr = "acr1";
262+
const identityFromIdp = {
263+
sub: "123",
264+
email: "test@test.com",
265+
given_name: "John",
266+
usual_name: "Doe",
267+
uid: "UID123",
268+
siret: "12345678900007",
269+
};
270+
271+
config.get = jest.fn().mockReturnValue({
272+
featureFetchOrganizationData: true,
273+
});
274+
275+
const connectionError = new ApiEntrepriseConnectionError(
276+
"connection failed",
277+
);
278+
(
279+
cachedOrganizationService.getCachedOrganizationBySiret as jest.Mock
280+
).mockRejectedValue(connectionError);
281+
282+
await expect(
283+
identitySanitizer.transformIdentity(
284+
identityFromIdp as IdentityFromIdpDto,
285+
idpId,
286+
sub,
287+
acr,
288+
),
289+
).rejects.toThrow(CoreFcaApiSireneDownException);
290+
291+
expect(logger.error).toHaveBeenCalledWith(
292+
expect.objectContaining({
293+
code: "identity-sanitizer-cached-organization-error",
294+
cachedOrganizationErrorType: "ApiEntrepriseConnectionError",
295+
}),
296+
);
297+
});
298+
254299
it("should not throw CoreFcaInvalidIdentityException when phone number is invalid", async () => {
255300
const idpId = "idp1";
256301
const sub = "sub123";

back/apps/core-fca-low/src/services/identity.sanitizer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import { LoggerService } from "@fc/logger";
1111
import { ApiEntrepriseConfig } from "@fc/api-entreprise";
1212
import { CachedOrganizationService } from "@fc/cached-organization";
1313
import { IdentityProviderMetadata } from "@fc/oidc";
14+
import { ApiEntrepriseConnectionError } from "@proconnect-gouv/proconnect.api_entreprise/types";
1415
import { AppConfig, IdentityForSpDto, IdentityFromIdpDto } from "../dto";
15-
import { CoreFcaInvalidIdentityException } from "../exceptions";
16+
import {
17+
CoreFcaApiSireneDownException,
18+
CoreFcaInvalidIdentityException,
19+
} from "../exceptions";
1620

1721
@Injectable()
1822
export class IdentitySanitizer {
@@ -91,6 +95,11 @@ export class IdentitySanitizer {
9195
cachedOrganizationErrorCause: error?.cause,
9296
cachedOrganizationErrorType: error?.constructor?.name,
9397
});
98+
99+
if (error instanceof ApiEntrepriseConnectionError) {
100+
throw new CoreFcaApiSireneDownException();
101+
}
102+
94103
identityForSp.roles = [];
95104
}
96105
} else {

0 commit comments

Comments
 (0)