Skip to content

Instantly share code, notes, and snippets.

@gouravd
Forked from funseiki/ICloud.java
Last active August 29, 2015 14:24
Show Gist options
  • Save gouravd/410907f490555aecef92 to your computer and use it in GitHub Desktop.
Save gouravd/410907f490555aecef92 to your computer and use it in GitHub Desktop.
// Uses retrofit to generate an implementation
public interface ICloud {
static String serviceProviderHeader = "X-Auth-Service-Provider";
static String credentialsAuthorizationHeader = "X-Verify-Credentials-Authorization";
@GET("/verify_credentials?provider=digits")
void verifyCredentials(
@Header(serviceProviderHeader) String serviceProvider,
@Header(credentialsAuthorizationHeader) String authorization,
@Query("id") long id,
Callback<String> callback);
}
public class SessionManager {
// ... //
// region Session related
public void verifySession(DigitsSession session, Callback callback) {
TwitterAuthConfig authConfig = TwitterCore.getInstance().getAuthConfig();
// Cast from AuthToken to TwitterAuthToken
TwitterAuthToken authToken = (TwitterAuthToken)session.getAuthToken();
OAuthSigning oAuthSigning = new OAuthSigning(authConfig, authToken);
// First value should be the location we're querying to twitter.
// The second is the actual validation information
Map<String, String> authHeaders = oAuthSigning.getOAuthEchoHeadersForVerifyCredentials();
try {
Cloud.verifyCredentials(
authHeaders.get(ICloud.serviceProviderHeader),
authHeaders.get(ICloud.credentialsAuthorizationHeader),
session.getId(),
callback);
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage().toString());
}
}
}
// The webhooks module
var express = require('express')
, _ = require('underscore');
/**
* Handles authorizing with Twitter
*/
function DigitsAuth(req, res) {
// These are the input fields coming in
var headers = [
// The url we'll be making the twitter request to
'X-Auth-Service-Provider',
// These are the credentials twitter will be verifying
'X-Verify-Credentials-Authorization'
];
// Check for any missing inputs
var missingValues = _.reject(headers, function(val) {
return req.get(val);
});
// Return an error if we don't have proper inputs
if(missingValues.length > 0) {
res.status(400).json({
error: 'Auth information missing',
missingInputs: missingValues
});
console.log('Missing values', missingValues);
return;
}
// Send the request to twitter
Parse.Cloud.httpRequest({
method: 'GET',
url: req.get(headers[0]),
headers: {'Authorization': req.get(headers[1])},
success: function(httpResponse) {
var obj = JSON.parse(httpResponse.text);
// TODO: Check for errors. Add to sessions table. Send JSON response
res.status(httpResponse.status).send("success");
},
error: function(httpResponse) {
res.status(400).json({
error: 'Unable to make a twitter request'
});
console.log('Twitter error', httpResponse.text);
}
});
}
/** Setup Express **/
var app = express();
app.use(express.bodyParser()); // Middleware for reading request body
app.get('/verify_credentials', DigitsAuth);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment