Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lots of fixes #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Package.onUse(function(api) {
api.use('http', ['client', 'server']);
api.use('templating', 'client');
api.use('service-configuration', ['client', 'server']);
api.use('random', ['client', 'server']);
api.use('underscore', ['client', 'server']);

api.export('RedditOauth');

Expand Down
2 changes: 1 addition & 1 deletion reddit_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RedditOauth.requestCredential = function (options, callback) {
var loginStyle = OAuth._loginStyle('steam', config, options);
var state = OAuth._stateParam(loginStyle, credentialToken);

var scope = ['read'];
var scope = ['identity'];
if (options && options.requestPermissions) {
scope = options.requestPermissions.join(',');
}
Expand Down
33 changes: 22 additions & 11 deletions reddit_server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
RedditOauth = {};

// Set a default value for user-agent to use with http-requests.
RedditOauth.userAgent = "Meteor/1.0";

var urlUtil = Npm.require('url');

OAuth.registerService('reddit', 2, null, function(query) {

var response = getTokenResponse(query);
var accessToken = response.accessToken;
var refreshToken = response.refreshToken;
var scope = (response.scope && typeof response.scope == 'string') ?
response.scope.split(' ') : [];
var identity = getIdentity(accessToken);

var serviceData = {
id: identity.name,
accessToken: accessToken,
refreshToken: refreshToken,
scope: scope,
expiresAt: (+new Date) + (1000 * response.expiresIn)
};

Expand Down Expand Up @@ -38,6 +46,7 @@ var isJSON = function (str) {

// returns an object containing:
// - accessToken
// - refreshToken
// - expiresIn: lifetime of token in seconds
var getTokenResponse = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'reddit'});
Expand All @@ -47,14 +56,15 @@ var getTokenResponse = function (query) {
var responseContent;
try {
// Request an access token
responseContent = Meteor.http.post(
responseContent = HTTP.call("POST",
"https://ssl.reddit.com/api/v1/access_token", {
auth: [config.appId, config.secret].join(':'),
params: {
grant_type: 'authorization_code',
code: query.code,
redirect_uri: Meteor.absoluteUrl("_oauth/reddit?close")
}
},
headers:{"User-Agent": RedditOauth.userAgent}
}).content;
} catch (err) {
throw new Error("Failed to complete OAuth handshake with reddit. " + err.message);
Expand All @@ -67,24 +77,25 @@ var getTokenResponse = function (query) {

// Success! Extract access token and expiration
var parsedResponse = JSON.parse(responseContent);
var accessToken = parsedResponse.access_token;
var expiresIn = parsedResponse.expires_in;

if (!accessToken) {
var tokenResponse = {};
tokenResponse.accessToken = parsedResponse.access_token;
tokenResponse.refreshToken = parsedResponse.refresh_token;
tokenResponse.scope = parsedResponse.scope;
tokenResponse.expiresIn = parsedResponse.expires_in;

if (!tokenResponse.accessToken) {
throw new Error("Failed to complete OAuth handshake with reddit " +
"-- can't find access token in HTTP response. " + responseContent);
}

return {
accessToken: accessToken,
expiresIn: expiresIn
};
return tokenResponse;
};

var getIdentity = function (accessToken) {
try {
return Meteor.http.get("https://oauth.reddit.com/api/v1/me", {
headers: { "Authorization": 'bearer ' + accessToken, "User-Agent": "Meteor/1.0"}
return HTTP.call("GET", "https://oauth.reddit.com/api/v1/me", {
headers: { "Authorization": 'bearer ' + accessToken, "User-Agent": RedditOauth.userAgent}
}).data;
} catch (err) {
throw new Error("Failed to fetch identity from reddit. " + err.message);
Expand Down