@@ -15,12 +15,15 @@ const AccountRecovery = require('./account-recovery')
1515const capabilityDiscovery = require ( './capability-discovery' )
1616const bodyParser = require ( 'body-parser' )
1717const API = require ( './api' )
18+ var debug = require ( './debug' )
19+ var OidcRpClient = require ( './oidc-rp-client' )
20+ var oidcHandler = require ( './handlers/oidc' )
1821
1922var corsSettings = cors ( {
2023 methods : [
2124 'OPTIONS' , 'HEAD' , 'GET' , 'PATCH' , 'POST' , 'PUT' , 'DELETE'
2225 ] ,
23- exposedHeaders : 'User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Updates-Via, Allow, Content-Length' ,
26+ exposedHeaders : 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Updates-Via, Allow, Content-Length' ,
2427 credentials : true ,
2528 maxAge : 1728000 ,
2629 origin : true ,
@@ -30,6 +33,7 @@ var corsSettings = cors({
3033function createApp ( argv = { } ) {
3134 var ldp = new LDP ( argv )
3235 var app = express ( )
36+ var oidcConfig = argv . oidc
3337
3438 app . use ( corsSettings )
3539
@@ -56,6 +60,8 @@ function createApp (argv = {}) {
5660 // Setting options as local variable
5761 app . locals . ldp = ldp
5862 app . locals . appUrls = argv . apps // used for service capability discovery
63+ app . locals . oidcConfig = oidcConfig
64+ app . locals . rootUrl = argv . rootUrl
5965
6066 if ( argv . email && argv . email . host ) {
6167 app . locals . email = new EmailService ( argv . email )
@@ -93,6 +99,21 @@ function createApp (argv = {}) {
9399 // Session
94100 app . use ( session ( sessionSettings ) )
95101
102+ // OpenID Connect Auth
103+ if ( oidcConfig && ldp . auth === 'oidc' ) {
104+ app . options ( '*' , oidcHandler . oidcIssuerHeader )
105+ debug . idp ( 'Auth: OIDC!' )
106+ var oidcRpClient = new OidcRpClient ( )
107+ // TODO: ensureTrustedClient is async, fix race condition on server startup
108+ debug . oidc ( 'Initializing local/trusted client...' )
109+ oidcRpClient . ensureTrustedClient ( oidcConfig )
110+ app . locals . oidc = oidcRpClient
111+
112+ app . use ( '/' , express . static ( path . join ( __dirname , '../static/oidc' ) ) )
113+ app . use ( '/' , oidcHandler . authenticate ( oidcRpClient ) )
114+ app . use ( '/api/oidc' , oidcHandler . api ( oidcRpClient ) )
115+ }
116+
96117 // Adding proxy
97118 if ( ldp . proxy ) {
98119 proxy ( app , ldp . proxy )
@@ -119,10 +140,10 @@ function createApp (argv = {}) {
119140
120141 var needsOverwrite = function ( req , res , next ) {
121142 checkMasterAcl ( req , function ( found ) {
122- if ( ! found ) {
143+ if ( ! found && ! ldp . idp ) {
123144 // this allows IdentityProvider to overwrite root acls
124145 idp . middleware ( true ) ( req , res , next )
125- } else if ( found && ldp . idp ) {
146+ } else if ( ldp . idp ) {
126147 idp . middleware ( false ) ( req , res , next )
127148 } else {
128149 next ( )
@@ -133,8 +154,10 @@ function createApp (argv = {}) {
133154 // adds POST /api/accounts/new
134155 // adds POST /api/accounts/newCert
135156 app . get ( '/' , idp . get . bind ( idp ) )
157+ app . post ( '/api/accounts/signin' ,
158+ bodyParser . urlencoded ( { extended : false } ) , API . accounts . signin ( ) )
136159 app . use ( '/api/accounts' , needsOverwrite )
137- app . post ( '/api/accounts/signin ' , bodyParser . urlencoded ( { extended : false } ) , API . accounts . signin ( ) )
160+ app . get ( '/signout ' , API . accounts . signout ( ) )
138161 app . post ( '/api/accounts/signout' , API . accounts . signout ( ) )
139162 }
140163
0 commit comments