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

allow for non-JWT access tokens #6

Merged
merged 6 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add example for Microsoft Azure AD
  • Loading branch information
soofstad committed Jul 12, 2022
commit d1bca42eccd66791a64173716027846275cf72c6
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
node_modules
.venv
/src/dist/
.idea
log.txt
/yarn.lock
/src/react-app-env.d.ts
/src/yarn.lock
.env
__pycache__
__pycache__
/examples/microsoft-auth-provider/web-app/yarn.lock
10 changes: 10 additions & 0 deletions examples/microsoft-auth-provider/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.8"

services:
web:
build: ./web-app
restart: unless-stopped
volumes:
- ./web-app/src:/app/src
ports:
- "3000:3000"
7 changes: 7 additions & 0 deletions examples/microsoft-auth-provider/web-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:16-alpine as base
WORKDIR /app
ADD package.json ./
RUN yarn install
ADD ./public ./public
ADD ./src ./src
CMD ["yarn", "start"]
30 changes: 30 additions & 0 deletions examples/microsoft-auth-provider/web-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react-example-app",
"version": "0.1.0",
"dependencies": {
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-oauth2-code-pkce": "^1.3.0-alpha.1",
"react-scripts": "^5.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build"
},
"devDependencies": {
"@types/react": "^18.0.9",
"typescript": "^4.4.3"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
41 changes: 41 additions & 0 deletions examples/microsoft-auth-provider/web-app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
84 changes: 84 additions & 0 deletions examples/microsoft-auth-provider/web-app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React, { useContext } from 'react'
// @ts-ignore
import ReactDOM from 'react-dom'
import { AuthContext, AuthProvider, TAuthConfig, IAuthContext } from "react-oauth2-code-pkce"

const authConfig: TAuthConfig = {
clientId: '6559ce69-219d-4e82-b6ed-889a861c7c94',
authorizationEndpoint:
'https://login.microsoftonline.com/d422398d-b6a5-454d-a202-7ed4c1bec457/oauth2/v2.0/authorize',
tokenEndpoint: 'https://login.microsoftonline.com/d422398d-b6a5-454d-a202-7ed4c1bec457/oauth2/v2.0/token',
redirectUri: 'http://localhost:3000/',
// Example to redirect back to original path after login has completed
preLogin: () => localStorage.setItem('preLoginPath', window.location.pathname),
postLogin: () => window.location.replace(localStorage.getItem('preLoginPath') || ''),
decodeToken: true,
scope: 'User.read'
}

function LoginInfo(): JSX.Element {
const { tokenData, token, logOut, idToken, error }: IAuthContext = useContext(AuthContext)

if (error){
return <>
<div style={{color: "red"}}>An error occurred during authentication: {error}</div>
<button onClick={()=>logOut()}>Logout</button>
</>

}

return (
<>
{token ?
<>
<div>
<h4>Access Token (JWT)</h4>
<pre style={{
width: '400px',
margin: "10px",
padding: "5px",
border: "black 2px solid",
wordBreak: 'break-all',
whiteSpace: 'break-spaces',
}}>
{token}</pre>
</div>
<div>
<h4>Login Information from Access Token (Base64 decoded JWT)</h4>
<pre style={{
width: '400px',
margin: "10px",
padding: "5px",
border: "black 2px solid",
wordBreak: 'break-all',
whiteSpace: 'break-spaces',
}}>
{JSON.stringify(tokenData, null, 2)}</pre>
</div>
<button onClick={()=>logOut()}>Logout</button>
</> :
<div>You are not logged in. Refresh page to login.</div>
}
</>
)

}


ReactDOM.render(
<div>
<div>
<h1>Demo using the 'react-oauth2-code-pkce' package</h1>
<p>Github: <a
href="https://github.com/soofstad/react-oauth2-pkce">https://github.com/soofstad/react-oauth2-pkce</a>
</p>
<p>NPM: <a
href="https://www.npmjs.com/package/react-oauth2-code-pkce">https://www.npmjs.com/package/react-oauth2-code-pkce</a>
</p>
</div>
<AuthProvider authConfig={authConfig}>
{/* @ts-ignore*/}
<LoginInfo/>
</AuthProvider>
</div>, document.getElementById('root'),
)
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { createRoot } from 'react-dom/client'
import { AuthContext, AuthProvider } from './AuthContext'

const authConfig = {
clientId: 'c43524cc7d3c82b05a47',
authorizationEndpoint: 'https://github.com/login/oauth/authorize',
tokenEndpoint: 'http://localhost:5000/api/token',
clientId: '6559ce69-219d-4e82-b6ed-889a861c7c94',
authorizationEndpoint:
'https://login.microsoftonline.com/d422398d-b6a5-454d-a202-7ed4c1bec457/oauth2/v2.0/authorize',
tokenEndpoint: 'https://login.microsoftonline.com/d422398d-b6a5-454d-a202-7ed4c1bec457/oauth2/v2.0/token',
redirectUri: 'http://localhost:3000/',
// Example to redirect back to original path after login has completed
preLogin: () => localStorage.setItem('preLoginPath', window.location.pathname),
postLogin: () => window.location.replace(localStorage.getItem('preLoginPath') || ''),
decodeToken: false,
decodeToken: true,
scope: 'User.read',
}

function LoginInfo() {
Expand Down