mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
UIWebView which aren't able to use the preferred "popup" login flow. See the specs for details: https://meteor.hackpad.com/OAuth-redirect-flow-spec-PeziTcaNPDP https://meteor.hackpad.com/OAuth-redirect-flow-part-II-vswwUKP4vXe I extracted code to construct a URL from the `http` package into a new `url` utility package. The new package has no public API, it simply has the original URL construction functions that were in `http` and makes them available to oauth. Fixes the Meetup account login, as Meetup now requires using "https://api.meetup.com/2/members" instead of "https://secure.meetup.com/2/members". The `?close` parameter for the redirect URI is now not needed or used. For backwards compatibility the `?close` parameter is included if the login service configuration doesn't include the `loginStyle` field (indicating it was created using old code).
73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
Github = {};
|
|
|
|
OAuth.registerService('github', 2, null, function(query) {
|
|
|
|
var accessToken = getAccessToken(query);
|
|
var identity = getIdentity(accessToken);
|
|
|
|
return {
|
|
serviceData: {
|
|
id: identity.id,
|
|
accessToken: OAuth.sealSecret(accessToken),
|
|
email: identity.email,
|
|
username: identity.login
|
|
},
|
|
options: {profile: {name: identity.name}}
|
|
};
|
|
});
|
|
|
|
// http://developer.github.com/v3/#user-agent-required
|
|
var userAgent = "Meteor";
|
|
if (Meteor.release)
|
|
userAgent += "/" + Meteor.release;
|
|
|
|
var getAccessToken = function (query) {
|
|
var config = ServiceConfiguration.configurations.findOne({service: 'github'});
|
|
if (!config)
|
|
throw new ServiceConfiguration.ConfigError();
|
|
|
|
var response;
|
|
try {
|
|
response = HTTP.post(
|
|
"https://github.com/login/oauth/access_token", {
|
|
headers: {
|
|
Accept: 'application/json',
|
|
"User-Agent": userAgent
|
|
},
|
|
params: {
|
|
code: query.code,
|
|
client_id: config.clientId,
|
|
client_secret: OAuth.openSecret(config.secret),
|
|
redirect_uri: OAuth._redirectUri('github', config),
|
|
state: query.state
|
|
}
|
|
});
|
|
} catch (err) {
|
|
throw _.extend(new Error("Failed to complete OAuth handshake with Github. " + err.message),
|
|
{response: err.response});
|
|
}
|
|
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 GitHub. " + response.data.error);
|
|
} else {
|
|
return response.data.access_token;
|
|
}
|
|
};
|
|
|
|
var getIdentity = function (accessToken) {
|
|
try {
|
|
return HTTP.get(
|
|
"https://api.github.com/user", {
|
|
headers: {"User-Agent": userAgent}, // http://developer.github.com/v3/#user-agent-required
|
|
params: {access_token: accessToken}
|
|
}).data;
|
|
} catch (err) {
|
|
throw _.extend(new Error("Failed to fetch identity from Github. " + err.message),
|
|
{response: err.response});
|
|
}
|
|
};
|
|
|
|
|
|
Github.retrieveCredential = function(credentialToken, credentialSecret) {
|
|
return OAuth.retrieveCredential(credentialToken, credentialSecret);
|
|
};
|