Commit 393ded65 authored by Adrien Dorsaz's avatar Adrien Dorsaz
Browse files

Initial commit

parents
Pipeline #319 failed with stages
in 0 seconds
This diff is collapsed.
{
"description": "a dumb example to get access token and refresh it from LinuxFr.org",
"private": "false",
"license": "MIT",
"type": "module",
"dependencies": {
"koa": "^2.13.4",
"koa2-router": "^2.0.1",
"node-fetch": "^3.0.0"
}
}
import Koa from 'koa';
import Router from 'koa2-router';
import fetch from 'node-fetch';
const app = new Koa();
process.env.OAUTH2_AUTHORIZATION_SERVER = 'https://linuxfr.org/api/oauth';
process.env.OAUTH2_CLIENT_ID = 'your-client-id';
process.env.OAUTH2_CLIENT_SECRET = 'your-secret-id';
process.env.OAUTH2_REDIRECT_URI = 'https://localhost:3000/';
// Basic logger
app.use(async (ctx, next) => {
await next();
const responseTime = ctx.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} ${ctx.request.body || '-'} - ${responseTime}`);
});
// Record Response Time
app.use(async (ctx, next) => {
const startTimestamp = Date.now();
await next();
const ms = Date.now() - startTimestamp;
ctx.set('X-Response-Time', `${ms}ms`);
});
// Index and redirect URI
const indexRouter = new Router();
app.use(indexRouter);
indexRouter.get('/', async (ctx) => {
if (ctx.request.query.code) {
console.log('code found: ', ctx.request.query.code);
const oauthAccessTokenParams = new URLSearchParams();
oauthAccessTokenParams.append('client_id', process.env.OAUTH2_CLIENT_ID);
oauthAccessTokenParams.append('client_secret', process.env.OAUTH2_CLIENT_SECRET);
oauthAccessTokenParams.append('redirect_uri', process.env.OAUTH2_REDIRECT_URI);
oauthAccessTokenParams.append('grant_type', 'authorization_code');
oauthAccessTokenParams.append('code', ctx.request.query.code);
const oauthAccessTokenResponse = await fetch(
`${process.env.OAUTH2_AUTHORIZATION_SERVER}/token`,
{
method: 'POST',
body: oauthAccessTokenParams,
});
const accessTokenResponseBody = await oauthAccessTokenResponse.json();
console.log(`Access token response (status: ${oauthAccessTokenResponse.status}): `, accessTokenResponseBody);
const oauthRefreshTokenParams = new URLSearchParams();
oauthRefreshTokenParams.append('client_id', process.env.OAUTH2_CLIENT_ID);
oauthRefreshTokenParams.append('client_secret', process.env.OAUTH2_CLIENT_SECRET);
oauthRefreshTokenParams.append('redirect_uri', process.env.OAUTH2_REDIRECT_URI);
oauthRefreshTokenParams.append('grant_type', 'refresh_token');
oauthRefreshTokenParams.append('refresh_token', accessTokenResponseBody.refresh_token);
const oauthRefreshTokenResponse = await fetch(
`${process.env.OAUTH2_AUTHORIZATION_SERVER}/token`,
{
method: 'POST',
body: oauthRefreshTokenParams,
});
const body = await oauthRefreshTokenResponse.json();
console.log(`Refresh token response (status: ${oauthRefreshTokenResponse.status}): `, body);
}
ctx.body=`
<html>
<body>
<a href='${process.env.OAUTH2_AUTHORIZATION_SERVER}/authorize?client_id=${process.env.OAUTH2_CLIENT_ID}&response_type=code&redirect_uri=${process.env.OAUTH2_REDIRECT_URI}'>Login on Linuxfr.org</a>
</body>
</html>`;
});
app.listen(3001);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment