-
Notifications
You must be signed in to change notification settings - Fork 195
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
docs: looker oauth implementation example #995
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
} | ||
} |
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> |
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 |
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> | ||
) | ||
} |
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>}</> | ||
} |
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> | ||
) |
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') | ||
) |
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?