Skip to content

Commit

Permalink
Merge pull request joemccann#149 from submitteddenied/plugin_config_f…
Browse files Browse the repository at this point in the history
…rom_environment

Plugin config from environment
  • Loading branch information
joemccann committed Apr 15, 2014
2 parents fc7573a + 77a387c commit 6e12378
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 73 deletions.
27 changes: 17 additions & 10 deletions plugins/dropbox/README.md
Expand Up @@ -4,13 +4,20 @@ Dropbox Dillinger Plugin
0. Create your app with dropbox: https://www.dropbox.com/developers/apps
1. Create your `dropbox-config.json`. It needs to contain:

```
{
"app_key": "YOUR_KEY",
"app_secret": "YOUR_SECRET",
"callback_url": "YOUR_CALLBACK_URL",
"auth_url": "https://www.dropbox.com/1/oauth/authorize",
"request_token_url": "https://api.dropbox.com/1/oauth/request_token",
"access_token_url": "https://api.dropbox.com/1/oauth/access_token"
}
```
{
"app_key": "YOUR_KEY",
"app_secret": "YOUR_SECRET",
"callback_url": "YOUR_CALLBACK_URL",
"auth_url": "https://www.dropbox.com/1/oauth/authorize",
"request_token_url": "https://api.dropbox.com/1/oauth/request_token",
"access_token_url": "https://api.dropbox.com/1/oauth/access_token"
}

Optional configuration via environment
==

Set the following environment variables if adding `dropbox-config.json` may present a challenge (when deploying on Heroku for example)

