docs: add refresh_token endpoint

This commit is contained in:
Jan De Dobbeleer 2022-01-06 21:19:21 +01:00
parent 5a69b3e771
commit a4727ddbbd
No known key found for this signature in database
GPG key ID: F6CC273CE5BA9AEE
5 changed files with 112 additions and 17 deletions

View file

@ -1,29 +1,31 @@
const axios = require('axios');
const strava = require('../shared/strava.js');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
// https://www.strava.com/oauth/authorize?client_id=76033&response_type=code&redirect_uri=https://ohmyposh.dev/api/auth&approval_prompt=force&scope=read,activity:read
// strava example:
// https://www.strava.com/oauth/authorize?client_id=76033&response_type=code&redirect_uri=https://ohmyposh.dev/api/auth&approval_prompt=force&scope=read,activity:read&state=strava
try {
const code = (req.query._code || (req.body && req.body.code));
if (!code) {
const segment = (req.query.state || (req.body && req.body.state));
if (!code || !segment) {
context.res = {
status: 400
};
return;
}
const params = {
client_id: process.env['STRAVA_CLIENT_ID'],
client_secret: process.env['STRAVA_CLIENT_SECRET'],
code: code,
grant_type: 'authorization_code',
};
const resp = await axios.post('https://www.strava.com/oauth/token', null, { params: params });
const body = {
access_token: resp.data.access_token,
refresh_token: resp.data.refresh_token,
let body = null;
switch (segment) {
case "strava":
body = await strava.getStravaToken(code);
break;
default:
context.res = {
body: "unknown segment",
status: 400
};
return;
}
context.res = {

View file

@ -1,3 +0,0 @@
{
"name": "Azure"
}

View file

@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}

39
docs/api/refresh/index.js Normal file
View file

@ -0,0 +1,39 @@
const strava = require('../shared/strava.js');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
// strava example:
// https://ohmyposh.dev/api/refresh?segment=strava&token=<refresh_token>
try {
const refresh_token = (req.query.token || (req.body && req.body.token));
const segment = (req.query.segment || (req.body && req.body.segment));
if (!refresh_token || !segment) {
context.res = {
status: 400
};
return;
}
let body = null;
switch (segment) {
case "strava":
body = await strava.refreshStravaToken(refresh_token);
break;
default:
context.res = {
body: "unknown segment",
status: 400
};
return;
}
context.res.json(body);
} catch (error) {
context.log(error);
context.res = {
body: error,
status: 500
};
}
}

38
docs/api/shared/strava.js Normal file
View file

@ -0,0 +1,38 @@
const axios = require('axios');
async function getStravaToken(code) {
const params = {
client_id: process.env['STRAVA_CLIENT_ID'],
client_secret: process.env['STRAVA_CLIENT_SECRET'],
code: code,
grant_type: 'authorization_code',
};
const resp = await axios.post('https://www.strava.com/api/v3/oauth/token', null, { params: params });
const body = {
access_token: resp.data.access_token,
refresh_token: resp.data.refresh_token,
}
return body;
}
async function refreshStravaToken(refresh_token) {
const params = {
client_id: process.env['STRAVA_CLIENT_ID'],
client_secret: process.env['STRAVA_CLIENT_SECRET'],
refresh_token: refresh_token,
grant_type: 'refresh_token',
};
const resp = await axios.post('https://www.strava.com/api/v3/oauth/token', null, { params: params });
const body = {
access_token: resp.data.access_token,
refresh_token: resp.data.refresh_token,
}
return body;
}
module.exports = {
getStravaToken: getStravaToken,
refreshStravaToken: refreshStravaToken,
}