2022-05-23 03:29:54 +00:00
|
|
|
const qs = require('qs'); // eslint-disable-line import/no-extraneous-dependencies
|
|
|
|
const request = require('request');
|
|
|
|
const conf = require('./conf');
|
|
|
|
|
2022-05-25 05:43:45 +00:00
|
|
|
function giteeToken(clientId, code, oauth2RedirectUri) {
|
2022-07-14 12:48:42 +00:00
|
|
|
const clientIndex = conf.values.giteeClientId.split(',').indexOf(clientId);
|
|
|
|
const clientSecret = conf.values.giteeClientSecret.split(',')[clientIndex];
|
2022-05-23 03:29:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request({
|
|
|
|
method: 'POST',
|
|
|
|
url: 'https://gitee.com/oauth/token',
|
|
|
|
form: {
|
|
|
|
client_id: clientId,
|
2022-07-14 12:48:42 +00:00
|
|
|
client_secret: clientSecret,
|
2022-05-23 03:29:54 +00:00
|
|
|
code,
|
|
|
|
grant_type: 'authorization_code',
|
2022-05-25 05:43:45 +00:00
|
|
|
redirect_uri: oauth2RedirectUri,
|
2022-05-23 03:29:54 +00:00
|
|
|
},
|
|
|
|
json: true
|
|
|
|
}, (err, res, body) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
const token = body.access_token;
|
|
|
|
if (token) {
|
2022-05-26 05:13:18 +00:00
|
|
|
resolve(body);
|
2022-05-23 03:29:54 +00:00
|
|
|
} else {
|
|
|
|
reject(res.statusCode + ',body:' + JSON.stringify(body));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.giteeToken = (req, res) => {
|
2022-05-25 05:43:45 +00:00
|
|
|
giteeToken(req.query.clientId, req.query.code, req.query.oauth2RedirectUri)
|
2022-05-23 03:29:54 +00:00
|
|
|
.then(
|
2022-05-26 05:13:18 +00:00
|
|
|
tokenBody => res.send(tokenBody),
|
2022-05-23 03:29:54 +00:00
|
|
|
err => res
|
|
|
|
.status(400)
|
|
|
|
.send(err ? err.message || err.toString() : 'bad_code'),
|
|
|
|
);
|
|
|
|
};
|