Skip to content

Commit

Permalink
feat: #1013 update prod auth flow (#2472)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcvay authored Aug 25, 2020
1 parent 978039a commit e04da4e
Show file tree
Hide file tree
Showing 35 changed files with 166 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ exports[`admin-apps AdminApprovals should match a snapshot 1`] = `
validate={null}
>
<div
className="field"
className=""
>
<div
className="control"
Expand Down Expand Up @@ -562,7 +562,7 @@ exports[`admin-apps AdminApprovals should match a snapshot 1`] = `
validate={null}
>
<div
className="field"
className=""
>
<div
className="control"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ exports[`AdminBilling should match a snapshot 1`] = `
validate={null}
>
<div
className="field pb-2"
className=""
>
<div
className="control"
Expand Down Expand Up @@ -240,7 +240,7 @@ exports[`AdminBilling should match a snapshot 1`] = `
validate={null}
>
<div
className="field pb-2"
className=""
>
<div
className="control"
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-portal/src/services/approvals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const fetchApprovalsList = async (params: FetchApprovalsListParams): Prom
url: `${URLS.approvals}?${setQueryParams(params)}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand Down
18 changes: 9 additions & 9 deletions packages/admin-portal/src/services/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const fetchAppsList = async (params: FetchAppsListParams): Promise<PagedR
url: `${URLS.apps}?${setQueryParams(params)}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -67,7 +67,7 @@ export const fetchAppById = async (params: FetchAppByIdParams): Promise<AppDetai
url: `${URLS.apps}/${id}?${setQueryParams({ clientId })}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -83,7 +83,7 @@ export const deleteAppById = async (params: DeleteAppByIdParams) => {
url: `${URLS.apps}/${id}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'DELETE',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -99,7 +99,7 @@ export const featureAppById = async (params: FeatureAppByIdParams) => {
url: `${URLS.apps}/${id}/feature`,
api: window.reapit.config.marketplaceApiUrl,
method: 'PUT',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -115,7 +115,7 @@ export const unfeatureAppById = async (params: UnfeatureAppByIdParams) => {
url: `${URLS.apps}/${id}/feature`,
api: window.reapit.config.marketplaceApiUrl,
method: 'DELETE',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -131,7 +131,7 @@ export const fetchAppRevisionsById = async (params: FetchAppRevisionsByIdParams)
url: `${URLS.apps}/${id}/revisions/${revisionId}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -148,7 +148,7 @@ export const approveAppRevisionById = async (params: ApproveAppRevisionByIdParam
api: window.reapit.config.marketplaceApiUrl,
method: 'POST',
body: rest,
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -165,7 +165,7 @@ export const rejectAppRevisionById = async (params: RejectAppRevisionByIdParams)
api: window.reapit.config.marketplaceApiUrl,
method: 'POST',
body: rest,
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -179,7 +179,7 @@ export const fetchDesktopIntegrationTypes = async () => {
url: URLS.desktopIntegrationTypes,
method: 'GET',
api: window.reapit.config.marketplaceApiUrl,
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
}
2 changes: 1 addition & 1 deletion packages/admin-portal/src/services/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const fetchCustomersList = async (params: FetchCustomersListParams): Prom
url: `${URLS.customers}/?${setQueryParams(params)}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const fetchDesktopIntegrationTypesList = async (
url: `${URLS.desktopIntegrationTypes}?${setQueryParams(params)}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -64,7 +64,7 @@ export const createDesktopIntegrationTypes = async (params: CreateDesktopIntegra
api: window.reapit.config.marketplaceApiUrl,
method: 'POST',
body: params,
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -82,7 +82,7 @@ export const fetchDesktopIntegrationTypesById = async (
url: `${URLS.desktopIntegrationTypes}/${id}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -99,7 +99,7 @@ export const updateDesktopIntegrationTypesById = async (params: UpdateDesktopInt
api: window.reapit.config.marketplaceApiUrl,
method: 'PUT',
body: rest,
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/admin-portal/src/services/developers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const fetchDevelopersList = async (params: FetchDevelopersListParams): Pr
url: `${URLS.developers}/?${setQueryParams(params)}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -42,7 +42,7 @@ export const updateDeveloperById = async (params: UpdateDeveloperByIdParams) =>
api: window.reapit.config.marketplaceApiUrl,
method: 'PUT',
body: rest,
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -58,7 +58,7 @@ export const fetchDeveloperBillingPeriod = async (params: FetchDeveloperBillingP
url: `${URLS.developers}/costs/${period}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions packages/admin-portal/src/services/installations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const fetchInstallationsList = async (
url: `${URLS.installations}?${setQueryParams(params)}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand All @@ -45,7 +45,7 @@ export const fetchApiKeyInstallationById = async (params: FetchApiKeyInstallatio
url: `${URLS.installations}/${installationId}/apiKey`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-portal/src/services/scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const fetchScopesList = async (): Promise<ScopeModel[]> => {
url: `${URLS.scopes}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
return response
} catch (error) {
Expand Down
43 changes: 29 additions & 14 deletions packages/admin-portal/src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@ import { StringMap } from '@/types/core'
import { API_VERSION } from './constants'
import { reapitConnectBrowserSession } from '@/core/connect-session'

export const generateHeader = (marketplaceApiKey): StringMap => ({
'Content-Type': 'application/json',
'X-Api-Key': marketplaceApiKey,
})
export const generateHeader = async (marketplaceApiKey: string): Promise<StringMap> => {
return window.reapit.config.appEnv === 'production'
? await initAuthorizedRequestHeaders()
: {
'Content-Type': 'application/json',
'X-Api-Key': marketplaceApiKey,
}
}

export const generateHeaderWithApiV2 = (marketplaceApiKey): StringMap => ({
...generateHeader(marketplaceApiKey),
'api-version': '2',
})
export const generateHeaderWithApiV2 = async (marketplaceApiKey: string): Promise<StringMap> => {
const headers =
window.reapit.config.appEnv === 'production'
? await initAuthorizedRequestHeaders()
: {
'Content-Type': 'application/json',
'X-Api-Key': marketplaceApiKey,
}
return {
...headers,
'api-version': '2',
}
}

export const initAuthorizedRequestHeaders = async () => {
const session = await reapitConnectBrowserSession.connectSession()
const bearerToken = session ? session.accessToken : ''

return {
Authorization: `Bearer ${bearerToken}`,
'api-version': API_VERSION,
'Content-Type': 'application/json',
if (session && session.accessToken) {
return {
Authorization: `Bearer ${session.accessToken}`,
'api-version': API_VERSION,
'Content-Type': 'application/json',
}
}

throw new Error('Cant get access token')
}
4 changes: 2 additions & 2 deletions packages/connect-session/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module.exports = {
global: {
branches: 78,
functions: 100,
lines: 100,
statements: 99
lines: 99,
statements: 98
},
},
}
2 changes: 1 addition & 1 deletion packages/connect-session/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reapit/connect-session",
"version": "2.0.2",
"version": "2.0.3",
"description": "OAuth Flow for Reapit Connect",
"keywords": [],
"homepage": "https://github.com/reapit/foundations#readme",
Expand Down
4 changes: 2 additions & 2 deletions packages/connect-session/src/browser/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('ReapitConnectBrowserSession', () => {

await session.connectSession()

expect(mockedAuthEndpoint).toHaveBeenCalled()
expect(mockedAuthEndpoint).toHaveBeenCalledTimes(1)
})

it('should redirect to login page if token endpoint fails', async () => {
Expand All @@ -168,7 +168,7 @@ describe('ReapitConnectBrowserSession', () => {
await session.connectSession()

expect(window.fetch).toHaveBeenCalledTimes(1)
expect(mockedAuthEndpoint).toHaveBeenCalled()
expect(mockedAuthEndpoint).toHaveBeenCalledTimes(1)
})

it('should redirect to login if the method is called on session', async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/connect-session/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ export class ReapitConnectBrowserSession {

private handleError(error: string) {
console.error('Reapit Connect Error:', error)
this.connectAuthorizeRedirect()
}

// set a redirect URI to my page where I instantiated the flow, by decoding the state object
Expand Down Expand Up @@ -254,7 +253,7 @@ export class ReapitConnectBrowserSession {
}

// The token endpoint failed to get a session so send me to login to get a new session
throw new Error('Failed to fetch session, redirecting to authorize to re-start OAuth Flow')
this.connectAuthorizeRedirect()
} catch (err) {
return this.handleError(`Reapit Connect Session error ${err.message}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('TransactionHistory', () => {
url: `${URLS.trafficEventBilling}/2020-01/download?applicationId=1&applicationId=2`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
expect(mockEvent.preventDefault).toHaveBeenCalled()
expect(mockEvent.preventDefault).toHaveBeenCalled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const createHandleDownLoadButtonOnClickFn = ({
url: `${URLS.trafficEventBilling}/${month}/download?${params.toString()}`,
api: window.reapit.config.marketplaceApiUrl,
method: 'GET',
headers: generateHeader(window.reapit.config.marketplaceApiKey),
headers: await generateHeader(window.reapit.config.marketplaceApiKey),
})
const fileName = `reapit-billing-data-${month}.csv`
FileSaver.saveAs(blob, fileName)
Expand Down
Loading

0 comments on commit e04da4e

Please sign in to comment.