Repository navigation
Expand file tree
/
Copy pathauthService.js
More file actions
42 lines (37 loc) · 1.48 KB
/
Copy pathauthService.js
File metadata and controls
42 lines (37 loc) · 1.48 KB
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
const { v4: uuidv4 } = require('uuid');
const qs = require('qs');
const { exchangeCodeAndGetUserInfo } = require('../utils/httpClient');
function buildAuthorizationUrl({ client, baseUrl, userFlow, server, realm, state, phone }) {
const { client_id: clientId, scope, channel } = client;
const params = {
response_type: 'code',
client_id: clientId,
redirect_uri: `${baseUrl}/auth/callback/${userFlow}/${server.id}`,
scope,
state,
nonce: uuidv4(),
ui_locales: 'en',
consent_id: process.env.CONSENT_ID,
consent_timestamp: Math.floor(Date.now() / 1000),
env: process.env.NODE_ENV || 'development',
};
if (channel) params.channel = channel;
if (phone) params.login_hint = phone;
const endpoint = userFlow === 'pvn_ipificator'
? 'https://ipificator.ipification.com/api'
: `${server.url}/realms/${realm}/protocol/openid-connect/auth`;
return `${endpoint}?${qs.stringify(params)}`;
}
async function exchangeAuthorizationCode({ server, realm, clientId, clientSecret, redirectUri, code }) {
const tokenUrl = `${server.url}/realms/${realm}/protocol/openid-connect/token`;
const userUrl = `${server.url}/realms/${realm}/protocol/openid-connect/userinfo`;
const params = {
code,
redirect_uri: redirectUri,
grant_type: 'authorization_code',
client_id: clientId,
client_secret: clientSecret,
};
return exchangeCodeAndGetUserInfo(tokenUrl, userUrl, params);
}
module.exports = { buildAuthorizationUrl, exchangeAuthorizationCode };