@@ -16,7 +16,15 @@ router.get('/user/status', function(req, res) {
1616 var headers = req . headers ;
1717 noCache ( res ) ;
1818 if ( req . session . user === undefined ) {
19- res . send ( { authenticated : false } ) ;
19+ if ( options . guestAccess ) {
20+ res . send ( {
21+ authenticated : true ,
22+ username : options . defaultUser ,
23+ profile : { fullname : 'Guest' }
24+ } ) ;
25+ } else {
26+ res . send ( { authenticated : false } ) ;
27+ }
2028 } else {
2129 delete headers [ 'content-length' ] ;
2230 var status = http . get ( {
@@ -29,15 +37,15 @@ router.get('/user/status', function(req, res) {
2937 if ( response . statusCode === 200 ) {
3038 response . on ( 'data' , function ( chunk ) {
3139 var json = JSON . parse ( chunk ) ;
32- if ( json . user !== undefined ) {
33- res . status ( 200 ) . send ( {
34- authenticated : true ,
35- username : req . session . user . name ,
36- profile : json . user
37- } ) ;
38- } else {
40+ if ( json . user === undefined ) {
3941 console . log ( 'did not find chunk.user' ) ;
4042 }
43+ res . status ( 200 ) . send ( {
44+ authenticated : true ,
45+ username : req . session . user . name ,
46+ profile : json . user || { }
47+ } ) ;
48+ req . session . user . profile = json . user || { } ;
4149 } ) ;
4250 } else if ( response . statusCode === 404 ) {
4351 //no profile yet for user
@@ -62,68 +70,74 @@ router.post('/user/login', function(req, res) {
6270 // Attempt to read the user's profile, then check the response code.
6371 // 404 - valid credentials, but no profile yet
6472 // 401 - bad credentials
65- var username = req . body . username ;
66- var password = req . body . password ;
73+ var username = req . body . username || '' ;
74+ var password = req . body . password || '' ;
6775 var headers = req . headers ;
76+
6877 //make sure login isn't cached
6978 noCache ( res ) ;
7079
71- // remove content length so ML doesn't wait for request body
72- // that isn't being passed.
73- delete headers [ 'content-length' ] ;
74- var login = http . get ( {
75- hostname : options . mlHost ,
76- port : options . mlHttpPort ,
77- path : '/v1/documents?uri=/api/users/' + username + '.json' ,
78- headers : headers ,
79- auth : username + ':' + password
80- } , function ( response ) {
81- if ( response . statusCode === 401 ) {
82- res . statusCode = 401 ;
83- res . send ( 'Unauthenticated' ) ;
84- } else if ( response . statusCode === 404 ) {
85- // authentication successful, but no profile defined
86- req . session . user = {
87- name : username ,
88- password : password
89- } ;
90- res . status ( 200 ) . send ( {
91- authenticated : true ,
92- username : username ,
93- profile : { }
94- } ) ;
95- } else {
96- console . log ( 'code: ' + response . statusCode ) ;
97- if ( response . statusCode === 200 ) {
98- // authentication successful, remember the username
80+ var startsWithMatch = new RegExp ( '^' + options . appName + '-' ) ;
81+ if ( options . appUsersOnly && ! startsWithMatch . test ( username ) ) {
82+ res . status ( 403 ) . send ( 'Forbidden' ) ;
83+ } else {
84+ // remove content length so ML doesn't wait for request body
85+ // that isn't being passed.
86+ delete headers [ 'content-length' ] ;
87+
88+ var login = http . get ( {
89+ hostname : options . mlHost ,
90+ port : options . mlHttpPort ,
91+ path : '/v1/documents?uri=/api/users/' + username + '.json' ,
92+ headers : headers ,
93+ auth : username + ':' + password
94+ } , function ( response ) {
95+
96+ if ( response . statusCode === 401 ) {
97+ res . status ( 401 ) . send ( 'Unauthenticated' ) ;
98+ } else if ( response . statusCode === 404 ) {
99+ // authentication successful, but no profile defined
99100 req . session . user = {
100101 name : username ,
101102 password : password
102103 } ;
103- response . on ( 'data' , function ( chunk ) {
104- var json = JSON . parse ( chunk ) ;
105- if ( json . user !== undefined ) {
104+ res . status ( 200 ) . send ( {
105+ authenticated : true ,
106+ username : username ,
107+ profile : { }
108+ } ) ;
109+ } else {
110+ console . log ( 'code: ' + response . statusCode ) ;
111+ if ( response . statusCode === 200 ) {
112+ // authentication successful, remember the username
113+ req . session . user = {
114+ name : username ,
115+ password : password
116+ } ;
117+ response . on ( 'data' , function ( chunk ) {
118+ var json = JSON . parse ( chunk ) ;
119+ if ( json . user === undefined ) {
120+ console . log ( 'did not find chunk.user' ) ;
121+ }
106122 res . status ( 200 ) . send ( {
107123 authenticated : true ,
108124 username : username ,
109- profile : json . user
125+ profile : json . user || { }
110126 } ) ;
111- req . session . user . profile = json . user ;
112- } else {
113- console . log ( 'did not find chunk.user' ) ;
114- }
115- } ) ;
116- } else {
117- res . statusCode = response . statusCode ;
118- res . send ( response . statusMessage ) ;
127+ req . session . user . profile = json . user || { } ;
128+ } ) ;
129+ } else {
130+ res . status ( response . statusCode ) . send ( response . statusMessage ) ;
131+ }
119132 }
120- }
121- } ) ;
122133
123- login . on ( 'error' , function ( e ) {
124- console . log ( JSON . stringify ( e ) ) ;
125- console . log ( 'login failed: ' + e . statusCode ) ;
126- } ) ;
134+ } ) ;
135+
136+ login . on ( 'error' , function ( e ) {
137+ console . log ( JSON . stringify ( e ) ) ;
138+ console . log ( 'login failed: ' + e . statusCode ) ;
139+ } ) ;
140+ }
127141} ) ;
128142
129143router . get ( '/user/logout' , function ( req , res ) {
0 commit comments