Skip to content

Instantly share code, notes, and snippets.

@mcfog
Created January 18, 2012 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfog/1631813 to your computer and use it in GitHub Desktop.
Save mcfog/1631813 to your computer and use it in GitHub Desktop.
everyauth module for qq
everyauth = require 'everyauth'
oauthModule = require 'everyauth/lib/modules/oauth2'
url = require 'url'
rest = require 'everyauth/lib/restler'
qq = module.exports = oauthModule.submodule('qq')
.configurable do
scope: 'URL identifying the Tencent QQ service to be accessed. See the documentation for the API you\'d like to use for what scope to specify. To specify more than one scope, list each one separated with a space.'
.oauthHost 'https://graph.qq.com'
.apiHost 'https://graph.qq.com'
.authPath '/oauth2.0/authorize'
.authQueryParam 'response_type', 'code'
.accessTokenPath '/oauth2.0/token'
.accessTokenParam 'grant_type', 'authorization_code'
.accessTokenHttpMethod 'post'
.postAccessTokenParamsVia 'data'
.entryPath '/auth/qq'
.callbackPath '/auth/qq/callback'
.authQueryParam 'scope', ->
@_scope && @scope!
.authCallbackDidErr (req)->
parsedUrl = url.parse(req.url, true);
parsedUrl.query && !!parsedUrl.query.error;
.handleAuthCallbackError (req, res)->
parsedUrl = url.parse(req.url, true)
errorDesc = parsedUrl.query.error + "; " + parsedUrl.query.error_description
if (res.render)
res.render __dirname + '/../views/auth-fail.jade',
errorDescription: errorDesc
else
throw new Error 'You must configure handleAuthCallbackError if you are not using express'
.convertErr ->new Error it
.fetchOAuthUser (accessToken)->
promise = this.Promise!
handleError = (data, res)->
promise.fail data||"unknown"
rest.get 'https://graph.qq.com/oauth2.0/me',
query:
access_token: accessToken
.on \success, (data, res)~>
id=false
callback = ->id:=it.openid
eval data
if (!id)
return promise.fail(data);
rest.get 'https://graph.qq.com/user/get_user_info',
query:
access_token: accessToken
oauth_consumer_key: @appId!
openid:id
format:\json
.on \success, (data, res)->
data = JSON.parse data
return handleError "#{data.ret}:#{data.msg}" if data.ret != 0
delete data.ret
data.id = id
promise.fulfill data
.on \error, handleError
.on \error, handleError
promise
Object.defineProperty everyauth, \qq,
get: ->
mod = @modules.qq || (@modules.qq = qq)
mod.everyauth = @ if (!mod.everyauth)
@enabled.qq = mod if (mod.shouldSetup)
mod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment