-
Notifications
You must be signed in to change notification settings - Fork 199
docs: looker oauth implementation example #995
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Draft
josephaxisa
wants to merge
1
commit into
main
Choose a base branch
from
jax/oauth-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,56 @@ | ||
{ | ||
"name": "looker-oauth", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"develop": "webpack serve --host=0.0.0.0 --https --disable-host-check --config webpack.dev.config.js" | ||
}, | ||
"dependencies": { | ||
"@looker/components": "^3.0.1", | ||
"@looker/extension-utils": "^0.1.4", | ||
"@looker/sdk": "^22.0.0", | ||
"@looker/sdk-rtl": "^21.3.2", | ||
"react": "^16.14.0", | ||
"react-dom": "^16.14.0", | ||
"react-is": "^16.13.1", | ||
"react-router-dom": "^5.1.2", | ||
"styled-components": "^5.2.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.13.16", | ||
"@babel/core": "^7.13.16", | ||
"@babel/plugin-proposal-class-properties": "^7.13.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.13.8", | ||
"@babel/plugin-transform-react-jsx": "^7.13.12", | ||
"@babel/plugin-transform-runtime": "^7.13.15", | ||
"@babel/preset-env": "^7.12.10", | ||
"@babel/preset-react": "^7.12.10", | ||
"@babel/preset-typescript": "^7.12.7", | ||
"@babel/runtime": "^7.12.5", | ||
"@looker/eslint-config": "^0.10.4", | ||
"@looker/prettier-config": "^0.10.4", | ||
"@types/react": "^16.14.2", | ||
"@types/react-dom": "^16.9.10", | ||
"@types/react-router": "^5.1.18", | ||
"@types/react-router-dom": "^5.1.5", | ||
"@types/styled-components": "5.1.5", | ||
"babel-loader": "^8.2.2", | ||
"babel-loader-exclude-node-modules-except": "^1.1.2", | ||
"babel-preset-nano-react-app": "^0.1.0", | ||
"eslint": "^7.19.0", | ||
"eslint-import-resolver-typescript": "^2.0.0", | ||
"eslint-import-resolver-webpack": "^0.12.1", | ||
"eslint-plugin-header": "^3.0.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-mdx": "^1.6.8", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"prettier": "^2.1.1", | ||
"typescript": "4.1.2", | ||
"webpack": "^5.10.0", | ||
"webpack-bundle-analyzer": "^4.2.0", | ||
"webpack-cli": "^4.6.0", | ||
"webpack-dev-server": "^3.11.2", | ||
"webpack-merge": "^5.8.0" | ||
} | ||
} |
This file contains hidden or 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,10 @@ | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Looker OAuth Example</title> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script src="/dist/bundle.js"></script> | ||
</body> | ||
</html> |
This file contains hidden or 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,80 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import React, { useEffect } from 'react' | ||
import { | ||
BrowserAdaptor, | ||
OAuthScene, | ||
registerEnvAdaptor, | ||
} from '@looker/extension-utils' | ||
import type { OAuthConfigProvider } from '@looker/extension-utils' | ||
import { Flex, FlexItem } from '@looker/components' | ||
import { Route, Switch } from 'react-router' | ||
|
||
import { ConfigForm } from './ConfigForm' | ||
import { initSdk } from './utils' | ||
import { HomeScene } from './HomeScene' | ||
import { Loader } from './Loader' | ||
|
||
const App: React.FC = () => { | ||
const adaptor = new BrowserAdaptor(initSdk()) | ||
registerEnvAdaptor(adaptor) | ||
const oauthReturn = window.location.pathname === '/oauth' | ||
const authIsConfigured = ( | ||
adaptor.sdk.authSession.settings as OAuthConfigProvider | ||
).authIsConfigured() | ||
const canLogin = | ||
authIsConfigured && | ||
!adaptor.sdk.authSession.isAuthenticated() && | ||
!oauthReturn | ||
|
||
useEffect(() => { | ||
const login = async () => await adaptor.login() | ||
if (canLogin) { | ||
login() | ||
} | ||
}, []) | ||
|
||
return ( | ||
<Flex flexDirection="column" justifyContent="center" mt="25%"> | ||
<FlexItem alignSelf="center"> | ||
<Switch> | ||
<Route path="/oauth"> | ||
<OAuthScene adaptor={adaptor} /> | ||
</Route> | ||
<Route path="/" exact> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if path not '/' and not '/oauth'? Can we remove the path and exact? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is something I did not think of. I will eliminate the "/" path so that it can handle anything. |
||
{canLogin ? ( | ||
<Loader message={`Configuration found. Logging in`} /> | ||
) : ( | ||
<> {authIsConfigured ? <HomeScene /> : <ConfigForm />} </> | ||
)} | ||
</Route> | ||
</Switch> | ||
</FlexItem> | ||
</Flex> | ||
) | ||
} | ||
|
||
export default App |
This file contains hidden or 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,75 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import type { BaseSyntheticEvent } from 'react' | ||
import React, { useState } from 'react' | ||
import { Form, FieldText, Button, Space } from '@looker/components' | ||
import { getEnvAdaptor } from '@looker/extension-utils' | ||
import { ConfigKey } from './utils' | ||
|
||
export const ConfigForm: React.FC = () => { | ||
const [serverUrl, setServerUrl] = useState('') | ||
const [canLogin, setCanLogin] = useState(false) | ||
|
||
const handleChange = (e: BaseSyntheticEvent) => { | ||
setServerUrl(e.target.value) | ||
} | ||
|
||
const handleSave = (e: BaseSyntheticEvent) => { | ||
e.preventDefault() | ||
const config = { | ||
base_url: `${serverUrl}:19999`, | ||
looker_url: `${serverUrl}:9999`, | ||
} | ||
localStorage.setItem(ConfigKey, JSON.stringify(config)) | ||
setCanLogin(true) | ||
} | ||
|
||
const handleLogin = (e: BaseSyntheticEvent) => { | ||
e.preventDefault() | ||
const adaptor = getEnvAdaptor() | ||
adaptor.login() | ||
} | ||
|
||
return ( | ||
<Form> | ||
<FieldText | ||
required | ||
label="Looker server URL" | ||
description="typically https://myserver.looker.com" | ||
value={serverUrl} | ||
onChange={handleChange} | ||
/> | ||
<Space> | ||
<Button onClick={handleSave} mr="small"> | ||
Save | ||
</Button> | ||
<Button onClick={handleLogin} mr="small" disabled={!canLogin}> | ||
josephaxisa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Login | ||
</Button> | ||
</Space> | ||
</Form> | ||
) | ||
} |
This file contains hidden or 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,49 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import React, { useState, useEffect } from 'react' | ||
import { getEnvAdaptor } from '@looker/extension-utils' | ||
import { Heading } from '@looker/components' | ||
import type { ILooker40SDK } from '@looker/sdk' | ||
import { me } from '@looker/sdk' | ||
|
||
export const HomeScene = () => { | ||
const adaptor = getEnvAdaptor() | ||
const [name, setName] = useState<string | undefined>() | ||
|
||
const getName = async () => { | ||
const sdk = adaptor.sdk as ILooker40SDK | ||
const res = await sdk.ok(me(sdk)) | ||
josephaxisa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (res) { | ||
setName(res.first_name!) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
getName() | ||
}, []) | ||
|
||
return <>{name && <Heading>{`Welcome, ${name}`}</Heading>}</> | ||
} |
This file contains hidden or 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,45 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import type { FC } from 'react' | ||
import React from 'react' | ||
import { Flex, FlexItem, Heading, ProgressCircular } from '@looker/components' | ||
|
||
export interface LoaderProps { | ||
message: string | ||
} | ||
|
||
export const Loader: FC<LoaderProps> = ({ message }) => ( | ||
<Flex flexDirection="column" justifyContent="center" mt="25%"> | ||
<FlexItem alignSelf="center"> | ||
<ProgressCircular size="large" /> | ||
</FlexItem> | ||
<FlexItem mt="large" alignSelf="center"> | ||
<Heading color="key" as="h2"> | ||
{message} | ||
</Heading> | ||
</FlexItem> | ||
</Flex> | ||
) |
This file contains hidden or 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 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import ReactDOM from 'react-dom' | ||
import React from 'react' | ||
import { ComponentsProvider } from '@looker/components' | ||
import { getThemeOverrides } from '@looker/extension-utils' | ||
import { BrowserRouter } from 'react-router-dom' | ||
josephaxisa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import App from './App' | ||
|
||
const themeOverrides = getThemeOverrides(false) | ||
|
||
ReactDOM.render( | ||
<ComponentsProvider | ||
loadGoogleFonts={themeOverrides.loadGoogleFonts} | ||
themeCustomizations={themeOverrides.themeCustomizations} | ||
> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</ComponentsProvider>, | ||
document.getElementById('container') | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern about the examples is the difficulty we have keeping the versions up to date. We have an issue with the access token server which I'm thinking should be moved to packages and eventually deleted when artifacts is done.
I don't know of a better place to put the example. I've been thinking of writing a script for examples that allows me to update versions easily. May be the script should go here and it can be run after each publish and eventually automated? Perhaps a script to ensure that examples have been updated to the current version when we do a publish. Maybe we can have a CI job update the packages post publish.
Thinking out loud here. Totally unrelated to the merits of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you've created this script now, @bryans99?