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

Adapt workflows - Add id provider to auth - Add logout #70

Merged
merged 8 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
112 changes: 112 additions & 0 deletions .github/workflows/ci-develop.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
2 changes: 2 additions & 0 deletions .github/workflows/ci-others.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ on:
push:
branches-ignore:
- main
- develop
pull_request:
branches-ignore:
- main
- develop

jobs:
test_lint:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/sonar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Sonar analysis
on:
push:
branches:
- "**"
- main
- develop
pull_request:
types: [opened, synchronize, reopened]
jobs:
Expand All @@ -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 }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "platine-management",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
6 changes: 4 additions & 2 deletions src/functions/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};

Expand All @@ -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 },
});
}

Expand All @@ -31,6 +32,7 @@ export const createAppOidc = () => {
decodedIdToken: guestUser,
accessToken: "accessToken",
},
logout: () => (window.location.href = "/"),
}),
};
};
7 changes: 5 additions & 2 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il manque pas un d et un l là ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non j'ai rectifié le nom aux endroits ou cela apparait dans l'appli, le token porte bien un vecteur "inseegroupedefaut" (écriture française)

};

export const useAccessToken = (): string => {
Expand All @@ -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 });

Expand All @@ -25,7 +29,6 @@ export function useIsAuthenticated(): boolean {
}
login({
doesCurrentHrefRequiresAuth: false,
extraQueryParams: { kc_idp_hint: "insee-ssp" },
});
}, [login]);

Expand Down
15 changes: 14 additions & 1 deletion src/ui/Header.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Row px={4} py={1} height={74} justifyContent="space-between" bgcolor="white">
<Row gap={2.5} component={HomeLink}>
Expand All @@ -23,6 +26,16 @@ export function Header() {
<IconButton component={RouterLink} to="/reglages">
<SettingsOutlinedIcon />
</IconButton>
<IconButton
onClick={() =>
logout({
redirectTo: "specific url",
url: "",
})
}
>
<ExitToAppIcon />
</IconButton>
</Box>
</Row>
);
Expand Down
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading