Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { useEffect } from 'react'
import { useRouter } from 'next/router'
import LoadingSpinner from '../../components/LoadingSpinner'
import MetaData from '../../components/MetaData'
import { getOpenIdClientService } from '../api/openid-client-service'
import { GetServerSidePropsContext } from 'next'
import { AuthIsDisabled, ValidateSession } from '../../lib/auth'
import { generators } from 'openid-client'
import React from 'react'
import { CircuitBreaker } from '../../lib/circuit-breaker'
import moize from 'moize'
import { getLogger } from '../../logging/log-util'
const log = getLogger('auth.login')
import {
deleteAllCookiesWithPrefix,
extendExpiryTime,
addCookie,
} from '../../lib/cookie-utils'
interface MetaDataProps {
data_en: {
title: string
desc: string
author: string
keywords: string
service: string
creator: string
accessRights: string
}
data_fr: {
title: string
desc: string
author: string
keywords: string
service: string
creator: string
accessRights: string
}
}
interface LoginProps {
locale?: string | undefined
meta: MetaDataProps
authDisabled: boolean
authorizationUrl: string
}
export default function Login(props: LoginProps) {
const router = useRouter()
useEffect(() => {
if (!router.isReady) return
Iif (!router.query.error) {
//If auth is disabled, redirect to dashboard without triggering signIn event, for testing purposes only
Iif (props.authDisabled) {
setTimeout(() => {
props.locale === 'en'
? router.push('/en/my-dashboard')
: router.push('/fr/mon-tableau-de-bord')
}, 3000)
return
}
router.push(props.authorizationUrl)
}
}, [
router.isReady,
props.authDisabled,
router,
props.locale,
props.authorizationUrl,
])
return (
<div role="main">
<MetaData language="en" data={props.meta}></MetaData>
<h1
className="grid h-screen place-items-center"
data-cy="loading-spinner"
aria-live="polite"
aria-busy="true"
>
<LoadingSpinner text="Loading / Chargement en cours ..." />
</h1>
</div>
)
}
Login.getLayout = function PageLayout(page: JSX.Element) {
return <>{page}</>
}
export const actuallyGetServerSideProps = async function ({
locale,
req,
res,
}: {
locale: GetServerSidePropsContext['locale']
req: GetServerSidePropsContext['req']
res: GetServerSidePropsContext['res']
}) {
const authDisabled = AuthIsDisabled() ? true : false
let authorizationUrl = null
Iif (!authDisabled) {
const sessionValid = await ValidateSession(
req.cookies,
process.env.CLIENT_ID as string,
)
if (sessionValid) {
extendExpiryTime(
req,
res,
process.env.AUTH_COOKIE_PREFIX + 'sessionId',
Number(process.env.SESSION_MAX_AGE as string),
)
return {
redirect: {
destination:
locale === 'en' ? '/en/my-dashboard' : '/fr/mon-tableau-de-bord',
permanent: false,
},
}
} else {
deleteAllCookiesWithPrefix(
req,
res,
process.env.AUTH_COOKIE_PREFIX as string,
)
}
const openIdClientService = getOpenIdClientService()
const codeVerifier = generators.codeVerifier()
const codeChallenge = generators.codeChallenge(codeVerifier)
const scope = 'openid profile'
const state = generators.state()
const codeChallengeMethod = 'S256'
const nonce = generators.nonce()
addCookie(
res,
process.env.AUTH_COOKIE_PREFIX + 'codeVerifier',
codeVerifier,
Number(process.env.SESSION_MAX_AGE as string),
)
addCookie(
res,
process.env.AUTH_COOKIE_PREFIX + 'nonce',
nonce,
Number(process.env.SESSION_MAX_AGE as string),
)
addCookie(
res,
process.env.AUTH_COOKIE_PREFIX + 'state',
state,
Number(process.env.SESSION_MAX_AGE as string),
)
addCookie(
res,
'localeForOauthCallback',
locale as string,
Number(process.env.SESSION_MAX_AGE as string),
)
authorizationUrl = await (
await openIdClientService
).authorize(scope, codeChallenge, codeChallengeMethod, state, nonce)
}
/* Place-holder Meta Data Props */
const meta = {
data_en: {
title: 'Loading-Chargement en cours - Canada.ca',
desc: 'English',
author: 'Service Canada',
keywords: '',
service: 'ESDC-EDSC_MSCA-MSDC-SCH',
creator: 'Employment and Social Development Canada',
accessRights: '1',
},
data_fr: {
title: 'Loading-Chargement en cours - Canada.ca',
desc: 'Français',
author: 'Service Canada',
keywords: '',
service: 'ESDC-EDSC_MSCA-MSDC-SCH',
creator: 'Emploi et Développement social Canada',
accessRights: '1',
},
}
return {
props: {
locale: locale,
meta,
authDisabled: authDisabled,
authorizationUrl: authorizationUrl,
},
}
}
const circuitBreaker = moize(createLoginCircuitBreaker, {
onCacheAdd: () => log.info('creating login circuit breaker'),
})
function createLoginCircuitBreaker() {
return new CircuitBreaker({
maxFailures: Number(process.env.LOGIN_MAX_ATTEMPTS) || undefined,
openAttemptDelay: Number(process.env.LOGIN_OPEN_DELAY) || undefined,
closedAttemptDelay: Number(process.env.LOGIN_CLOSED_DELAY) || undefined,
})
}
export const getServerSideProps = async function ({
locale,
req,
res,
}: {
locale: GetServerSidePropsContext['locale']
req: GetServerSidePropsContext['req']
res: GetServerSidePropsContext['res']
}) {
return await circuitBreaker().wrappedCallback(() =>
actuallyGetServerSideProps({ locale, req, res }),
)
}
|