Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check authorities in app adapter [LIBS-370] #757

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: ensure core app name formatting and handle legacy versions
  • Loading branch information
KaiVandivier committed Oct 20, 2022
commit 09b100c5b263f0d8490c63940db115da85d9677f
21 changes: 12 additions & 9 deletions adapter/src/components/AuthBoundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import i18n from '../locales'

const IS_PRODUCTION_ENV = process.env.NODE_ENV === 'production'
const APP_MANAGER_AUTHORITY = 'M_dhis-web-maintenance-appmanager'
const REQUIRED_APP_AUTHORITY = process.env.REACT_APP_DHIS2_APP_AUTH_NAME
const APP_AUTH_NAME = process.env.REACT_APP_DHIS2_APP_AUTH_NAME
const LEGACY_APP_AUTH_NAME = process.env.REACT_APP_DHIS2_APP_LEGACY_AUTH_NAME

const isAppAvailable = (authorities) => {
const isAppAvailable = ({ userAuthorities, apiVersion }) => {
// Skip check on dev
// TODO: should we check on dev environments too?
if (!IS_PRODUCTION_ENV) {
return true
}

// On server versions < 35, auth name uses config.title instead of .name
const requiredAppAuthority =
apiVersion >= 35 ? APP_AUTH_NAME : LEGACY_APP_AUTH_NAME

// Check for three possible authorities
return authorities.some((authority) =>
['ALL', APP_MANAGER_AUTHORITY, REQUIRED_APP_AUTHORITY].includes(
authority
)
return userAuthorities.some((authority) =>
['ALL', APP_MANAGER_AUTHORITY, requiredAppAuthority].includes(authority)
)
}

Expand All @@ -27,9 +30,9 @@ const isAppAvailable = (authorities) => {
* app.
*/
export function AuthBoundary({ user, children }) {
const { appName } = useConfig()
const { appName, apiVersion } = useConfig()

return isAppAvailable(user.authorities) ? (
return isAppAvailable({ userAuthorities: user.authorities, apiVersion }) ? (
children
) : (
<CenteredContent>
Expand Down
17 changes: 13 additions & 4 deletions cli/src/lib/constructAppUrl.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module.exports.constructAppUrl = (baseUrl, config, serverVersion) => {
const formatUrlSafeAppSlug = (appName) => {
return appName.replace(/[^A-Za-z0-9\s-]/g, '').replace(/\s+/g, '-')
}

const constructAppUrl = (baseUrl, config, serverVersion) => {
let appUrl = baseUrl

const isModernServer = serverVersion.major >= 2 && serverVersion.minor >= 35

// From core version 2.35, short_name is used instead of the human-readable title to generate the url slug
const urlSafeAppSlug = (isModernServer ? config.name : config.title)
.replace(/[^A-Za-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
const urlSafeAppSlug = formatUrlSafeAppSlug(
isModernServer ? config.name : config.title
)

// From core version 2.35, core apps are hosted at the server root under the /dhis-web-* namespace
if (config.coreApp && isModernServer) {
Expand All @@ -25,3 +29,8 @@ module.exports.constructAppUrl = (baseUrl, config, serverVersion) => {
appUrl = scheme + appUrl.substr(scheme.length).replace(/\/+/g, '/')
return appUrl
}

module.exports = {
constructAppUrl,
formatUrlSafeAppSlug,
}
15 changes: 10 additions & 5 deletions cli/src/lib/formatAppAuthName.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { formatUrlSafeAppSlug } = require('./constructAppUrl')

const APP_AUTH_PREFIX = 'M_'
const DHIS_WEB = 'dhis-web-'

Expand All @@ -8,19 +10,22 @@ const DHIS_WEB = 'dhis-web-'
* Ex: coreApp && name = 'data-visualizer': authName = 'M_dhis-web-data-visualizer'
* Ex: name = 'pwa-example': authName = 'M_pwaexample'
* Ex: name = 'BNA Action Tracker': authName = 'M_BNA_Action_Tracker'
*
* The 'legacy' parameter specifies server version < 2.35 which uses
* config.title instead of config.name
*/
const formatAppAuthName = (config) => {
const formatAppAuthName = ({ config, legacy }) => {
const appName = legacy ? config.title : config.name

if (config.coreApp) {
// TODO: Verify this formatting - are there any transformations,
// or do we trust that it's lower-case and hyphenated?
return APP_AUTH_PREFIX + DHIS_WEB + config.name
return APP_AUTH_PREFIX + DHIS_WEB + formatUrlSafeAppSlug(appName)
}

// This formatting is drawn from https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java#L494-L499
// (replaceAll is only introduced in Node 15)
return (
APP_AUTH_PREFIX +
config.name
appName
.trim()
.replace(/[^a-zA-Z0-9\s]/g, '')
.replace(/\s/g, '_')
Expand Down
3 changes: 2 additions & 1 deletion cli/src/lib/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = ({ config, paths }) => {
const baseEnvVars = {
name: config.title,
version: config.version,
auth_name: formatAppAuthName(config),
auth_name: formatAppAuthName({ config }),
legacy_auth_name: formatAppAuthName({ config, legacy: true }),
}

return {
Expand Down