Skip to content

Commit 10d8465

Browse files
committed
use external id
1 parent af7b9fc commit 10d8465

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

src/accounts-client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const accountsClient = Axios.create({
1010
* @param ctx {import('koa').Context}
1111
* @returns {Promise<string>}
1212
*/
13-
module.exports.getUsername = async (ctx) => {
13+
module.exports.getExternalId = async (ctx) => {
1414
try {
1515
const { data } = await accountsClient.get('/api/me', {
1616
headers: {
1717
cookie: ctx.get('cookie')
1818
}
1919
})
20-
return data.data.username
20+
return data.data.openid
2121
} catch (e) {
2222
return undefined
2323
}

src/asktug-proxy.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,27 @@ const request = (url, options, cb) => {
2929
/**
3030
*
3131
* @param ctx {import('koa').Context}
32-
* @param username {string}
32+
* @param externalId {string}
3333
* @return Promise<void>
3434
*/
35-
function proxyAsktug (ctx, username) {
35+
function proxyAsktug (ctx, externalId) {
3636
const { req, res } = ctx
3737

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

5849
const clientRequest = request(url, {
5950
method: req.method,
6051
timeout: 1500,
61-
headers: {
62-
'Accept': 'application/json',
63-
'x-forwarded-for': ctx.request.get('x-forwarded-for'),
64-
}
52+
headers,
6553
}, asktugRes => {
6654
res.statusCode = asktugRes.statusCode
6755
res.statusMessage = asktugRes.statusMessage

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Koa = require('koa')
22
const logger = require('koa-logger')
3-
const { getUsername } = require('./accounts-client')
3+
const { getExternalId } = require('./accounts-client')
44
const proxyAsktug = require('./asktug-proxy')
55
const { asktug } = require('./config')
66
const Axios = require("axios");
@@ -61,8 +61,8 @@ app.use(async (ctx, next) => {
6161
// ACCOUNTS_COOKIE_NAME
6262
app.use(async ctx => {
6363
try {
64-
const username = await getUsername(ctx)
65-
await proxyAsktug(ctx, username)
64+
const externalId = await getExternalId(ctx)
65+
await proxyAsktug(ctx, externalId)
6666
} catch (e) {
6767
console.error('proxy failed', e)
6868
ctx.status = e?.response?.status ?? e?.status ?? 500

0 commit comments

Comments
 (0)