@@ -16,12 +16,15 @@ const capabilityDiscovery = require('./capability-discovery')
1616const bodyParser = require ( 'body-parser' )
1717const API = require ( './api' )
1818var authentication = require ( './handlers/authentication' )
19+ var debug = require ( './debug' )
20+ var OidcRpClient = require ( './oidc-rp-client' )
21+ var oidcHandler = require ( './handlers/oidc' )
1922
2023var corsSettings = cors ( {
2124 methods : [
2225 'OPTIONS' , 'HEAD' , 'GET' , 'PATCH' , 'POST' , 'PUT' , 'DELETE'
2326 ] ,
24- exposedHeaders : 'User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Updates-Via, Allow, Content-Length' ,
27+ exposedHeaders : 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Updates-Via, Allow, Content-Length' ,
2528 credentials : true ,
2629 maxAge : 1728000 ,
2730 origin : true ,
@@ -31,6 +34,7 @@ var corsSettings = cors({
3134function createApp ( argv = { } ) {
3235 var ldp = new LDP ( argv )
3336 var app = express ( )
37+ var oidcConfig = argv . oidc
3438
3539 app . use ( corsSettings )
3640
@@ -57,6 +61,8 @@ function createApp (argv = {}) {
5761 // Setting options as local variable
5862 app . locals . ldp = ldp
5963 app . locals . appUrls = argv . apps // used for service capability discovery
64+ app . locals . oidcConfig = oidcConfig
65+ app . locals . rootUrl = argv . rootUrl
6066
6167 if ( argv . email && argv . email . host ) {
6268 app . locals . email = new EmailService ( argv . email )
@@ -94,6 +100,21 @@ function createApp (argv = {}) {
94100 // Session
95101 app . use ( session ( sessionSettings ) )
96102
103+ // OpenID Connect Auth
104+ if ( oidcConfig && ldp . auth === 'oidc' ) {
105+ app . options ( '*' , oidcHandler . oidcIssuerHeader )
106+ debug . idp ( 'Auth: OIDC!' )
107+ var oidcRpClient = new OidcRpClient ( )
108+ // TODO: ensureTrustedClient is async, fix race condition on server startup
109+ debug . oidc ( 'Initializing local/trusted client...' )
110+ oidcRpClient . ensureTrustedClient ( oidcConfig )
111+ app . locals . oidc = oidcRpClient
112+
113+ app . use ( '/' , express . static ( path . join ( __dirname , '../static/oidc' ) ) )
114+ app . use ( '/' , oidcHandler . authenticate ( oidcRpClient ) )
115+ app . use ( '/api/oidc' , oidcHandler . api ( oidcRpClient ) )
116+ }
117+
97118 // Adding proxy
98119 if ( ldp . proxy ) {
99120 proxy ( app , ldp . proxy )
@@ -120,10 +141,10 @@ function createApp (argv = {}) {
120141
121142 var needsOverwrite = function ( req , res , next ) {
122143 checkMasterAcl ( req , function ( found ) {
123- if ( ! found ) {
144+ if ( ! found && ! ldp . idp ) {
124145 // this allows IdentityProvider to overwrite root acls
125146 idp . middleware ( true ) ( req , res , next )
126- } else if ( found && ldp . idp ) {
147+ } else if ( ldp . idp ) {
127148 idp . middleware ( false ) ( req , res , next )
128149 } else {
129150 next ( )
@@ -134,8 +155,10 @@ function createApp (argv = {}) {
134155 // adds POST /api/accounts/new
135156 // adds POST /api/accounts/newCert
136157 app . get ( '/' , idp . get . bind ( idp ) )
158+ app . post ( '/api/accounts/signin' ,
159+ bodyParser . urlencoded ( { extended : false } ) , API . accounts . signin ( ) )
137160 app . use ( '/api/accounts' , needsOverwrite )
138- app . post ( '/api/accounts/signin ' , bodyParser . urlencoded ( { extended : false } ) , API . accounts . signin ( ) )
161+ app . get ( '/signout ' , API . accounts . signout ( ) )
139162 app . post ( '/api/accounts/signout' , API . accounts . signout ( ) )
140163 app . post ( '/api/messages' , authentication , bodyParser . urlencoded ( { extended : false } ) , API . messages . send ( ) )
141164 }
0 commit comments