Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Jul 14, 2013
1 parent 48593f0 commit 5f3cd83
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
36 changes: 18 additions & 18 deletions index.js
Expand Up @@ -37,7 +37,7 @@ Object.keys(request).forEach(function (key) {

if (self.expiresAt && self.expiresAt - new Date() < 60000) {
if (self.refreshToken && 'function' === typeof self._onRefreshed) {
exports.refresh(self.clientInfo, self.refreshToken, function (err, refreshed) {
self._refresh(self.refreshToken, function (err, refreshed) {
if (err) {
if ('function' === typeof self._onExpired) {
self._onExpired(self.accessToken)
Expand All @@ -56,7 +56,7 @@ Object.keys(request).forEach(function (key) {
self.accessToken = refreshed.accessToken
self.expiresAt = refreshed.expiresAt

exports.use(self.accessToken, options)
self._use(options)

return original(uri, options, callback)
})
Expand All @@ -71,7 +71,7 @@ Object.keys(request).forEach(function (key) {
}
else {
if (self.accessToken) {
exports.use(self.accessToken, options)
self._use(options)
}

return original(uri, options, callback)
Expand All @@ -80,13 +80,13 @@ Object.keys(request).forEach(function (key) {
})

Client.prototype.authorize = function (options) {
return exports.authorize(this.clientInfo, options)
return this._authorize(options)
}

Client.prototype.credentical = function (code, callback) {
var self = this

exports.credentical(self.clientInfo, code, function (err, credentical, userInfo) {
self._credentical(code, function (err, credentical, userInfo) {
if (err) {
return callback(err)
}
Expand Down Expand Up @@ -133,32 +133,32 @@ Client.prototype.expired = function (onExpired) {
return this
}

exports.Client = Client

function TokenExpiredError (/* ..., */token) {
Error.apply(this, [].slice.call(arguments, 0, -1))
this.token = [].slice.call(arguments, -1)[0]
Client.prototype._authorize = function (options) {
throw new Error('You can\'t call this method without override it.')
}

exports.TokenExpiredError = TokenExpiredError
inherits(TokenExpiredError, Error)

exports.authorize = function (clientInfo, options) {
Client.prototype._credentical = function (code, callback) {
throw new Error('You can\'t call this method without override it.')
}

exports.credentical = function (clientInfo, code, callback) {
Client.prototype._refresh = function (refreshToken, callback) {
throw new Error('You can\'t call this method without override it.')
}

exports.refresh = function (clientInfo, refreshToken, callback) {
Client.prototype._use = function (options) {
throw new Error('You can\'t call this method without override it.')
}

exports.use = function (accessToken, options) {
throw new Error('You can\'t call this method without override it.')
exports.Client = Client

function TokenExpiredError (/* ..., */token) {
Error.apply(this, [].slice.call(arguments, 0, -1))
this.token = [].slice.call(arguments, -1)[0]
}

exports.TokenExpiredError = TokenExpiredError
inherits(TokenExpiredError, Error)

function extend (to, from) {
Object.keys(from).forEach(function (key) {
to[key] = form[key]
Expand Down
40 changes: 20 additions & 20 deletions test/oauthic.test.js
Expand Up @@ -66,14 +66,14 @@ server.post('/oauth2/token', function (req, res, next) {
// prepares implemented oauthic
//

oauthic.authorize = function (clientInfo, options) {
clientInfo = clientInfo || {}
oauthic.Client.prototype._authorize = function (options) {
this.clientInfo = this.clientInfo || {}
options = options || {}

var query = {}

query['client_id'] = clientInfo.clientId
query['redirect_uri'] = clientInfo.redirectUri
query['client_id'] = this.clientInfo.clientId
query['redirect_uri'] = this.clientInfo.redirectUri

if (options.scope) {
query['scope'] = Array.isArray(options.scope)
Expand All @@ -85,17 +85,17 @@ oauthic.authorize = function (clientInfo, options) {
query['state'] = String(options.state)
}

return this.BASE_URL + '/oauth2/authorize?' + stringify(query)
return oauthic.BASE_URL + '/oauth2/authorize?' + stringify(query)
}

oauthic.credentical = function (clientInfo, code, callback) {
clientInfo = clientInfo || {}
oauthic.Client.prototype._credentical = function (code, callback) {
this.clientInfo = this.clientInfo || {}

request.post(this.BASE_URL + '/oauth2/token', { form: {
request.post(oauthic.BASE_URL + '/oauth2/token', { form: {
'code': code
, 'client_id': clientInfo.clientId
, 'client_secret': clientInfo.clientSecret
, 'redirect_uri': clientInfo.redirectUri
, 'client_id': this.clientInfo.clientId
, 'client_secret': this.clientInfo.clientSecret
, 'redirect_uri': this.clientInfo.redirectUri
, 'grant_type': 'authorization_code'
}}, function (err, res, body) {
if (err) {
Expand All @@ -121,14 +121,14 @@ oauthic.credentical = function (clientInfo, code, callback) {
})
}

oauthic.refresh = function (clientInfo, refreshToken, callback) {
clientInfo = clientInfo || {}
oauthic.Client.prototype._refresh = function (refreshToken, callback) {
this.clientInfo = this.clientInfo || {}

request.post(this.BASE_URL + '/oauth2/token', { form: {
request.post(oauthic.BASE_URL + '/oauth2/token', { form: {
'refresh_token': refreshToken
, 'client_id': clientInfo.clientId
, 'client_secret': clientInfo.clientSecret
, 'redirect_uri': clientInfo.redirectUri
, 'client_id': this.clientInfo.clientId
, 'client_secret': this.clientInfo.clientSecret
, 'redirect_uri': this.clientInfo.redirectUri
, 'grant_type': 'refresh_token'
}}, function (err, res, body) {
if (err) {
Expand All @@ -150,11 +150,11 @@ oauthic.refresh = function (clientInfo, refreshToken, callback) {
})
}

oauthic.use = function (accessToken, options) {
oauthic.Client.prototype._use = function (options) {
options.headers = options.headers || {}

if (accessToken) {
options.headers['Authorization'] = ['Bearer', accessToken].join(' ')
if (this.accessToken) {
options.headers['Authorization'] = ['Bearer', this.accessToken].join(' ')
}

return options
Expand Down

0 comments on commit 5f3cd83

Please sign in to comment.