|
| 1 | +'use strict'; |
| 2 | +// Load modules |
| 3 | + |
| 4 | +var oauth2orize = require('oauth2orize'); |
| 5 | +var server = oauth2orize.createServer(); |
| 6 | +var Hapi = null; |
| 7 | + |
| 8 | +// Declare internals |
| 9 | + |
| 10 | +var internals = { |
| 11 | + defaults: {} |
| 12 | +}; |
| 13 | + |
| 14 | +exports.register = function (plugin, options, next) { |
| 15 | + internals.setHapi(plugin.hapi); |
| 16 | + |
| 17 | + var settings = plugin.hapi.utils.applyToDefaults(internals.defaults, options); |
| 18 | + |
| 19 | + plugin.dependency('yar'); |
| 20 | + plugin.expose('settings', settings); |
| 21 | + plugin.expose('grant', internals.grant); |
| 22 | + plugin.expose('grants', oauth2orize.grant); |
| 23 | + plugin.expose('exchange', internals.exchange); |
| 24 | + plugin.expose('exchanges', oauth2orize.exchange); |
| 25 | + plugin.expose('authorize', internals.authorize); |
| 26 | + plugin.expose('decision', internals.decision); |
| 27 | + plugin.expose('token', internals.token); |
| 28 | + plugin.expose('errorHandler', internals.errorHandler); |
| 29 | + plugin.expose('serializeClient', internals.serializeClient); |
| 30 | + plugin.expose('deserializeClient', internals.deserializeClient); |
| 31 | + |
| 32 | + next(); |
| 33 | +}; |
| 34 | + |
| 35 | +internals.setHapi = function (module) { |
| 36 | + Hapi = Hapi || module; |
| 37 | +}; |
| 38 | + |
| 39 | +internals.grant = function (grant) { |
| 40 | + server.grant(grant); |
| 41 | +}; |
| 42 | + |
| 43 | +internals.exchange = function (exchange) { |
| 44 | + server.exchange(exchange); |
| 45 | +}; |
| 46 | + |
| 47 | +internals.authorize = function (request, reply, callback, authorization) { |
| 48 | + var express = internals.convertToExpress(request, reply); |
| 49 | + server.authorize(authorization)(express.req, express.res, function (err) { |
| 50 | + if (err) { |
| 51 | + console.log(err); |
| 52 | + } |
| 53 | + callback(express.req, express.res); |
| 54 | + }); |
| 55 | +}; |
| 56 | + |
| 57 | +internals.decision = function (request, reply, options, parse) { |
| 58 | + var result, |
| 59 | + express = internals.convertToExpress(request, reply), |
| 60 | + handler = function (err) { |
| 61 | + if (err) { |
| 62 | + console.log('Err1: ' + err); |
| 63 | + } |
| 64 | + }; |
| 65 | + options = options || {}; |
| 66 | + if (options && options.loadTransaction === false) { |
| 67 | + server.decision(options, parse)(express.req, express.res, handler); |
| 68 | + } else { |
| 69 | + result = server.decision(options, parse); |
| 70 | + result[0](express.req, express.res, function (err) { |
| 71 | + if (err) { |
| 72 | + console.log('Err2: ' + err); |
| 73 | + } |
| 74 | + result[1](express.req, express.res, handler); |
| 75 | + }); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | + |
| 80 | +internals.serializeClient = function (fn) { |
| 81 | + server.serializeClient(fn); |
| 82 | +}; |
| 83 | + |
| 84 | +internals.deserializeClient = function (fn) { |
| 85 | + server.deserializeClient(fn); |
| 86 | +}; |
| 87 | + |
| 88 | +internals.token = function () { |
| 89 | + server.token(); |
| 90 | +}; |
| 91 | + |
| 92 | +internals.errorHandler = function () { |
| 93 | + server.errorHandler(); |
| 94 | +}; |
| 95 | + |
| 96 | +internals.convertToExpress = function (request, reply) { |
| 97 | + var server = { |
| 98 | + req: { |
| 99 | + session: request.session, |
| 100 | + query: request.query, |
| 101 | + body: request.payload, |
| 102 | + user: request.user |
| 103 | + }, |
| 104 | + res: { |
| 105 | + redirect: function (uri) { |
| 106 | + reply().redirect(uri); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + request.session.lazy(true); |
| 111 | + return server; |
| 112 | +}; |
0 commit comments