File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var API = require ( 'booljs-api' )
4+ , passport = require ( 'passport' ) ;
5+
6+ module . exports = class BoolJSPassport extends API . Middleware {
7+ constructor ( ) {
8+ super ( 'booljs-passport' , [ require . resolve ( './route_middleware' ) ] ) ;
9+ }
10+
11+ action ( _instance ) {
12+ var app = _instance . getComponents ( )
13+ , configuration = app . configuration . get ( 'security' )
14+ , moduleName = (
15+ configuration . passport && configuration . passport . module
16+ ) || 'Passport'
17+ , strategiesList = (
18+ configuration . passport && configuration . passport . strategies
19+ ) || [ ]
20+ , strategies = (
21+ app . dao [ moduleName ] && new app . dao [ moduleName ] ( )
22+ ) || { } ;
23+
24+ for ( var strategy of strategiesList ) {
25+ if ( strategies [ strategy ] ) {
26+ passport . use ( strategy , strategies [ strategy ] ( passport ) ) ;
27+ }
28+ }
29+
30+ return passport . initialize ( ) ;
31+ }
32+ } ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var API = require ( 'booljs-api' )
4+ , passport = require ( 'passport' ) ;
5+
6+ var usedStrategies = { } ;
7+
8+ module . exports = class BoolJSPassportRoute extends API . RouteMiddleware {
9+ constructor ( ) {
10+ super ( 'booljs-passport-route' , 'omittable' , {
11+ public : true
12+ } ) ;
13+ }
14+
15+ action ( _instance , router , route ) {
16+ var app = _instance . getComponents ( )
17+ , configuration = app . configuration . get ( 'security' )
18+ , strategy = (
19+ route . authentication && route . authentication . strategy
20+ ) || (
21+ configuration . passport && configuration . passport . defaultStrategy
22+ )
23+ , authOptions = (
24+ route . authentication && route . authentication . options
25+ ) || (
26+ configuration . passport && configuration . passport . strategyOptions
27+ ) ;
28+
29+ if ( ! usedStrategies [ strategy ] ) {
30+ var strategyMiddleware ;
31+
32+ if ( strategy && authOptions ) {
33+ strategyMiddleware = passport . authenticate (
34+ strategy , authOptions
35+ ) ;
36+ } else if ( strategy ) {
37+ strategyMiddleware = passport . authenticate ( strategy ) ;
38+ } else {
39+ strategyMiddleware = ( req , res , next ) => { next ( ) ; } ;
40+ }
41+
42+ usedStrategies [ strategy ] = strategyMiddleware ;
43+ }
44+
45+ return [
46+ usedStrategies [ strategy ] ,
47+ ( req , res , next ) => {
48+ if ( req . query . access_token ) delete req . query . access_token ;
49+ next ( ) ;
50+ }
51+ ] ;
52+ }
53+ } ;
Original file line number Diff line number Diff line change 22 "name" : " booljs-passport" ,
33 "version" : " 0.0.3" ,
44 "description" : " Middleware to connect Passport.js with Bool.js and a RouteMiddleware to authenticate API users through Auth Strategies." ,
5- "main" : " index.js" ,
5+ "main" : " lib/ index.js" ,
66 "scripts" : {
77 "test" : " NODE_ENV=test mocha --timeout 5000 --recursive test"
88 },
2222 "url" : " https://github.com/booljs/booljs-passport/issues"
2323 },
2424 "homepage" : " https://github.com/booljs/booljs-passport#readme" ,
25+ "engines" : {
26+ "node" : " >=4.0.0" ,
27+ "npm" : " >=3.0.0"
28+ },
2529 "peerDependencies" : {
26- "passport" : " ^0.3.2"
30+ "passport" : " ^0.3.2" ,
31+ "booljs-api" : " 0.3.x"
2732 },
2833 "devDependencies" : {
34+ "chai" : " ^3.5.0" ,
2935 "mocha" : " ^2.3.4" ,
3036 "supertest" : " ^1.1.0" ,
3137 "supertest-as-promised" : " ^2.0.2"
Original file line number Diff line number Diff line change @@ -7,4 +7,5 @@ booljs('com.example.api', [
77 resolver ( '' ) , 'passport-http-bearer'
88] ) . setBase ( 'example' ) . run ( ) ;
99
10+ global . expect = require ( 'chai' ) . expect ;
1011global . Agent = require ( 'supertest-as-promised' ) ;
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- describe ( 'Plugin' , ( ) => {
4-
5- it ( 'Integrity test passes' , ( ) => {
6- require ( '..' ) . checkIntegrity ( ) ;
7- } ) ;
8- } ) ;
9-
103describe ( 'Bearer' , ( ) => {
114 var booljs = require ( 'bool.js' )
125 , agent ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ describe ( 'Plugin' , ( ) => {
4+
5+ it ( 'Integrity test passes' , ( ) => {
6+ let Plugin = require ( '..' ) ;
7+ new Plugin ( ) ;
8+ } ) ;
9+ } ) ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ describe ( 'User' , ( ) => {
4+ var booljs = require ( 'bool.js' )
5+ , agent ;
6+
7+ before ( ( ) => {
8+ return booljs ( 'com.example.api' ) . run ( ) . then ( function ( api ) {
9+ agent = new Agent ( api . server ) ;
10+ } ) ;
11+ } ) ;
12+
13+ it ( `Unauthenticated request returns 401` , ( ) => {
14+ return ( agent
15+ . get ( '/users/me' )
16+ . set ( 'Authorization' , 'Bearer 123456' )
17+ . expect ( 200 )
18+ ) . then ( res => res . body ) . then ( body => {
19+ /* jshint -W030 */ expect ( body . data ) . to . exist ;
20+ } ) ;
21+ } ) ;
22+ } ) ;
You can’t perform that action at this time.
0 commit comments