mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Change accounts and oauth to use async for Weibo account
This commit is contained in:
@@ -473,3 +473,23 @@ OAuth.openSecrets = (serviceData, userId) => {
|
||||
);
|
||||
return result;
|
||||
};
|
||||
|
||||
OAuth._fetch = async (
|
||||
url,
|
||||
method = 'GET',
|
||||
{ headers = {}, queryParams = {}, body, ...options } = {}
|
||||
) => {
|
||||
const urlWithParams = new URL(url);
|
||||
|
||||
Object.entries(queryParams).forEach(([key, value]) => {
|
||||
urlWithParams.searchParams.set(key, `${value}`);
|
||||
});
|
||||
|
||||
const requestOptions = {
|
||||
method: method.toUpperCase(),
|
||||
headers,
|
||||
...(body ? { body } : {}),
|
||||
...options,
|
||||
};
|
||||
return fetch(urlWithParams.toString(), requestOptions);
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@ Package.onUse(api => {
|
||||
api.use(['reload', 'base64'], 'client');
|
||||
|
||||
api.use('oauth-encryption', 'server', {weak: true});
|
||||
api.use('fetch', 'server');
|
||||
|
||||
|
||||
api.export('OAuth');
|
||||
|
||||
@@ -7,7 +7,7 @@ Package.onUse(api => {
|
||||
api.use('oauth1', ['client', 'server']);
|
||||
api.use('oauth', ['client', 'server']);
|
||||
api.use('random', 'client');
|
||||
api.use('http@1.4.4 || 2.0.0', 'server');
|
||||
api.use('fetch', 'server');
|
||||
api.use(['service-configuration', 'ecmascript'], ['client', 'server']);
|
||||
|
||||
api.addFiles('weibo_client.js', 'client');
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
Weibo = {};
|
||||
|
||||
OAuth.registerService('weibo', 2, null, query => {
|
||||
OAuth.registerService('weibo', 2, null, async query => {
|
||||
|
||||
const response = getTokenResponse(query);
|
||||
const response = await getTokenResponse(query);
|
||||
const uid = parseInt(response.uid, 10);
|
||||
|
||||
// different parts of weibo's api seem to expect numbers, or strings
|
||||
@@ -11,7 +11,7 @@ OAuth.registerService('weibo', 2, null, query => {
|
||||
throw new Error(`Expected 'uid' to parse to an integer: ${JSON.stringify(response)}`);
|
||||
}
|
||||
|
||||
const identity = getIdentity(response.access_token, uid);
|
||||
const identity = await getIdentity(response.access_token, uid);
|
||||
|
||||
return {
|
||||
serviceData: {
|
||||
@@ -31,46 +31,48 @@ OAuth.registerService('weibo', 2, null, query => {
|
||||
// - uid
|
||||
// - access_token
|
||||
// - expires_in: lifetime of this token in seconds (5 years(!) right now)
|
||||
const getTokenResponse = query => {
|
||||
const config = ServiceConfiguration.configurations.findOne({service: 'weibo'});
|
||||
if (!config)
|
||||
throw new ServiceConfiguration.ConfigError();
|
||||
const getTokenResponse = async (query) => {
|
||||
const config = ServiceConfiguration.configurations.findOne({
|
||||
service: 'weibo',
|
||||
});
|
||||
if (!config) throw new ServiceConfiguration.ConfigError();
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = HTTP.post(
|
||||
"https://api.weibo.com/oauth2/access_token", {params: {
|
||||
code: query.code,
|
||||
client_id: config.clientId,
|
||||
client_secret: OAuth.openSecret(config.secret),
|
||||
redirect_uri: OAuth._redirectUri('weibo', config, null, {replaceLocalhost: true}),
|
||||
grant_type: 'authorization_code'
|
||||
}});
|
||||
} catch (err) {
|
||||
throw Object.assign(new Error(`Failed to complete OAuth handshake with Weibo. ${err.message}`),
|
||||
{response: err.response});
|
||||
}
|
||||
|
||||
// result.headers["content-type"] is 'text/plain;charset=UTF-8', so
|
||||
// the http package doesn't automatically populate result.data
|
||||
response.data = JSON.parse(response.content);
|
||||
|
||||
if (response.data.error) { // if the http response was a json object with an error attribute
|
||||
throw new Error(`Failed to complete OAuth handshake with Weibo. ${response.data.error}`);
|
||||
} else {
|
||||
return response.data;
|
||||
}
|
||||
return OAuth._fetch('https://api.weibo.com/oauth2/access_token', 'POST', {
|
||||
queryParams: {
|
||||
code: query.code,
|
||||
client_id: config.clientId,
|
||||
client_secret: OAuth.openSecret(config.secret),
|
||||
redirect_uri: OAuth._redirectUri('weibo', config, null, {
|
||||
replaceLocalhost: true,
|
||||
}),
|
||||
grant_type: 'authorization_code',
|
||||
},
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.catch((err) => {
|
||||
throw Object.assign(
|
||||
new Error(
|
||||
`Failed to complete OAuth handshake with Weibo. ${err.message}`
|
||||
),
|
||||
{ response: err.response }
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const getIdentity = (accessToken, userId) => {
|
||||
try {
|
||||
return HTTP.get(
|
||||
"https://api.weibo.com/2/users/show.json",
|
||||
{params: {access_token: accessToken, uid: userId}}).data;
|
||||
} catch (err) {
|
||||
throw Object.assign(new Error("Failed to fetch identity from Weibo. " + err.message),
|
||||
{response: err.response});
|
||||
}
|
||||
const getIdentity = async (accessToken, userId) => {
|
||||
return OAuth._fetch('https://api.weibo.com/2/users/show.json', 'GET', {
|
||||
queryParams: {
|
||||
access_token: accessToken,
|
||||
uid: userId,
|
||||
},
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.catch((err) => {
|
||||
throw Object.assign(
|
||||
new Error('Failed to fetch identity from Weibo. ' + err.message),
|
||||
{ response: err.response }
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
Weibo.retrieveCredential = (credentialToken, credentialSecret) =>
|
||||
|
||||
Reference in New Issue
Block a user