-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add CAM token SSO support * document new auth env vars * fix lint errors * add lint to CI * Refactor auth into adapters * fix lint errors * simplify CAM validation logic * add support for referrer based login UI redirection * rename DefaultAuthAdapter to FakeAuthAdapter, run prettier * convert sso token env var to string array * remove FakeAuthAdapter * update NoAuthAdapter to use new UI flow * add more secure AUTH_TYPE default case * update auth env var docs * remove auth type check in `session` handler * Throw unsupported configuration error in `NoAuthAdapter`
- Loading branch information
Showing
11 changed files
with
271 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: lint | ||
|
||
on: | ||
push: | ||
branches: | ||
- develop | ||
pull_request: | ||
branches: | ||
- develop | ||
|
||
jobs: | ||
list: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '16.13.0' | ||
- name: Install Dev Dependencies and Build | ||
run: | | ||
npm install | ||
npm run build | ||
- name: Lint | ||
run: | | ||
npm run lint |
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
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,111 @@ | ||
import { getEnv } from '../../../env.js'; | ||
import { generateJwt, getUserRoles } from '../functions.js'; | ||
import fetch from 'node-fetch'; | ||
import type { AuthAdapter, AuthResponse, ValidateResponse } from '../types.js'; | ||
|
||
import { Request } from 'express'; | ||
|
||
type CAMValidateResponse = { | ||
validated?: boolean; | ||
errorCode?: string; | ||
errorMessage?: string; | ||
}; | ||
|
||
type CAMInvalidateResponse = { | ||
invalidated?: boolean; | ||
errorCode?: string; | ||
errorMessage?: string; | ||
}; | ||
|
||
type CAMLoginResponse = { | ||
userId?: string; | ||
errorCode?: string; | ||
errorMessage?: string; | ||
}; | ||
|
||
export const CAMAuthAdapter: AuthAdapter = { | ||
logout: async (req: Request): Promise<boolean> => { | ||
const { AUTH_SSO_TOKEN_NAME, AUTH_URL } = getEnv(); | ||
|
||
const cookies = req.cookies; | ||
const ssoToken = cookies[AUTH_SSO_TOKEN_NAME[0]]; | ||
|
||
const body = JSON.stringify({ ssoToken }); | ||
const url = `${AUTH_URL}/ssoToken?action=invalidate`; | ||
const response = await fetch(url, { body, method: 'DELETE' }); | ||
const { invalidated = false } = (await response.json()) as CAMInvalidateResponse; | ||
|
||
return invalidated; | ||
}, | ||
|
||
validate: async (req: Request): Promise<ValidateResponse> => { | ||
const { AUTH_SSO_TOKEN_NAME, AUTH_URL, AUTH_UI_URL } = getEnv(); | ||
|
||
const cookies = req.cookies; | ||
const ssoToken = cookies[AUTH_SSO_TOKEN_NAME[0]]; | ||
|
||
const body = JSON.stringify({ ssoToken }); | ||
const url = `${AUTH_URL}/ssoToken?action=validate`; | ||
const response = await fetch(url, { body, method: 'POST' }); | ||
const json = (await response.json()) as CAMValidateResponse; | ||
|
||
const { validated = false, errorCode = false } = json; | ||
|
||
const redirectTo = req.headers.referrer; | ||
|
||
const redirectURL = `${AUTH_UI_URL}/?goto=${redirectTo}`; | ||
|
||
if (errorCode || !validated) { | ||
return { | ||
message: 'invalid token, redirecting to login UI', | ||
redirectURL, | ||
success: false, | ||
}; | ||
} | ||
|
||
const loginResp = await loginSSO(ssoToken); | ||
|
||
return { | ||
message: 'valid SSO token', | ||
redirectURL: '', | ||
success: validated, | ||
token: loginResp.token ?? undefined, | ||
userId: loginResp.message, | ||
}; | ||
}, | ||
}; | ||
|
||
async function loginSSO(ssoToken: any): Promise<AuthResponse> { | ||
const { AUTH_URL, DEFAULT_ROLE, ALLOWED_ROLES } = getEnv(); | ||
|
||
try { | ||
const body = JSON.stringify({ ssoToken }); | ||
const url = `${AUTH_URL}/userProfile`; | ||
const response = await fetch(url, { body, method: 'POST' }); | ||
const json = (await response.json()) as CAMLoginResponse; | ||
const { userId = '', errorCode = false } = json; | ||
|
||
if (errorCode) { | ||
const { errorMessage } = json; | ||
return { | ||
message: errorMessage ?? 'error logging into CAM', | ||
success: false, | ||
token: null, | ||
}; | ||
} | ||
|
||
const { allowed_roles, default_role } = await getUserRoles(userId, DEFAULT_ROLE, ALLOWED_ROLES); | ||
|
||
return { | ||
message: userId, | ||
success: true, | ||
token: generateJwt(userId, default_role, allowed_roles), | ||
}; | ||
} catch (error) { | ||
return { | ||
message: 'An unexpected error occurred', | ||
success: false, | ||
token: null, | ||
}; | ||
} | ||
} |
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,11 @@ | ||
import type { AuthAdapter, ValidateResponse } from '../types.js'; | ||
|
||
export const NoAuthAdapter: AuthAdapter = { | ||
logout: async (): Promise<boolean> => true, | ||
validate: async (): Promise<ValidateResponse> => { | ||
throw new Error(` | ||
The UI is configured to use SSO auth, but the Gateway has AUTH_TYPE=none set, which is not a supported configuration. | ||
Disable SSO auth on the UI if JWT-only auth is desired. | ||
`); | ||
}, | ||
}; |
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
Oops, something went wrong.