Skip to content
Browse files

Working google oauth authentication

  • Loading branch information...
1 parent ea6f87e commit bd626f1d45064edf69d7a22c80170b796acd7e1b @benjreinhart benjreinhart committed Mar 9, 2013
View
36 components/oauth/controller.coffee
@@ -1,35 +1,17 @@
-qs = require 'qs'
-config = require "#{ process.env.APP_ROOT }/core/config"
-OauthSession = require './datastore'
-{tokenRequest} = require './lib/oauth_requests'
-
-googleApiConfig = config.google_apis
+OAuthSession = require './datastore'
+{getTokens, googleOAuthUrl} = require './lib/google_oauth'
exports.authenticate = (_, res) ->
- res.redirect buildOauthUrl()
+ res.redirect googleOAuthUrl
exports.authorize = (req, res) ->
{code} = req.query
- tokenRequest code, (response) ->
- res.json 200, { response }
-
-exports.persistTokens = (req, res) ->
- (new OauthSession req.body).save (err, record) ->
- res.send 200
-
-
-###########
-# PRIVATE #
-###########
+ getTokens code, (err, response) ->
+ return res.json 500, { err } if err?
+ return res.json 500, { response } if response.error?
-buildOauthUrl = ->
- "#{ config.oauth.url }?#{ buildQueryParams() }"
+ (new OAuthSession response).save (err) ->
+ return res.json 500, { err, message: 'Error persisting session' } if err?
-buildQueryParams = ->
- qs.stringify
- scope : googleApiConfig.scope
- client_id : googleApiConfig.client_id
- access_type : googleApiConfig.access_type
- redirect_uri : googleApiConfig.redirect_uri
- response_type : googleApiConfig.response_type
+ res.json 200, { message: 'Successfully authenticated' }
View
5 components/oauth/datastore.coffee
@@ -1,8 +1,9 @@
{ mongoose, db } = require "#{ process.env.APP_ROOT }/core/db"
-OauthSessionSchema = new mongoose.Schema
+OAuthSessionSchema = new mongoose.Schema
+ token_type: String
access_token: String
refresh_token: String
expires_in: Number
-module.exports = db.model('OauthSession', OauthSessionSchema)
+module.exports = db.model('OAuthSession', OAuthSessionSchema)
View
18 components/oauth/lib/google_oauth.coffee
@@ -0,0 +1,18 @@
+{OAuth2Client} = require 'googleapis'
+{google_apis: config} = require "#{ process.env.APP_ROOT }/core/config"
+
+oauth2Client = new OAuth2Client(
+ config.client_id,
+ config.client_secret,
+ config.redirect_uri
+)
+
+exports.googleOAuthUrl =
+ oauth2Client.generateAuthUrl
+ scope : config.scope
+ access_type : config.access_type
+ response_type : config.response_type
+ approval_prompt : config.approval_prompt
+
+exports.getTokens = (code, cb) ->
+ oauth2Client.getToken code, cb
View
32 components/oauth/lib/oauth_requests.coffee
@@ -1,32 +0,0 @@
-qs = require 'qs'
-https = require 'https'
-config = require "#{ process.env.APP_ROOT }/core/config"
-googleApiConfig = config.google_apis
-
-exports.tokenRequest = (code, cb) ->
- options = buildTokenRequest()
-
- req = https.request options, (response) ->
- data = ''
- response.on 'data', (chunk) ->
- data += chunk
- response.on 'end', ->
- cb null, data
-
- req.write requestBody()
- req.end()
-
-buildTokenRequest = (authCode) ->
- hostname: 'accounts.google.com'
- path: '/o/oauth2/token'
- port: 443
- method: 'POST'
- headers: { 'content-type': 'application/x-www-form-urlencoded' }
-
-requestBody = (authCode) ->
- qs.stringify
- code : authCode
- client_id : googleApiConfig.client_id
- grant_type : googleApiConfig.grant_type
- redirect_uri : 'http://blackmailboxapp.com/oauth/persist_tokens'
- client_secret : googleApiConfig.client_secret
View
7 core/routes.coffee
@@ -3,7 +3,7 @@
{requireController, namespace} = require "#{ APP_ROOT }/core/lib/route_helpers"
Users = requireController 'users'
-Oauth = requireController 'oauth'
+OAuth = requireController 'oauth'
Promises = requireController 'promises'
##########
@@ -19,6 +19,5 @@ namespace 'api', ({ get, post }) ->
post '/users/:id/promises', Promises.create
namespace 'oauth', ({ get }) ->
- get '/authenticate', Oauth.authenticate
- get '/authorize', Oauth.authorize
- get '/persist_tokens', Oauth.persistTokens
+ get '/authenticate', OAuth.authenticate
+ get '/authorize', OAuth.authorize
View
1 package.json
@@ -17,6 +17,7 @@
"request": ">=2.14.0",
"mongoose": ">=3.5.7",
"underscore": ">=1.4.4",
+ "googleapis": ">=0.2.1-alpha",
"coffee-script": ">=1.4.0"
},
"devDependencies": {},

0 comments on commit bd626f1

Please sign in to comment.
Something went wrong with that request. Please try again.