Skip to content

Commit 5fa4cab

Browse files
refactored proptypes
1 parent 2e29d3b commit 5fa4cab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+11027
-11025
lines changed

server/index.js

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ const certificate = fs.readFileSync('./server.crt', 'utf8');
2626
const credentials = { key: privateKey, cert: certificate };
2727

2828
const {
29-
AUTH_PATH,
30-
CLIENT_ID,
31-
CLIENT_SECRET,
32-
HOST,
33-
ISSUER_URL,
34-
JWKS_PATH,
35-
NODE_ENV,
36-
REACT_APP_SERVER_PORT,
37-
REDIRECT_URL,
38-
SESSION_SECRET,
39-
TOKEN_PATH,
29+
AUTH_PATH,
30+
CLIENT_ID,
31+
CLIENT_SECRET,
32+
HOST,
33+
ISSUER_URL,
34+
JWKS_PATH,
35+
NODE_ENV,
36+
REACT_APP_SERVER_PORT,
37+
REDIRECT_URL,
38+
SESSION_SECRET,
39+
TOKEN_PATH,
4040
} = process.env;
4141

4242
let publicKey = '';
@@ -60,66 +60,66 @@ app.use(compression()); // GZip compress responses
6060

6161
// static files
6262
if (NODE_ENV !== 'development') {
63-
app.use(express.static(path.join(__dirname, 'build')));
63+
app.use(express.static(path.join(__dirname, 'build')));
6464
}
6565
app.use(favicon(path.join(__dirname, '../public/favicon.ico')));
6666

6767
app.use(
68-
session({
69-
httpOnly: true,
70-
maxAge: 7200000,
71-
secret: SESSION_SECRET,
72-
secure: true,
73-
})
68+
session({
69+
httpOnly: true,
70+
maxAge: 7200000,
71+
secret: SESSION_SECRET,
72+
secure: true,
73+
})
7474
);
7575

7676
(async () => {
77-
try {
78-
const { data } = await axios.get(ISSUER_URL + JWKS_PATH);
79-
console.log('Received public key from TARA'); // eslint-disable-line no-console
80-
publicKey = data.keys[0]; // eslint-disable-line prefer-destructuring
81-
} catch (e) {
82-
console.log(`Public key request error: ${e}`); // eslint-disable-line no-console
83-
}
77+
try {
78+
const { data } = await axios.get(ISSUER_URL + JWKS_PATH);
79+
console.log('Received public key from TARA'); // eslint-disable-line no-console
80+
publicKey = data.keys[0]; // eslint-disable-line prefer-destructuring
81+
} catch (e) {
82+
console.log(`Public key request error: ${e}`); // eslint-disable-line no-console
83+
}
8484
})();
8585

8686
// middlewares
8787
let LOCALE = 'et';
8888
app.use((req, res, next) => {
89-
LOCALE = req.cookies.locale || 'et';
90-
next();
89+
LOCALE = req.cookies.locale || 'et';
90+
next();
9191
});
9292

9393
const redirect_uri =
94-
NODE_ENV === 'development'
95-
? `https://${HOST}:${REACT_APP_SERVER_PORT}${REDIRECT_URL}`
96-
: `https://${HOST}${REDIRECT_URL}`;
94+
NODE_ENV === 'development'
95+
? `https://${HOST}:${REACT_APP_SERVER_PORT}${REDIRECT_URL}`
96+
: `https://${HOST}${REDIRECT_URL}`;
9797

9898
// grant auth
9999
app.use(
100-
grant({
101-
defaults: {
102-
protocol: 'https',
103-
host: HOST,
104-
state: true,
105-
callback: '/auth/callback',
106-
transport: 'querystring',
107-
},
108-
openid: {
109-
authorize_url: ISSUER_URL + AUTH_PATH,
110-
access_url: ISSUER_URL + TOKEN_PATH,
111-
oauth: 2,
112-
key: CLIENT_ID,
113-
secret: CLIENT_SECRET,
114-
scope: 'openid',
115-
redirect_uri,
116-
response_type: 'code',
117-
callback: REDIRECT_URL,
118-
custom_params: {
119-
ui_locales: LOCALE,
120-
},
121-
},
122-
})
100+
grant({
101+
defaults: {
102+
protocol: 'https',
103+
host: HOST,
104+
state: true,
105+
callback: '/auth/callback',
106+
transport: 'querystring',
107+
},
108+
openid: {
109+
authorize_url: ISSUER_URL + AUTH_PATH,
110+
access_url: ISSUER_URL + TOKEN_PATH,
111+
oauth: 2,
112+
key: CLIENT_ID,
113+
secret: CLIENT_SECRET,
114+
scope: 'openid',
115+
redirect_uri,
116+
response_type: 'code',
117+
callback: REDIRECT_URL,
118+
custom_params: {
119+
ui_locales: LOCALE,
120+
},
121+
},
122+
})
123123
);
124124

