-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockFCPlus.js
133 lines (110 loc) · 6.42 KB
/
mockFCPlus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express = require('express');
const jose = require('jose');
const adaptateurChiffrement = require('./src/adaptateurs/adaptateurChiffrement');
const JETON_CAS_SIGNATURE_INVALIDE = 'jetonCasSignatureInvalide';
const jwkValide = {
kty: 'RSA',
n: 'whYOFK2Ocbbpb_zVypi9SeKiNUqKQH0zTKN1-6fpCTu6ZalGI82s7XK3tan4dJt90ptUPKD2zvxqTzFNfx4HHHsrYCf2-FMLn1VTJfQazA2BvJqAwcpW1bqRUEty8tS_Yv4hRvWfQPcc2Gc3-_fQOOW57zVy-rNoJc744kb30NjQxdGp03J2S3GLQu7oKtSDDPooQHD38PEMNnITf0pj-KgDPjymkMGoJlO3aKppsjfbt_AH6GGdRghYRLOUwQU-h-ofWHR3lbYiKtXPn5dN24kiHy61e3VAQ9_YAZlwXC_99GGtw_NpghFAuM4P1JDn0DppJldy3PGFC0GfBCZASw',
e: 'AQAB',
d: 'VuVE_KEP6323WjpbBdAIv7HGahGrgGANvbxZsIhm34lsVOPK0XDegZkhAybMZHjRhp-gwVxX5ChC-J3cUpOBH5FNxElgW6HizD2Jcq6t6LoLYgPSrfEHm71iHg8JsgrqfUnGYFzMJmv88C6WdCtpgG_qJV1K00_Ly1G1QKoBffEs-v4fAMJrCbUdCz1qWto-PU-HLMEo-krfEpGgcmtZeRlDADh8cETMQlgQfQX2VWq_aAP4a1SXmo-j0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xVOxB09V4_GAD8oYgojjmhwCb0AVE0U-I3t4jqKhSNFMDVOBR2Vf-WZLrzDG4puKMGNcnPSopn_S8LTOTZf3E',
dq: 'vuFNkQJUcBJT5IObQc2MIbi6JaGxXCmPfBIkspqyGKUHiff63ZWYRx-J2_wz0_ID2nWVhBIFg_Evo1AsCS2HsixZopr1-jumLec9r9GA9z2LDsMKndmNW9NFQVjONv1nBw-054vljHUFY9Yz05eXjG8yw7AVLpWwO44dwSsCdbE',
qi: 'tA3h9_ecg1pu9zenGhdkW9lY7HqU3a41ROPQxYVX8rVybgwiO7MlqBQmy2zgezB5NQfzIgeWMtgThemN5RiQ3bunGRBOHL9PFjMo_aGO3hZkj93EdoxZBiIlLwJX11yxmMBC05jojVR-2XjX9Eijs5xmFdrjkngmwlAhbYyz6M0',
};
const ootsJWK = JSON.parse(atob(process.env.CLE_PRIVEE_JWK_EN_BASE64));
const enJWE = (cleSignature, infos) => {
const headerJWT = {
alg: 'RS256',
};
const headerJWE = {
alg: 'RSA-OAEP',
enc: 'A256GCM',
cty: 'JWT',
kid: adaptateurChiffrement.cleHachage(ootsJWK.n),
iss: 'http://oots',
aud: process.env.IDENTIFIANT_CLIENT_FCPLUS,
};
return jose.importJWK(cleSignature)
.then((clePrivee) => new jose.SignJWT(infos)
.setProtectedHeader(headerJWT)
.setIssuedAt()
.setIssuer('http://mock_fcplus')
.setAudience(process.env.IDENTIFIANT_CLIENT_FCPLUS)
.setExpirationTime('1h')
.sign(clePrivee))
.then((jwt) => jose.importJWK(ootsJWK)
.then((clePrivee) => new jose.CompactEncrypt(new TextEncoder().encode(jwt))
.setProtectedHeader(headerJWE)
.encrypt(clePrivee)));
};
const port = 4000;
const app = express();
let nonce;
app.use(express.urlencoded({ extended: true }));
app.get('/', (_requete, reponse) => {
reponse.json({
authorization_endpoint: `${process.env.URL_BASE_MOCK_FCPLUS}/debut_session`,
end_session_endpoint: `${process.env.URL_BASE_MOCK_FCPLUS}/fin_session`,
jwks_uri: `${process.env.URL_BASE_MOCK_FCPLUS}/jwks`,
token_endpoint: `${process.env.URL_BASE_MOCK_FCPLUS}/jeton`,
userinfo_endpoint: `${process.env.URL_BASE_MOCK_FCPLUS}/userinfo`,
});
});
app.get('/debut_session', (requete, reponse) => {
const contexteMock = requete.query.contexte_mock;
const etat = (contexteMock === 'etatRenvoyeInvalide') ? 'oups' : requete.query.state;
const code = (contexteMock === 'signatureJetonInvalide') ? 'XXX' : 'abcdef';
nonce = (contexteMock === 'nonceInvalide') ? 'oups' : requete.query.nonce;
reponse.redirect(`${process.env.URL_REDIRECTION_CONNEXION}?state=${etat}&code=${code}`);
});
app.get('/fin_session', (_requete, reponse) => {
reponse.redirect(process.env.URL_REDIRECTION_DECONNEXION);
});
app.post('/jeton', (requete, reponse) => {
const { code } = requete.body;
const jeton = (code === 'XXX') ? JETON_CAS_SIGNATURE_INVALIDE : 'unJeton';
enJWE(jwkValide, { nonce })
.then((jwe) => reponse.json({ access_token: jeton, id_token: jwe }));
});
app.get('/jwks', (_requete, reponse) => {
const { kty, n, e } = jwkValide;
reponse.json({
keys: [{
use: 'sig',
kty,
n,
e,
}],
});
});
app.get('/userinfo', (requete, reponse) => {
const jeton = requete.headers.authorization.match(/Bearer (.*)/)[1];
const cleSignature = (jeton === JETON_CAS_SIGNATURE_INVALIDE) ? jwkInvalide : jwkValide;
const envoieInfos = (infos) => enJWE(cleSignature, infos)
.then((jwe) => reponse.send(jwe));
envoieInfos({
given_name: 'Anne-Juliette',
family_name: 'HAUDEBERT',
birthdate: '1962-08-24',
gender: 'female',
sub: '1234567890abcdef',
});
});
app.listen(port, () => {
/* eslint-disable no-console */
console.log(`Mock FCPlus est démarré et écoute sur le port ${port} !…`);
/* eslint-enable no-console */
});