Skip to content

Commit

Permalink
feat: OAuthConfigProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
josephaxisa authored Nov 29, 2021
1 parent f6872a6 commit 209f399
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 473 deletions.
3 changes: 1 addition & 2 deletions packages/api-explorer/src/StandaloneApiExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type { FC } from 'react'
import React, { useEffect, useState } from 'react'
import {
RunItProvider,
defaultConfigurator,
loadSpecsFromVersions,
RunItConfigKey,
RunItNoConfig,
Expand Down Expand Up @@ -87,7 +86,7 @@ export const StandaloneApiExplorer: FC<StandaloneApiExplorerProps> = ({

return (
<Provider store={store}>
<RunItProvider configurator={defaultConfigurator} basePath="/api/4.0">
<RunItProvider basePath="/api/4.0">
<>
{specs ? (
<ApiExplorer
Expand Down
31 changes: 1 addition & 30 deletions packages/extension-api-explorer/src/ExtensionApiExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import type { FC } from 'react'
import React, { useContext, useEffect, useState } from 'react'
import type { IStorageValue, RunItConfigurator } from '@looker/run-it'
import { RunItProvider, runItNoSet, sdkSpecFetch } from '@looker/run-it'
import type { ExtensionContextData } from '@looker/extension-sdk-react'
import { ExtensionContext } from '@looker/extension-sdk-react'
Expand All @@ -37,34 +36,6 @@ import { getExtensionSDK } from '@looker/extension-sdk'
import { Provider } from 'react-redux'
import { ExtensionAdaptor } from '@looker/extension-utils'

class ExtensionConfigurator implements RunItConfigurator {
storage: Record<string, string> = {}
getStorage(key: string, defaultValue = ''): IStorageValue {
const value = this.storage[key]
if (value) {
return {
location: 'session',
value,
}
}
return {
location: 'session',
value: defaultValue,
}
}

setStorage(key: string, value: string): string {
this.storage[key] = value
return value
}

removeStorage(key: string) {
delete this.storage[key]
}
}

const configurator = new ExtensionConfigurator()

export const ExtensionApiExplorer: FC = () => {
const extensionContext = useContext<ExtensionContextData>(ExtensionContext)
const [specs, setSpecs] = useState<SpecList>()
Expand Down Expand Up @@ -94,7 +65,7 @@ export const ExtensionApiExplorer: FC = () => {

return (
<Provider store={store}>
<RunItProvider configurator={configurator} basePath="">
<RunItProvider basePath="">
<>
{specs ? (
<ApiExplorer
Expand Down
1 change: 1 addition & 0 deletions packages/extension-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@looker/components": "^2.8.1",
"@looker/extension-sdk": "^21.20.0",
"@looker/extension-sdk-react": "^21.20.0",
"@looker/sdk-rtl": "^21.2.0",
"react": "^16.13.1"
},
"devDependencies": {
Expand Down
111 changes: 111 additions & 0 deletions packages/extension-utils/src/authUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
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 { IApiSection, IApiSettings } from '@looker/sdk-rtl'
import { ApiSettings } from '@looker/sdk-rtl'

export type StorageLocation = 'session' | 'local'

/** Object returned from storage service */
export interface IStorageValue {
/** Location of the stored object */
location: StorageLocation
/** Stored string representation of the value (usually JSON) */
value: string
}

/**
* An OAuth Session configuration provider
*/
export class OAuthConfigProvider extends ApiSettings {
constructor(
settings: Partial<IApiSettings>,
private readonly configKey: string
) {
super(settings)
}

private getStorage(key: string, defaultValue = ''): IStorageValue {
let value = sessionStorage.getItem(key)
if (value) {
return {
location: 'session',
value,
}
}
value = localStorage.getItem(key)
if (value) {
return {
location: 'local',
value,
}
}
return {
location: 'session',
value: defaultValue,
}
}

getStoredConfig() {
const storage = this.getStorage(this.configKey)
let config = { base_url: '', looker_url: '' }
if (storage.value) {
config = JSON.parse(storage.value)
}
return config
}

authIsConfigured(): boolean {
const config = this.getStoredConfig()
return config.base_url !== '' && config.looker_url !== ''
}

readConfig(_section?: string): IApiSection {
// Read server url values from storage
let config = this.getStoredConfig()
if (!this.authIsConfigured()) {
// derive Looker server URL from base_url
const url = new URL(this.base_url)
const authServer = `${url.protocol}//${url.hostname}`
config = {
base_url: this.base_url,
looker_url: `${authServer}:9999`,
}
}

const { base_url, looker_url } = config
/* update base_url to the dynamically determined value for standard transport requests */
this.base_url = base_url
return {
...super.readConfig(_section),
...{
base_url,
looker_url,
client_id: 'looker.api-explorer',
redirect_uri: `${window.location.origin}/oauth`,
},
}
}
}
1 change: 1 addition & 0 deletions packages/extension-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
export * from './adaptorUtils'
export * from './browserAdaptor'
export * from './extensionAdaptor'
export * from './authUtils'
37 changes: 21 additions & 16 deletions packages/run-it/src/RunIt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { ApiModel, IMethod } from '@looker/sdk-codegen'

import { BrowserAdaptor } from '@looker/extension-utils'
import { BrowserAdaptor, OAuthConfigProvider } from '@looker/extension-utils'
import { RunIt } from './RunIt'
import { api, testTextResponse } from './test-data'
import { initRunItSdk, runItNoSet, RunItSettings } from './utils'
import { defaultConfigurator } from './components'
import { initRunItSdk, runItNoSet } from './utils'
import { RunItProvider } from './RunItProvider'

describe('RunIt', () => {
Expand All @@ -47,7 +46,7 @@ describe('RunIt', () => {
method: IMethod = api.methods.run_inline_query
) => {
renderWithTheme(
<RunItProvider configurator={defaultConfigurator} basePath="/api/4.0">
<RunItProvider basePath="/api/4.0">
<RunIt
adaptor={adaptor}
api={_api}
Expand All @@ -61,10 +60,12 @@ describe('RunIt', () => {
describe('configured and authenticated', () => {
beforeEach(() => {
jest.spyOn(sdk.authSession, 'isAuthenticated').mockReturnValue(true)
jest.spyOn(RunItSettings.prototype, 'getStoredConfig').mockReturnValue({
base_url: 'https://foo:19999',
looker_url: 'https://foo:9999',
})
jest
.spyOn(OAuthConfigProvider.prototype, 'getStoredConfig')
.mockReturnValue({
base_url: 'https://foo:19999',
looker_url: 'https://foo:9999',
})
})
afterEach(() => {
jest.clearAllMocks()
Expand Down Expand Up @@ -131,10 +132,12 @@ describe('RunIt', () => {
describe('not configured or authenticated', () => {
beforeEach(() => {
jest.spyOn(sdk.authSession, 'isAuthenticated').mockReturnValue(false)
jest.spyOn(RunItSettings.prototype, 'getStoredConfig').mockReturnValue({
base_url: '',
looker_url: '',
})
jest
.spyOn(OAuthConfigProvider.prototype, 'getStoredConfig')
.mockReturnValue({
base_url: '',
looker_url: '',
})
})

test('it has Configure button', () => {
Expand All @@ -154,10 +157,12 @@ describe('RunIt', () => {
describe('configured but not authenticated', () => {
beforeEach(() => {
jest.spyOn(sdk.authSession, 'isAuthenticated').mockReturnValue(false)
jest.spyOn(RunItSettings.prototype, 'getStoredConfig').mockReturnValue({
base_url: 'https://foo:19999',
looker_url: 'https://foo:9999',
})
jest
.spyOn(OAuthConfigProvider.prototype, 'getStoredConfig')
.mockReturnValue({
base_url: 'https://foo:19999',
looker_url: 'https://foo:9999',
})
})

test('it has Login button', () => {
Expand Down
Loading

0 comments on commit 209f399

Please sign in to comment.