125125
app.use(helmet());
@@ -145,14 +145,14 @@ app.get(REDIRECT_URL, (req, res) => callbackPage(req, res, jwkToPem(publicKey).t
145145
app.get('/*', (req, res) => res.sendFile(path.join(__dirname, 'build', 'index.html')));
146146

147147
const server = https
148-
.createServer(credentials, app)
149-
.listen(NODE_ENV === 'test' ? 4000 : REACT_APP_SERVER_PORT, () => {
150-
banner();
151-
// eslint-disable-next-line no-console
152-
console.log(`Environment: ${NODE_ENV}`);
153-
// 'ready' is a hook used by the e2e (integration) tests (see node-while)
154-
server.emit('ready');
155-
});
148+
.createServer(credentials, app)
149+
.listen(NODE_ENV === 'test' ? 4000 : REACT_APP_SERVER_PORT, () => {
150+
banner();
151+
// eslint-disable-next-line no-console
152+
console.log(`Environment: ${NODE_ENV}`);
153+
// 'ready' is a hook used by the e2e (integration) tests (see node-while)
154+
server.emit('ready');
155+
});
156156

157157
// export server instance so we can hook into it in e2e tests etc
158158
export default server;

server/routes/callbackPageRoute.js

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,113 +8,113 @@ import capitalize from 'capitalize';
88
dotenv.config();
99

1010
const {
11-
CLIENT_ID,
12-
CLIENT_SECRET,
13-
HOST,
14-
ISSUER_URL,
15-
NODE_ENV,
16-
REACT_APP_SERVER_PORT,
17-
REDIRECT_URL,
18-
TOKEN_PATH,
11+
CLIENT_ID,
12+
CLIENT_SECRET,
13+
HOST,
14+
ISSUER_URL,
15+
NODE_ENV,
16+
REACT_APP_SERVER_PORT,
17+
REDIRECT_URL,
18+
TOKEN_PATH,
1919
} = process.env;
2020

2121
const B64_VALUE = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64');
2222

2323
const redirect_uri =
24-
NODE_ENV === 'development'
25-
? `https://${HOST}:${REACT_APP_SERVER_PORT}${REDIRECT_URL}`
26-
: `https://${HOST}${REDIRECT_URL}`;
24+
NODE_ENV === 'development'
25+
? `https://${HOST}:${REACT_APP_SERVER_PORT}${REDIRECT_URL}`
26+
: `https://${HOST}${REDIRECT_URL}`;
2727

2828
export default async function callbackPageRoute(req, res, publicKey) {
29-
try {
30-
if (req.query.error) {
31-
throw new Error(req.query.error);
32-
}
29+
try {
30+
if (req.query.error) {
31+
throw new Error(req.query.error);
32+
}
3333

34-
/* Võta päringu query-osast TARA poolt saadetud volituskood (authorization code) */
35-
const { code } = req.query;
34+
/* Võta päringu query-osast TARA poolt saadetud volituskood (authorization code) */
35+
const { code } = req.query;
3636

37-
/*
37+
/*
3838
Turvaelemendi state kontroll
3939
*/
40-
const returnedState = req.query.state;
41-
const sessionState = req.session.grant.state;
42-
if (returnedState !== sessionState) {
43-
throw new Error('Request query state and session state do not match.');
44-
}
45-
const options = {
46-
data: qs.stringify({
47-
code,
48-
grant_type: 'authorization_code',
49-
redirect_uri,
50-
}),
51-
headers: {
52-
Accept: 'application/json, application/xml, text/plain, text/html, *.*',
53-
Authorization: `Basic ${B64_VALUE}`,
54-
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
55-
},
56-
method: 'POST',
57-
url: ISSUER_URL + TOKEN_PATH,
58-
};
40+
const returnedState = req.query.state;
41+
const sessionState = req.session.grant.state;
42+
if (returnedState !== sessionState) {
43+
throw new Error('Request query state and session state do not match.');
44+
}
45+
const options = {
46+
data: qs.stringify({
47+
code,
48+
grant_type: 'authorization_code',
49+
redirect_uri,
50+
}),
51+
headers: {
52+
Accept: 'application/json, application/xml, text/plain, text/html, *.*',
53+
Authorization: `Basic ${B64_VALUE}`,
54+
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
55+
},
56+
method: 'POST',
57+
url: ISSUER_URL + TOKEN_PATH,
58+
};
5959

60-
const {
61-
data: { id_token },
62-
} = await axios(options); // eslint-disable-line camelcase
60+
const {
61+
data: { id_token },
62+
} = await axios(options); // eslint-disable-line camelcase
6363

64-
/*
64+
/*
6565
Identsustõendi kontrollimine. Teegi jsonwebtoken
6666
abil kontrollitakse allkirja, tõendi saajat (aud), tõendi
6767
väljaandjat (iss) ja tõendi kehtivust (nbf ja exp).
6868
Vt https://www.npmjs.com/package/jsonwebtoken
6969
*/
70-
// remove the cookie
71-
res.clearCookie('connect.sid', {
72-
domain: HOST,
73-
httpOnly: true,
74-
path: '/',
75-
sameSite: 'lax',
76-
secure: true,
77-
});
70+
// remove the cookie
71+
res.clearCookie('connect.sid', {
72+
domain: HOST,
73+
httpOnly: true,
74+
path: '/',
75+
sameSite: 'lax',
76+
secure: true,
77+
});
7878

79-
jwt.verify(
80-
id_token,
81-
publicKey,
82-
{
83-
audience: CLIENT_ID,
84-
clockTolerance: 10,
85-
issuer: ISSUER_URL,
86-
},
87-
(err, verifiedJwt) => {
88-
if (err) {
89-
throw new Error(err);
90-
}
91-
const userData = {
92-
country_code: get_user_country_code(verifiedJwt.sub),
93-
first_name: capitalize.words(verifiedJwt.profile_attributes.given_name),
94-
ident: get_user_ident(verifiedJwt.sub),
95-
last_name: capitalize.words(verifiedJwt.profile_attributes.family_name),
96-
};
79+
jwt.verify(
80+
id_token,
81+
publicKey,
82+
{
83+
audience: CLIENT_ID,
84+
clockTolerance: 10,
85+
issuer: ISSUER_URL,
86+
},
87+
(err, verifiedJwt) => {
88+
if (err) {
89+
throw new Error(err);
90+
}
91+
const userData = {
92+
country_code: get_user_country_code(verifiedJwt.sub),
93+
first_name: capitalize.words(verifiedJwt.profile_attributes.given_name),
94+
ident: get_user_ident(verifiedJwt.sub),
95+
last_name: capitalize.words(verifiedJwt.profile_attributes.family_name),
96+
};
9797

98-
console.log('Decrypted JWT from TARA:');
99-
console.log(verifiedJwt);
100-
console.log('userData:');
101-
console.log(userData);
102-
req.session.user = userData;
103-
if (NODE_ENV === 'development') {
104-
res.redirect(`https://${HOST}:3000`);
105-
} else {
106-
res.redirect('/');
107-
}
108-
}
109-
);
110-
} catch (e) {
111-
console.log(e); // eslint-disable-line no-console
112-
if (NODE_ENV === 'development') {
113-
res.redirect(`https://${HOST}:3000/login`);
114-
} else {
115-
res.redirect('/login');
116-
}
117-
}
98+
console.log('Decrypted JWT from TARA:');
99+
console.log(verifiedJwt);
100+
console.log('userData:');
101+
console.log(userData);
102+
req.session.user = userData;
103+
if (NODE_ENV === 'development') {
104+
res.redirect(`https://${HOST}:3000`);
105+
} else {
106+
res.redirect('/');
107+
}
108+
}
109+
);
110+
} catch (e) {
111+
console.log(e); // eslint-disable-line no-console
112+
if (NODE_ENV === 'development') {
113+
res.redirect(`https://${HOST}:3000/login`);
114+
} else {
115+
res.redirect('/login');
116+
}
117+
}
118118
}
119119

120120
export const get_user_ident = (ident) => ident.substr(2);

0 commit comments

Comments
 (0)