Skip to content
Snippets Groups Projects
Commit e597d01f authored by David Mynors's avatar David Mynors
Browse files

Merge branch 'oauth-testing' into 'master'

Add mastodon oauth for meet.coop-registration-form

See merge request social.coop/tech/wiki.social.coop!2
parents 946db6fa aa4c906f
Branches
No related tags found
1 merge request!2Add mastodon oauth for meet.coop-registration-form
......@@ -3,3 +3,5 @@
/node_modules/
/build/
/config.js
*.log
src
module.exports = {
schema: 2,
theme: {
timezone: 'Europe/London',
title: 'Example Title',
......
const axios = require('axios');
const url = require('url');
// TODO: logging
let config = require('./config.js');
const baseUrl = config.theme.urls.base;
config = config.oauth;
const ensureAuthenticated = async (req, res, next) => {
const code = req.url.split('?code=')[1];
const redirectUri = baseUrl + req.url;
if (!code) {
try {
return res.redirect(
`${config.instance}/oauth/authorize?client_id=${config.clientId}&scope=read:accounts&redirect_uri=${redirectUri}&response_type=code`
);
} catch (err) {
console.log('error redirecting to login:', err);
return res.json("an error ocurred")
}
}
const params = new url.URLSearchParams({
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: redirectUri,
grant_type: 'authorization_code',
code: code,
scope: 'read:accounts',
});
let token;
try {
const tokenResponse = await axios.post(
`${config.instance}/oauth/token`,
params.toString()
);
token = tokenResponse.data.access_token;
} catch (err) {
console.log('error requesting access token:', err.response.data || err);
return res.json("an error ocurred")
}
try {
const profileResponse = await axios.get(
`${config.instance}/api/v1/accounts/verify_credentials`,
{
headers: { Authorization: 'Bearer ' + token },
}
);
if (profileResponse.status === 200) {
console.log(
'successfully logged in',
profileResponse.data.username
);
next();
} else {
throw new Error(
`verify_credentials response was ${profileResponse.status}, body: ${profileResponse.data}`
);
}
} catch (err) {
console.log('error verifying credentials:', err.response.data || err);
return res.json("an error ocurred")
}
};
module.exports = ensureAuthenticated;
......@@ -4,6 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"prebuild": "test -d src || git clone git@git.coop:social.coop/general.wiki.git src",
"build": "node build.js",
"serve": "node serve.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "wu-lee <wu_lee@social.coop>",
......
'use strict';
const express = require('express');
const fs = require('fs');
const ensureAuthenticated = require('./lib/auth.js')
const templates = require('./lib/templates.js');
const metalsmith = require('./metalsmith.js');
const email = require('./lib/email.js');
......@@ -72,6 +73,9 @@ app.get('/', (req, res) => {
app.get('/style.css', (req, res) => {
res.sendFile(req.path, fileOptions);
});
app.get(/^\/meet.coop-registration-form(.html)?/, ensureAuthenticated, (req, res, next) => {
next();
});
app.get('*', (req, res, next) => {
var path = req.path+'/index.html';
if (fs.existsSync(fileOptions.root+path)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment