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 Meteor developer account
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
OAuth.registerService("meteor-developer", 2, null, query => {
|
||||
const response = getTokens(query);
|
||||
OAuth.registerService("meteor-developer", 2, null, async query => {
|
||||
const response = await getTokens(query);
|
||||
const { accessToken } = response;
|
||||
const identity = getIdentity(accessToken);
|
||||
const identity = await getIdentity(accessToken);
|
||||
|
||||
const serviceData = {
|
||||
accessToken: OAuth.sealSecret(accessToken),
|
||||
@@ -23,74 +23,84 @@ OAuth.registerService("meteor-developer", 2, null, query => {
|
||||
};
|
||||
});
|
||||
|
||||
const toFormUrlencoded = data => {
|
||||
return Object.entries(data)
|
||||
.map(([key, value]) => `${key}=${value}`)
|
||||
.join('&');
|
||||
};
|
||||
|
||||
// returns an object containing:
|
||||
// - accessToken
|
||||
// - expiresIn: lifetime of token in seconds
|
||||
// - refreshToken, if this is the first authorization request and we got a
|
||||
// refresh token from the server
|
||||
const getTokens = query => {
|
||||
const getTokens = async query => {
|
||||
const config = ServiceConfiguration.configurations.findOne({
|
||||
service: 'meteor-developer'
|
||||
service: 'meteor-developer',
|
||||
});
|
||||
if (!config)
|
||||
if (!config) {
|
||||
throw new ServiceConfiguration.ConfigError();
|
||||
}
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = HTTP.post(
|
||||
MeteorDeveloperAccounts._server + "/oauth2/token", {
|
||||
params: {
|
||||
grant_type: "authorization_code",
|
||||
code: query.code,
|
||||
client_id: config.clientId,
|
||||
client_secret: OAuth.openSecret(config.secret),
|
||||
redirect_uri: OAuth._redirectUri('meteor-developer', config)
|
||||
}
|
||||
const bodyFormEncoded = toFormUrlencoded({
|
||||
grant_type: 'authorization_code',
|
||||
code: query.code,
|
||||
client_id: config.clientId,
|
||||
client_secret: OAuth.openSecret(config.secret),
|
||||
redirect_uri: OAuth._redirectUri('meteor-developer', config),
|
||||
});
|
||||
|
||||
return await fetch(MeteorDeveloperAccounts._server + '/oauth2/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: bodyFormEncoded,
|
||||
})
|
||||
.then(data => data.json())
|
||||
.then(data => {
|
||||
if (data.error) {
|
||||
throw new Error(
|
||||
'Failed to complete OAuth handshake with Meteor developer accounts. ' +
|
||||
(data ? data.error :
|
||||
'No response data'),
|
||||
);
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
throw Object.assign(
|
||||
new Error(
|
||||
"Failed to complete OAuth handshake with Meteor developer accounts. "
|
||||
+ err.message
|
||||
),
|
||||
{response: err.response}
|
||||
);
|
||||
}
|
||||
|
||||
if (! response.data || response.data.error) {
|
||||
// if the http response was a json object with an error attribute
|
||||
throw new Error(
|
||||
"Failed to complete OAuth handshake with Meteor developer accounts. " +
|
||||
(response.data ? response.data.error :
|
||||
"No response data")
|
||||
);
|
||||
} else {
|
||||
return {
|
||||
accessToken: response.data.access_token,
|
||||
refreshToken: response.data.refresh_token,
|
||||
expiresIn: response.data.expires_in
|
||||
};
|
||||
}
|
||||
return {
|
||||
accessToken: data.access_token,
|
||||
refreshToken: data.refresh_token,
|
||||
expiresIn: data.expires_in,
|
||||
};
|
||||
})
|
||||
.catch(err => {
|
||||
throw Object.assign(
|
||||
new Error(
|
||||
`Failed to complete OAuth handshake with Meteor developer accounts. ${err.message}`
|
||||
),
|
||||
{ response: err.response },
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const getIdentity = accessToken => {
|
||||
try {
|
||||
return HTTP.get(
|
||||
`${MeteorDeveloperAccounts._server}/api/v1/identity`,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${accessToken}`}
|
||||
}
|
||||
).data;
|
||||
} catch (err) {
|
||||
throw Object.assign(
|
||||
new Error("Failed to fetch identity from Meteor developer accounts. " +
|
||||
err.message),
|
||||
{response: err.response}
|
||||
);
|
||||
}
|
||||
const getIdentity = async accessToken => {
|
||||
return fetch(
|
||||
`${MeteorDeveloperAccounts._server}/api/v1/identity`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
},
|
||||
)
|
||||
.then(data => data.json())
|
||||
.catch(err => {
|
||||
throw Object.assign(
|
||||
new Error('Failed to fetch identity from Meteor developer accounts. ' +
|
||||
err.message),
|
||||
{ response: err.response },
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
MeteorDeveloperAccounts.retrieveCredential =
|
||||
(credentialToken, credentialSecret) =>
|
||||
MeteorDeveloperAccounts.retrieveCredential =
|
||||
(credentialToken, credentialSecret) =>
|
||||
OAuth.retrieveCredential(credentialToken, credentialSecret);
|
||||
|
||||
@@ -6,7 +6,7 @@ Package.describe({
|
||||
Package.onUse(api => {
|
||||
api.use('oauth2', ['client', 'server']);
|
||||
api.use('oauth', ['client', 'server']);
|
||||
api.use('http@1.4.4 || 2.0.0', ['server']);
|
||||
api.use('fetch', ['server']);
|
||||
api.use(['ecmascript', 'service-configuration'], ['client', 'server']);
|
||||
api.use('random', 'client');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user