Skip to content

Commit 2d6c083

Browse files
committed
Merge branch 'paul/fixApiKey' into deploy
2 parents ddfaa68 + 40bfc6b commit 2d6c083

File tree

8 files changed

+65
-13
lines changed

8 files changed

+65
-13
lines changed

src/demo/clientUtil.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const getPresetDates = function(): PresetDates {
8282
}
8383

8484
export const getCustomData = async (
85-
appId: string,
85+
apiKey: string,
8686
pluginIds: string[],
8787
start: string,
8888
end: string,
@@ -97,7 +97,7 @@ export const getCustomData = async (
9797
) {
9898
trueTimePeriod = 'daymonth'
9999
}
100-
const query = { start, end, appId, pluginIds, timePeriod: trueTimePeriod }
100+
const query = { start, end, apiKey, pluginIds, timePeriod: trueTimePeriod }
101101
const response = await fetch(endPoint, {
102102
headers: {
103103
'Content-Type': 'application/json'
@@ -184,10 +184,10 @@ export const getAppId = async (apiKey: string): Promise<AppIdResponse> => {
184184
}
185185

186186
export const getPartnerIds = async (
187-
appId: string
187+
apiKey: string
188188
): Promise<PartnerIdsResponse> => {
189189
const partners = Object.keys(Partners)
190-
const url = `${apiHost}/v1/getPluginIds?appId=${appId}`
190+
const url = `${apiHost}/v1/getPluginIds?apiKey=${apiKey}`
191191
const response = await fetch(url)
192192
const json = await response.json()
193193
const ids = asGetPartnerIds(json)

src/demo/components/Custom.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Custom extends Component<CustomProps, CustomState> {
7979
async componentDidMount(): Promise<void> {
8080
const { appId, redirect } = await getAppId(this.props.apiKey)
8181
this.setState({ appId, redirect })
82-
const { partnerIds } = await getPartnerIds(this.state.appId)
82+
const { partnerIds } = await getPartnerIds(this.props.apiKey)
8383
this.setState({ partnerIds })
8484
if (
8585
typeof this.props.match.params.start === 'string' &&
@@ -98,7 +98,7 @@ class Custom extends Component<CustomProps, CustomState> {
9898
console.time('getData')
9999
this.setState({ loading: true })
100100
const data = await getCustomData(
101-
this.state.appId,
101+
this.props.apiKey,
102102
this.state.partnerIds,
103103
start,
104104
end

src/demo/components/Preset.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Preset extends Component<PresetProps, PresetState> {
8888
async componentDidMount(): Promise<void> {
8989
const { appId, redirect } = await getAppId(this.props.apiKey)
9090
this.setState({ appId, redirect })
91-
const { partnerIds } = await getPartnerIds(this.state.appId)
91+
const { partnerIds } = await getPartnerIds(this.props.apiKey)
9292
this.setState({ partnerIds })
9393
await this.getGraphData()
9494
}
@@ -108,7 +108,7 @@ class Preset extends Component<PresetProps, PresetState> {
108108
const startDate = timeRanges[0]
109109
const endDate = timeRanges[1]
110110
const newData = await getCustomData(
111-
this.state.appId,
111+
this.props.apiKey,
112112
this.state.partnerIds,
113113
startDate,
114114
endDate,

src/demo/demo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class App extends Component<
9696

9797
async getPluginIds(): Promise<void> {
9898
const partners = Object.keys(Partners)
99-
const url = `${apiHost}/v1/getPluginIds?appId=${this.state.appId}`
99+
const url = `${apiHost}/v1/getPluginIds?apiKey=${this.state.apiKey}`
100100
const response = await fetch(url)
101101
const json = await response.json()
102102
const existingPartners = json.filter(pluginId =>

src/routes/v1/analytics.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { asArray, asObject, asString } from 'cleaners'
22
import Router from 'express-promise-router'
33

44
import { cacheAnalytic } from '../../dbutils'
5+
import { validateApiKey } from '../../util/validateApiKey'
56

67
const asAnalyticsReq = asObject({
78
start: asString,
89
end: asString,
9-
appId: asString,
10+
apiKey: asString,
1011
pluginIds: asArray(asString),
1112
timePeriod: asString
1213
})
@@ -21,7 +22,16 @@ analyticsRouter.post(`/`, async function(req, res) {
2122
res.status(400).send(`Missing Request Fields`)
2223
return
2324
}
24-
const { start, end, appId, pluginIds } = analyticsQuery
25+
const { start, end, apiKey, pluginIds } = analyticsQuery
26+
27+
// Validate API key and get appId
28+
let appId: string
29+
try {
30+
appId = await validateApiKey(apiKey)
31+
} catch {
32+
res.status(401).send(`Invalid API Key`)
33+
return
34+
}
2535
const timePeriod = analyticsQuery.timePeriod.toLowerCase()
2636
const queryStart = new Date(start).getTime() / 1000
2737
const queryEnd = new Date(end).getTime() / 1000

src/routes/v1/getPluginIds.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { asObject, asOptional, asString } from 'cleaners'
22
import Router from 'express-promise-router'
33

44
import { reportsApps } from '../../indexApi'
5+
import { validateApiKey } from '../../util/validateApiKey'
56

67
const asPluginIdsReq = asObject({
7-
appId: asString
8+
apiKey: asString
89
})
910
const asPartnerIdsDbReq = asObject({
1011
partnerIds: asObject(
@@ -25,9 +26,19 @@ getPluginIdsRouter.get('/', async function(req, res) {
2526
res.status(400).send(`Missing Request fields.`)
2627
return
2728
}
29+
30+
// Validate API key and get appId
31+
let appId: string
32+
try {
33+
appId = await validateApiKey(queryResult.apiKey)
34+
} catch {
35+
res.status(401).send(`Invalid API Key`)
36+
return
37+
}
38+
2839
const query = {
2940
selector: {
30-
appId: { $eq: queryResult.appId.toLowerCase() }
41+
appId: { $eq: appId.toLowerCase() }
3142
},
3243
fields: ['partnerIds'],
3344
limit: 1

src/routes/v1/getTxInfo.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import { asDbTx, DbTx, Status } from '../../types'
66
import { EdgeTokenId } from '../../util/asEdgeTokenId'
77
import { HttpError } from '../../util/httpErrors'
88
import { trial } from '../../util/trail'
9+
import { validateApiKey } from '../../util/validateApiKey'
910

1011
const asGetTxInfoReq = asObject({
12+
/**
13+
* API key for authentication.
14+
*/
15+
apiKey: asString,
16+
1117
/**
1218
* Prefix of the destination address.
1319
* Minimum 3 character; Maximum 5 characters.
@@ -50,6 +56,14 @@ getTxInfoRouter.get('/', async function(req, res) {
5056
}
5157
)
5258

59+
// Validate API key
60+
try {
61+
await validateApiKey(query.apiKey)
62+
} catch {
63+
res.status(401).send('Invalid API Key')
64+
return
65+
}
66+
5367
if (query.addressPrefix.length < 3) {
5468
res.status(400).send('addressPrefix must be at least 3 characters')
5569
return

src/util/validateApiKey.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { asObject, asString } from 'cleaners'
2+
3+
import { reportsApps } from '../indexApi'
4+
5+
const asApiKeyDbResult = asObject({
6+
appId: asString
7+
})
8+
9+
/**
10+
* Validates an API key and returns the corresponding appId.
11+
* Throws an error if the API key is not recognized.
12+
*/
13+
export async function validateApiKey(apiKey: string): Promise<string> {
14+
const dbResult = await reportsApps.get(apiKey)
15+
const { appId } = asApiKeyDbResult(dbResult)
16+
return appId
17+
}

0 commit comments

Comments
 (0)