dropbox_app_key=YOUR_KEY
dropbox_app_secret=YOUR_SECRET
dropbox_callback_url=YOUR_CALLBACK_URL
46 changes: 29 additions & 17 deletions plugins/dropbox/dropbox.js
Expand Up @@ -14,6 +14,18 @@ var dropbox_config_file = path.resolve(__dirname, 'dropbox-config.json')
if(fs.existsSync(dropbox_config_file)) {
dropbox_config = JSON.parse( fs.readFileSync( dropbox_config_file, 'utf-8' ) )
isConfigEnabled = true
} else if(process.env.dropbox_app_key !== undefined) {
dropbox_config = {
"app_key": process.env.dropbox_app_key,
"app_secret": process.env.dropbox_app_secret,
"callback_url": process.env.dropbox_callback_url,
"auth_url": "https://www.dropbox.com/1/oauth/authorize",
"request_token_url": "https://api.dropbox.com/1/oauth/request_token",
"access_token_url": "https://api.dropbox.com/1/oauth/access_token",
"collections_url": "https://api-content.dropbox.com/1"
};
isConfigEnabled = true
console.log('Dropbox config found in environment. Plugin enabled. (Key: "' + dropbox_config.app_key + '")');
} else {
dropbox_config = {
"app_key": "YOUR_KEY"
Expand All @@ -29,18 +41,18 @@ if(fs.existsSync(dropbox_config_file)) {

exports.Dropbox = (function() {

var dboxapp = dbox.app({
"app_key": dropbox_config.app_key,
"app_secret": dropbox_config.app_secret,
var dboxapp = dbox.app({
"app_key": dropbox_config.app_key,
"app_secret": dropbox_config.app_secret,
"root": "dropbox" })


return {
isConfigured: isConfigEnabled,
isConfigured: isConfigEnabled,
config: dropbox_config,
getNewRequestToken: function(req, res, cb) {

// Create your auth_url for the view
// Create your auth_url for the view
dboxapp.requesttoken(function(status, request_token){

return cb(status, request_token)
Expand All @@ -55,7 +67,7 @@ exports.Dropbox = (function() {
dboxapp.accesstoken(req_token, function(status, access_token){
return cb(status, access_token)
})

}, // end getRemoteAccessToken()
getAccountInfo: function(dropbox_obj, cb) {
var access_token = {oauth_token : dropbox_obj.oauth.access_token, oauth_token_secret : dropbox_obj.oauth.access_token_secret};
Expand All @@ -65,22 +77,22 @@ exports.Dropbox = (function() {
dboxclient.account(function(status, reply){
return cb(status, reply)
})

}, // end getAccountInfo()
fetchDropboxFile: function(req, res) {

if(!req.session.isDropboxSynced){
res.type('text/plain')
return res.status(403).send("You are not authenticated with Dropbox.")
}
}

var access_token = {
oauth_token : req.session.dropbox.oauth.access_token,
oauth_token : req.session.dropbox.oauth.access_token,
oauth_token_secret : req.session.dropbox.oauth.access_token_secret
}

var dboxclient = dboxapp.client(access_token)

var pathToMdFile = req.body.mdFile

dboxclient.get(pathToMdFile, function(status, reply, metadata) {
Expand All @@ -95,9 +107,9 @@ exports.Dropbox = (function() {

},
searchForMdFiles: function(dropbox_obj, cb) {

// *sigh* http://forums.dropbox.com/topic.php?id=50266&replies=1

var access_token = {oauth_token : dropbox_obj.oauth.access_token, oauth_token_secret : dropbox_obj.oauth.access_token_secret};

var dboxclient = dboxapp.client(access_token)
Expand Down Expand Up @@ -133,14 +145,14 @@ exports.Dropbox = (function() {
})

})

},
saveToDropbox: function(req, res){

if(!req.session.isDropboxSynced){
res.type('text/plain')
return res.status(403).send("You are not authenticated with Dropbox.")
}
}

var access_token = {oauth_token : req.session.dropbox.oauth.access_token, oauth_token_secret : req.session.dropbox.oauth.access_token_secret};

Expand Down Expand Up @@ -173,6 +185,6 @@ exports.Dropbox = (function() {
} // end handleIncomingFlowRequest

}

})()

24 changes: 18 additions & 6 deletions plugins/github/README.md
Expand Up @@ -3,9 +3,21 @@ Github Dillinger Plugin

0. Create your app with Github: https://github.com/settings/applications/new
1. Create your `github-config.json`. It needs to contain:
{
"client_id": "YOUR_ID"
, "redirect_uri": "http://dillinger.io/"
, "client_secret": "YOUR_SECRET"
, "callback_url": "http://dillinger.io/oauth/github"
}

{
"client_id": "YOUR_ID",
"client_secret": "YOUR_SECRET",
"redirect_uri": "YOUR_REDIRECT_URI", // eg, http://dillinger.io
"callback_url": "YOUR_CALLBACK_URL" // eg, http://dillinger.io/oauth/github
}

Optional configuration via environment
==

Set the following environment variables if adding `github-config.json` may present a challenge (when deploying on Heroku for example)

github_client_id=YOUR_KEY
github_client_secret=YOUR_SECRET
github_callback_url=YOUR_CALLBACK_URL
github_redirect_uri=YOUR_REDIRECT_URI

77 changes: 43 additions & 34 deletions plugins/github/github.js
Expand Up @@ -4,14 +4,23 @@ var fs = require('fs')

var github_config_file = path.resolve(__dirname, 'github-config.json')
, github_config = {}
, isConfigEnabled = false
, isConfigEnabled = false

// ^^^helps with the home page view; should we show the github dropdown?

if(fs.existsSync(github_config_file)) {
github_config = JSON.parse( fs.readFileSync( github_config_file, 'utf-8' ) )
isConfigEnabled = true
} else {
github_config = JSON.parse( fs.readFileSync( github_config_file, 'utf-8' ) );
isConfigEnabled = true;
} else if(process.env.github_client_id !== undefined) {
github_config = {
"client_id": process.env.github_client_id,
"redirect_uri": process.env.github_redirect_uri,
"client_secret": process.env.github_client_secret,
"callback_url": process.env.github_callback_url
};
isConfigEnabled = true;
console.log('Github config found in environment. Plugin enabled. (Key: "' + github_config.client_id +'")');
} else {
github_config = {
"client_id": "YOUR_ID"
, "redirect_uri": "http://dillinger.io/"
Expand All @@ -22,25 +31,25 @@ if(fs.existsSync(github_config_file)) {
}

exports.Github = (function(){

var github_api = 'https://api.github.com/'

// String builder for auth url...
function _buildAuthUrl(){
return 'https://github.com/login/oauth/authorize?client_id='
+ github_config.client_id
+ '&scope=repo&redirect_uri='
return 'https://github.com/login/oauth/authorize?client_id='
+ github_config.client_id
+ '&scope=repo&redirect_uri='
+ github_config.callback_url
}

return {
isConfigured: isConfigEnabled,
github_config: github_config,
generateAuthUrl: function(req,res){
return _buildAuthUrl()
},
getUsername: function(req,res,cb){

var uri = github_api + 'user?access_token=' + req.session.github.oauth

var options = {
Expand All @@ -57,17 +66,17 @@ exports.Github = (function(){
console.error(e)
return res.redirect(r.statusCode)
}
else if(!e && r.statusCode === 200)
else if(!e && r.statusCode === 200)
{
d = JSON.parse(d)
req.session.github.username = d.login
req.session.github.username = d.login
cb && cb()
}
}) // end request.get()

}, // end getUsername
searchForMdFiles: function(req,res){

var uri = github_api + 'user/repos?access_token=' + req.session.github.oauth

var options = {
Expand All @@ -91,7 +100,7 @@ exports.Github = (function(){

d.forEach(function(el){

var item =
var item =
{
url: el.url
, name: el.name
Expand All @@ -110,10 +119,10 @@ exports.Github = (function(){
}) // end request callback
}, // end searchForMdFiles
fetchGithubBranches: function(req,res){
var uri = github_api
+ 'repos/'
+ req.session.github.username

var uri = github_api
+ 'repos/'
+ req.session.github.username
+ '/'
+ req.body.repo
+'/branches?access_token=' + req.session.github.oauth
Expand All @@ -128,27 +137,27 @@ exports.Github = (function(){
if(e) {
res.send(
{
error: 'Request error.'
error: 'Request error.'
, d: r.statusCode
})
}
else if(!e && r.statusCode === 200)
else if(!e && r.statusCode === 200)
{
res.send(d)
} // end else if
else{
res.json({error: 'Unable to fetch repos from Github.'})
}
}) // end request callback

}, // end fetchGithubBranches
fetchTreeFiles: function(req,res){

// /repos/:user/:repo/git/trees/:sha

var uri = github_api
+ 'repos/'
+ req.session.github.username
var uri = github_api
+ 'repos/'
+ req.session.github.username
+ '/'
+ req.body.repo
+ '/git/trees/'
Expand All @@ -165,11 +174,11 @@ exports.Github = (function(){
if(e) {
res.send(
{
error: 'Request error.'
error: 'Request error.'
, data: r.statusCode
})
}
else if(!e && r.statusCode === 200)
else if(!e && r.statusCode === 200)
{
d = JSON.parse(d)
res.json(d)
Expand All @@ -178,13 +187,13 @@ exports.Github = (function(){
res.json({error: 'Unable to fetch repos from Github.'})
}
}) // end request callback

}, // end fetchTreeFiles
fetchFile: function(req,res){

var uri = req.body.mdFile
, isPrivateRepo = /blob/.test(uri)

// https://api.github.com/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132
// If it is a private repo, we need to make an API call, because otherwise it is the raw file.
if(isPrivateRepo){
Expand All @@ -204,13 +213,13 @@ exports.Github = (function(){
if(e){
console.error(e)
res.send({
error: 'Request error.'
error: 'Request error.'
, data: r.statusCode
})
}
else if(!e && r.statusCode === 200){

var json_resp =
var json_resp =
{
data: d
, error: false
Expand All @@ -228,9 +237,9 @@ exports.Github = (function(){
res.json({error: 'Unable to fetch file from Github.'})
}
}) // end request callback

} // end fetchFile
}

})()

0 comments on commit 6e12378

Please sign in to comment.