|
|
@ -1,4 +1,4 @@
|
|
|
|
import { Router } from 'express';
|
|
|
|
import { Router } from 'express'
|
|
|
|
import { AuthorizationCode } from 'simple-oauth2'
|
|
|
|
import { AuthorizationCode } from 'simple-oauth2'
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
|
|
|
|
|
|
|
@ -11,53 +11,55 @@ export function setupOauth(r: Router) {
|
|
|
|
auth: {
|
|
|
|
auth: {
|
|
|
|
authorizePath: process.env.OAUTH_AUTH_URL ?? '',
|
|
|
|
authorizePath: process.env.OAUTH_AUTH_URL ?? '',
|
|
|
|
tokenHost: process.env.OAUTH_TOKEN_HOST ?? '',
|
|
|
|
tokenHost: process.env.OAUTH_TOKEN_HOST ?? '',
|
|
|
|
tokenPath: process.env.OAUTH_TOKEN_PATH ?? ''
|
|
|
|
tokenPath: process.env.OAUTH_TOKEN_PATH ?? '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const conf = {
|
|
|
|
const conf = {
|
|
|
|
redirect_uri: process.env.OAUTH_REDIRECT_URL ?? '',
|
|
|
|
redirect_uri: process.env.OAUTH_REDIRECT_URL ?? '',
|
|
|
|
scope: process.env.OAUTH_SCOPES ?? '',
|
|
|
|
scope: process.env.OAUTH_SCOPES ?? '',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const client = new AuthorizationCode(config);
|
|
|
|
const client = new AuthorizationCode(config)
|
|
|
|
|
|
|
|
|
|
|
|
const authorizationUri = client.authorizeURL({
|
|
|
|
const authorizationUri = client.authorizeURL({
|
|
|
|
redirect_uri: conf.redirect_uri,
|
|
|
|
redirect_uri: conf.redirect_uri,
|
|
|
|
scope: conf.scope,
|
|
|
|
scope: conf.scope,
|
|
|
|
state: ''
|
|
|
|
state: '',
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
r.get('/redirect', (req, res) => {
|
|
|
|
r.get('/redirect', (req, res) => {
|
|
|
|
res.redirect(authorizationUri);
|
|
|
|
res.redirect(authorizationUri)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Callback service parsing the authorization token and asking for the access token
|
|
|
|
// Callback service parsing the authorization token and asking for the access token
|
|
|
|
r.get('/callback', async (req, res) => {
|
|
|
|
r.get('/callback', async (req, res) => {
|
|
|
|
const code = req.query.code as string;
|
|
|
|
const code = req.query.code as string
|
|
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
const options = {
|
|
|
|
code,
|
|
|
|
code,
|
|
|
|
redirect_uri: conf.redirect_uri
|
|
|
|
redirect_uri: conf.redirect_uri,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const accessToken = await client.getToken(options);
|
|
|
|
const accessToken = await client.getToken(options)
|
|
|
|
|
|
|
|
|
|
|
|
console.log(accessToken.token.access_token)
|
|
|
|
console.log(accessToken.token.access_token)
|
|
|
|
|
|
|
|
|
|
|
|
const userInfo = await (await fetch(process.env.OAUTH_USER_INFO_URL ?? '', {
|
|
|
|
const userInfo = await (
|
|
|
|
method: 'GET',
|
|
|
|
await fetch(process.env.OAUTH_USER_INFO_URL ?? '', {
|
|
|
|
headers: {
|
|
|
|
method: 'GET',
|
|
|
|
'Authorization': "Bearer " + accessToken.token.access_token
|
|
|
|
headers: {
|
|
|
|
}
|
|
|
|
Authorization: 'Bearer ' + accessToken.token.access_token,
|
|
|
|
})).json()
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
).json()
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: call to db && login
|
|
|
|
// TODO: call to db && login
|
|
|
|
return res.status(200).json(userInfo);
|
|
|
|
return res.status(200).json(userInfo)
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Access Token Error', error.message);
|
|
|
|
console.error('Access Token Error', error.message)
|
|
|
|
return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`);
|
|
|
|
return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|