-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/SLB-450-preview-auth' into dev
- Loading branch information
Showing
11 changed files
with
739 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
PROJECT_NAME=example | ||
DRUPAL_URL="https://nginx.${LAGOON_ENVIRONMENT}.${LAGOON_PROJECT}.ch4.amazee.io" | ||
|
||
# Authentication with OAuth2 | ||
AUTHENTICATION_TYPE=oauth2 | ||
OAUTH2_CLIENT_SECRET=REPLACE_ME | ||
OAUTH2_SESSION_SECRET=REPLACE_ME | ||
|
||
# Authentication with Basic Auth | ||
#AUTHENTICATION_TYPE=basic | ||
#BASIC_AUTH_USER=preview | ||
#BASIC_AUTH_PASSWORD=preview |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DRUPAL_URL="https://example.cms.amazeelabs.dev" | ||
OAUTH2_ENVIRONMENT_TYPE=production |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { NextFunction, Request, RequestHandler, Response } from 'express'; | ||
import basicAuth from 'express-basic-auth'; | ||
|
||
import { getConfig } from './config.js'; | ||
import { oAuth2AuthCodeMiddleware } from './oAuth2.js'; | ||
|
||
/** | ||
* Returns the Express authentication middleware based on the configuration. | ||
* | ||
* Favours OAuth2, then Basic Auth, then falling back to no auth | ||
* if not configured (= grant access). | ||
*/ | ||
export const getAuthenticationMiddleware = (): RequestHandler => | ||
((): RequestHandler => { | ||
const config = getConfig(); | ||
switch (config.authenticationType) { | ||
case 'oauth2': | ||
if (config.oAuth2) { | ||
return oAuth2AuthCodeMiddleware; | ||
} else { | ||
console.error('Missing OAuth2 configuration.'); | ||
} | ||
break; | ||
case 'basic': | ||
if (config.basicAuth) { | ||
return basicAuth({ | ||
users: { [config.basicAuth.username]: config.basicAuth.password }, | ||
challenge: true, | ||
}); | ||
} else { | ||
console.error('Missing basic auth configuration.'); | ||
} | ||
break; | ||
case 'noauth': | ||
break; | ||
default: | ||
console.error('Unknown authentication type.'); | ||
break; | ||
} | ||
|
||
return (req: Request, res: Response, next: NextFunction): void => next(); | ||
})(); | ||
|
||
/** | ||
* Checks if a session is required based on the configuration. | ||
*/ | ||
export const isSessionRequired = (): boolean => { | ||
let result = false; | ||
if (getConfig().oAuth2) { | ||
const oAuth2Config = getConfig().oAuth2; | ||
if (!oAuth2Config) { | ||
throw new Error('Missing OAuth2 configuration.'); | ||
} | ||
result = true; | ||
} | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export type PreviewConfig = { | ||
authenticationType: string; // 'oauth2' | 'basic' | 'noauth'; | ||
drupalHost: string; | ||
/** | ||
* Basic auth. | ||
*/ | ||
basicAuth?: { | ||
username: string; | ||
password: string; | ||
}; | ||
/** | ||
* OAuth2. | ||
*/ | ||
oAuth2?: { | ||
clientId: string; | ||
clientSecret: string; | ||
scope: string; | ||
tokenHost: string; | ||
tokenPath: string; | ||
authorizePath: string; | ||
sessionSecret: string; | ||
environmentType?: string; // 'development' | 'production'; | ||
}; | ||
}; | ||
|
||
export const getConfig = (): PreviewConfig => { | ||
return { | ||
authenticationType: process.env.AUTHENTICATION_TYPE || 'noauth', | ||
drupalHost: process.env.DRUPAL_URL || 'http://127.0.0.1:8888', | ||
basicAuth: { | ||
username: process.env.BASIC_AUTH_USER || 'test', | ||
password: process.env.BASIC_AUTH_PASSWORD || 'test', | ||
}, | ||
oAuth2: { | ||
clientId: process.env.OAUTH2_CLIENT_ID || 'preview', | ||
clientSecret: process.env.OAUTH2_CLIENT_SECRET || 'preview', | ||
scope: process.env.OAUTH2_SCOPE || 'preview', | ||
tokenHost: process.env.DRUPAL_URL || 'http://127.0.0.1:8888', | ||
tokenPath: process.env.OAUTH2_TOKEN_PATH || '/oauth/token', | ||
authorizePath: | ||
process.env.OAUTH2_AUTHORIZE_PATH || | ||
'/oauth/authorize?response_type=code', | ||
sessionSecret: process.env.OAUTH2_SESSION_SECRET || 'banana', | ||
environmentType: process.env.OAUTH2_ENVIRONMENT_TYPE || 'development', | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.