diff --git a/.env b/.env index e3e0f1b..23f8d03 100644 --- a/.env +++ b/.env @@ -2,3 +2,4 @@ VITE_API_ENDPOINT=http://localhost:8000 VITE_AUTH_TYPE=anonymous VITE_OIDC_CLIENT_ID= VITE_OIDC_ISSUER=https://localhost:8000 +VITE_IDENTITY_PROVIDER= diff --git a/.github/workflows/ci-develop.yml b/.github/workflows/ci-develop.yml new file mode 100644 index 0000000..c10a4df --- /dev/null +++ b/.github/workflows/ci-develop.yml @@ -0,0 +1,112 @@ +name: Main Branch CI +on: + push: + branches: + - develop + pull_request: + branches: + - develop + +jobs: + test_lint: + runs-on: ubuntu-latest + if: ${{ !github.event.created && github.repository != 'garronej/ts-ci' }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + - uses: bahmutov/npm-install@v1 + - name: If this step fails run 'yarn lint' and 'yarn format' then commit again. + run: | + yarn lint:check + yarn format:check + test: + runs-on: ${{ matrix.os }} + needs: test_lint + strategy: + matrix: + node: ["16", "18"] + os: [ubuntu-latest] + name: Test with Node v${{ matrix.node }} on ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + - uses: bahmutov/npm-install@v1 + - run: yarn build + - run: echo "you should replace this line by yarn test (with green test)" + + check_if_version_upgraded: + name: Check if version upgrade + # When someone forks the repo and opens a PR we want to enables the tests to be run (the previous jobs) + # but obviously only us should be allowed to release. + # In the following check we make sure that we own the branch this CI workflow is running on before continuing. + if: | + github.event_name == 'push' || + github.event.pull_request.head.repo.owner.login == github.event.pull_request.base.repo.owner.login + runs-on: ubuntu-latest + needs: test + outputs: + from_version: ${{ steps.step1.outputs.from_version }} + to_version: ${{ steps.step1.outputs.to_version }} + is_upgraded_version: ${{ steps.step1.outputs.is_upgraded_version }} + is_pre_release: ${{steps.step1.outputs.is_pre_release }} + steps: + - uses: garronej/ts-ci@v2.1.0 + id: step1 + with: + action_name: is_package_json_version_upgraded + branch: ${{ github.head_ref || github.ref }} + + create_github_release: + runs-on: ubuntu-latest + # We create release only if the version in the package.json have been upgraded and this CI is running against the main branch. + # We allow branches with a PR open on main to publish pre-release (x.y.z-rc.u) but not actual releases. + if: | + needs.check_if_version_upgraded.outputs.is_upgraded_version == 'true' && + ( + github.event_name == 'push' || + needs.check_if_version_upgraded.outputs.is_pre_release == 'true' + ) + needs: + - check_if_version_upgraded + steps: + - uses: softprops/action-gh-release@v1 + with: + name: Release Candidate v${{ needs.check_if_version_upgraded.outputs.to_version }}-rc + tag_name: v${{ needs.check_if_version_upgraded.outputs.to_version }}-rc + target_commitish: ${{ github.head_ref || github.ref }} + generate_release_notes: true + draft: false + prerelease: ${{ needs.check_if_version_upgraded.outputs.is_pre_release == 'true' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + docker: + needs: check_if_version_upgraded + if: | + needs.check_if_version_upgraded.outputs.is_upgraded_version == 'true' && + ( + github.event_name == 'push' || + needs.check_if_version_upgraded.outputs.is_pre_release == 'true' + ) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.ref }} + - uses: actions/setup-node@v3 + - uses: docker/setup-qemu-action@v2 + - uses: docker/setup-buildx-action@v2 + - uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - uses: docker/build-push-action@v4 + with: + platforms: linux/amd64,linux/arm64 + context: . + push: true + tags: | + inseefr/platine-management:${{ needs.check_if_version_upgraded.outputs.to_version }}-rc + inseefr/platine-management:latest diff --git a/.github/workflows/ci-others.yml b/.github/workflows/ci-others.yml index beeaf22..87e7b9c 100644 --- a/.github/workflows/ci-others.yml +++ b/.github/workflows/ci-others.yml @@ -3,9 +3,11 @@ on: push: branches-ignore: - main + - develop pull_request: branches-ignore: - main + - develop jobs: test_lint: diff --git a/.github/workflows/sonar.yaml b/.github/workflows/sonar.yaml index 0ae3a0f..43b339a 100644 --- a/.github/workflows/sonar.yaml +++ b/.github/workflows/sonar.yaml @@ -2,7 +2,8 @@ name: Sonar analysis on: push: branches: - - "**" + - main + - develop pull_request: types: [opened, synchronize, reopened] jobs: @@ -17,4 +18,4 @@ jobs: uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/package.json b/package.json index 45238f6..dedb4ab 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "platine-management", "private": true, - "version": "1.0.0", + "version": "1.0.1", "type": "module", "scripts": { "dev": "vite", diff --git a/src/functions/oidc.ts b/src/functions/oidc.ts index d1f6982..0ff8112 100644 --- a/src/functions/oidc.ts +++ b/src/functions/oidc.ts @@ -2,12 +2,12 @@ import { createReactOidc } from "oidc-spa/react"; import { Fragment } from "react"; type TokenInfo = { - inseegroupdefault: string[]; + inseegroupedefaut: string[]; preferred_username: string; }; const guestUser: TokenInfo = { - inseegroupdefault: [], + inseegroupedefaut: [], preferred_username: "Guest", }; @@ -19,6 +19,7 @@ export const createAppOidc = () => { issuerUri: import.meta.env.VITE_OIDC_ISSUER, clientId: import.meta.env.VITE_OIDC_CLIENT_ID, publicUrl: "/", + extraQueryParams: { kc_idp_hint: import.meta.env.VITE_IDENTITY_PROVIDER }, }); } @@ -31,6 +32,7 @@ export const createAppOidc = () => { decodedIdToken: guestUser, accessToken: "accessToken", }, + logout: () => (window.location.href = "/"), }), }; }; diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index 2af5be8..137e18d 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -5,7 +5,7 @@ const { OidcProvider, useOidc } = await createAppOidc(); export const useHasRole = (role: string): boolean => { const { oidcTokens } = useOidc({ assertUserLoggedIn: true }); - return oidcTokens.decodedIdToken.inseegroupdefault.includes(role); + return oidcTokens.decodedIdToken.inseegroupedefaut.includes(role); }; export const useAccessToken = (): string => { @@ -16,6 +16,10 @@ export const useUser = () => { return useOidc({ assertUserLoggedIn: true }).oidcTokens.decodedIdToken; }; +export const useLogout = () => { + return useOidc({ assertUserLoggedIn: true }).logout; +}; + export function useIsAuthenticated(): boolean { const { login, isUserLoggedIn } = useOidc({ assertUserLoggedIn: false }); @@ -25,7 +29,6 @@ export function useIsAuthenticated(): boolean { } login({ doesCurrentHrefRequiresAuth: false, - extraQueryParams: { kc_idp_hint: "insee-ssp" }, }); }, [login]); diff --git a/src/ui/Header.tsx b/src/ui/Header.tsx index d6690e5..709ce1d 100644 --- a/src/ui/Header.tsx +++ b/src/ui/Header.tsx @@ -1,12 +1,15 @@ import { Box, IconButton, Link } from "@mui/material"; import SettingsOutlinedIcon from "@mui/icons-material/SettingsOutlined"; +import ExitToAppIcon from "@mui/icons-material/ExitToApp"; import { Link as RouterLink } from "react-router-dom"; import { Row } from "./Row.tsx"; import { PropsWithChildren } from "react"; -import { useUser } from "../hooks/useAuth.ts"; +import { useUser, useLogout } from "../hooks/useAuth.ts"; export function Header() { const { preferred_username } = useUser(); + const logout = useLogout(); + return ( @@ -23,6 +26,16 @@ export function Header() { + + logout({ + redirectTo: "specific url", + url: "", + }) + } + > + + ); diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index c3848ce..59a0dda 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -4,6 +4,7 @@ interface ImportMetaEnv { readonly VITE_AUTH_TYPE: string; readonly VITE_OIDC_CLIENT_ID: string; readonly VITE_OIDC_ISSUER: string; + readonly VITE_IDENTITY_PROVIDER: string; } interface ImportMeta {