Skip to content

Commit

Permalink
use external id
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 committed Sep 19, 2022
1 parent af7b9fc commit 10d8465
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/accounts-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const accountsClient = Axios.create({
* @param ctx {import('koa').Context}
* @returns {Promise<string>}
*/
module.exports.getUsername = async (ctx) => {
module.exports.getExternalId = async (ctx) => {
try {
const { data } = await accountsClient.get('/api/me', {
headers: {
cookie: ctx.get('cookie')
}
})
return data.data.username
return data.data.openid
} catch (e) {
return undefined
}
Expand Down
32 changes: 10 additions & 22 deletions src/asktug-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,27 @@ const request = (url, options, cb) => {
/**
*
* @param ctx {import('koa').Context}
* @param username {string}
* @param externalId {string}
* @return Promise<void>
*/
function proxyAsktug (ctx, username) {
function proxyAsktug (ctx, externalId) {
const { req, res } = ctx

return new Promise((resolve, reject) => {
//// https://github.com/discourse/discourse/pull/7129
//// utf8 username is invalid
//
const headers = {
'Accept': 'application/json',
'x-forwarded-for': ctx.request.get('x-forwarded-for'),
}
let url = asktug.url + req.url
if (username) {
// https://github.com/pingcap/discourse/blob/69a66722364fb23e82e8bb81cf7645c1b7e585db/lib/auth/default_current_user_provider.rb#L300
// We could only use one of header or query to pass auth info
const sig = `api_key=${encodeURIComponent(asktug.token)}&api_username=${encodeURIComponent(username)}`
if (url.indexOf('?') > 0) {
if (url.endsWith('&') || url.endsWith('?')) {
url += sig
} else {
url += '&' + sig
}
} else {
url += '?' + sig
}
if (externalId) {
headers['Api-Key'] = asktug.token
headers['Api-User-External-Id'] = externalId
}

const clientRequest = request(url, {
method: req.method,
timeout: 1500,
headers: {
'Accept': 'application/json',
'x-forwarded-for': ctx.request.get('x-forwarded-for'),
}
headers,
}, asktugRes => {
res.statusCode = asktugRes.statusCode
res.statusMessage = asktugRes.statusMessage
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Koa = require('koa')
const logger = require('koa-logger')
const { getUsername } = require('./accounts-client')
const { getExternalId } = require('./accounts-client')
const proxyAsktug = require('./asktug-proxy')
const { asktug } = require('./config')
const Axios = require("axios");
Expand Down Expand Up @@ -61,8 +61,8 @@ app.use(async (ctx, next) => {
// ACCOUNTS_COOKIE_NAME
app.use(async ctx => {
try {
const username = await getUsername(ctx)
await proxyAsktug(ctx, username)
const externalId = await getExternalId(ctx)
await proxyAsktug(ctx, externalId)
} catch (e) {
console.error('proxy failed', e)
ctx.status = e?.response?.status ?? e?.status ?? 500
Expand Down

0 comments on commit 10d8465

Please sign in to comment.