jaredhanson/oauth2orize — pull request #47 by tellnes (2013-07-13 21:16:02+00:00)
Title: Implement immediate callback

I do not think the API I have chosen is the best. I'm thinking about these:

```js
// 1)
// The one I have now.
server.authorization({ immediate: immediate }, findClient)

// 2)
server.authorization({}, immediate, findClient)

// 3)
server.authorization({}, findClient, immediate)

// 4)
server.immediate(immediate)

// 5)
// This breaks backward compatibility, but is the one I like most.
server.authorization(checkAuthorization)

// 6
server.authorization(function (clientID, redirectURI, scope, type, user, done) {
checkAuthorization(
{ clientID: clientID
, redirectURI: redirectURI
, scope: scope
, type: type
}
, user
)
}

// 7
// Here we need to copy areq before we adds the user property.
server.authorization(function (areq, done) {
checkAuthorization(areq, areq.user, done)
})

function checkAuthorization(areq, user, done) {
findClient(areq.clientID, areq.redirectURI, function (err, client) {
if (err) return done(err)

immediate(client, areq.user, function (err, allow) {
if (err) return done(err)

return done(null, client, areq.redirectURI, { allow: allow })
})
})
})

function immediate(client, user, done) {
db.authorizations.find(client, user, function (err, authorization) {
done(err, !!authorization)
})
}

function findClient(clientID, redirectURI, done) {
db.clients.findByClientId(clientID, function (err, client) {
if (err) return done(err)
return done(null, client, redirectURI)
})
}
```