-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from osstotalsoft/axa-fr-update
`@axa-fr/react-oidc` update to v6.x
- Loading branch information
Showing
16 changed files
with
246 additions
and
172 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"javascript.format.enable": true, | ||
"typescript.format.enable": false, | ||
"cSpell.words": [ | ||
"Oidc", | ||
"totalsoft" | ||
], | ||
} |
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
46 changes: 46 additions & 0 deletions
46
generators/app/templates/infrastructure/public/OidcTrustedDomains.js
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,46 @@ | ||
// Copyright 2023 cdiaconita | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Add below trusted domains, access tokens will automatically injected to be send to | ||
// trusted domain can also be a path like https://www.myapi.com/users, | ||
// then all subroute like https://www.myapi.com/useers/1 will be authorized to send access_token to. | ||
|
||
// Service worker will continue to give access token to the JavaScript client | ||
// Ideal to hide refresh token from client JavaScript, but to retrieve access_token for some | ||
// scenarios which require it. For example, to send it via websocket connection. | ||
|
||
const REACT_APP_URL = `${oidc.REACT_APP_GQL_HTTP_PROTOCOL}://${oidc.REACT_APP_GQL}/graphql` | ||
|
||
// Domains used by OIDC server must be also declared here | ||
const defaultTrustedDomains = { | ||
config_show_access_token: { | ||
domains: [oidc.REACT_APP_IDENTITY_AUTHORITY, REACT_APP_URL], | ||
showAccessToken: true | ||
} | ||
} | ||
|
||
const handler = { | ||
get(target, name) { | ||
if (name in target) { | ||
return target[name] | ||
} | ||
return { | ||
domains: [oidc.REACT_APP_IDENTITY_AUTHORITY, REACT_APP_URL], | ||
showAccessToken: true | ||
} | ||
} | ||
} | ||
|
||
const trustedDomains = new Proxy(defaultTrustedDomains, handler) | ||
|
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,51 @@ | ||
// Copyright 2023 cdiaconita | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
const fs = require('fs') | ||
const dotenv = require('dotenv') | ||
|
||
const injectedObjectRegEx = /(const oidc = {[^}]*})/ | ||
|
||
fs.readFile('public/OidcTrustedDomains.js', 'utf8', function (err, data) { | ||
if (err) { | ||
console.error(err) | ||
return | ||
} | ||
const result = dotenv.config() | ||
if (result.error) { | ||
const path = `.env` | ||
dotenv.config({ path }) | ||
} | ||
|
||
const { REACT_APP_IDENTITY_AUTHORITY, REACT_APP_GQL_HTTP_PROTOCOL, REACT_APP_GQL } = process.env | ||
|
||
const injectedValue = | ||
'const oidc = ' + | ||
JSON.stringify({ | ||
REACT_APP_IDENTITY_AUTHORITY, | ||
REACT_APP_GQL_HTTP_PROTOCOL, | ||
REACT_APP_GQL | ||
}) | ||
|
||
if (data.match(injectedObjectRegEx)) { | ||
data = data.replace(injectedObjectRegEx, injectedValue) | ||
} else { | ||
data = injectedValue + data | ||
} | ||
|
||
fs.writeFile('public/OidcTrustedDomains.js', data, 'utf8', function (err) { | ||
if (err) throw err | ||
|
||
console.log('The file was saved!') | ||
}) | ||
}) |
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
33 changes: 18 additions & 15 deletions
33
generators/app/templates/infrastructure/src/apollo/AuthApolloProvider.js
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 |
---|---|---|
@@ -1,25 +1,28 @@ | ||
import { ApolloProvider } from '@apollo/client'; | ||
import React, { useContext } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { AuthenticationContext } from '@axa-fr/react-oidc-context'; | ||
import React, { useEffect } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { ApolloProvider } from '@apollo/client' | ||
import { useOidcUser, OidcUserStatus, useOidcAccessToken } from '@axa-fr/react-oidc' | ||
|
||
import { getApolloClient } from './client'; | ||
import { getApolloClient, getAccessToken } from './client' | ||
import { getOidcConfigName } from "utils/functions" | ||
|
||
export function AuthApolloProvider({ children }) { | ||
const oidc = useContext(AuthenticationContext); | ||
const { oidcUserLoadingState } = useOidcUser(getOidcConfigName()) | ||
const { accessToken } = useOidcAccessToken(getOidcConfigName()) | ||
|
||
if (oidc.isLoading) { | ||
return (<>auth loading</>) | ||
} | ||
useEffect(() => { | ||
setAccessToken(accessToken) | ||
}, [accessToken]) | ||
|
||
return ( | ||
<ApolloProvider client={getApolloClient()}> | ||
{children} | ||
</ApolloProvider>) | ||
if (oidcUserLoadingState === OidcUserStatus.Loading) { | ||
return <>auth loading</> | ||
} | ||
|
||
return <ApolloProvider client={getApolloClient()}>{children}</ApolloProvider> | ||
} | ||
|
||
AuthApolloProvider.propTypes = { | ||
children: PropTypes.element.isRequired | ||
children: PropTypes.element.isRequired | ||
} | ||
|
||
export default AuthApolloProvider; | ||
export default AuthApolloProvider |
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
10 changes: 6 additions & 4 deletions
10
generators/app/templates/infrastructure/src/components/routing/CustomRoute.js
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.