Skip to content

Commit

Permalink
Working impl. (#2826)
Browse files Browse the repository at this point in the history
  • Loading branch information
betimshahini authored Jan 28, 2024
1 parent 5ca7b8c commit 5d80b01
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion platform/galaxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const plugins = [
galaxyApiKey: {
type: 'apiKey',
in: 'header',
name: 'X-GALAXY-API-KEY',
name: 'X-GALAXY-KEY',
description: 'Galaxy API Key',
},
bearerAuthorization: {
Expand Down
60 changes: 60 additions & 0 deletions platform/galaxy/src/schema/resolvers/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { ResolverContext } from './common'
import createCoreClient from '@proofzero/platform-clients/core'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import { GraphQLError } from 'graphql/error'
import { AppAPIKeyHeader } from '@proofzero/types/headers'
import {
ApplicationURN,
ApplicationURNSpace,
} from '@proofzero/urns/application'
import * as jose from 'jose'

const authorizationResolvers: Resolvers = {
Query: {
Expand All @@ -29,6 +36,53 @@ const authorizationResolvers: Resolvers = {
clientId,
})
},
getAuthorizedIdentities: async (
_parent: any,
{ opts }: { opts: { limit: number; offset: number } },
{ env, apiKey, traceSpan }: ResolverContext
) => {
const coreClient = createCoreClient(env.Core, {
...generateTraceContextHeaders(traceSpan),
[AppAPIKeyHeader]: apiKey,
})
const { limit, offset } = opts

//Check if valid numbers, including value of 0 for offset
if (
limit == null ||
offset == null ||
!Number.isInteger(limit) ||
!Number.isInteger(offset) ||
limit > 50 ||
limit < 1
)
throw new GraphQLError(
'Limit and offset numbers need to be provided, with the limit beging between 1 and 50'
)

let clientIdFromApiKey
try {
const apiKeyApplicationURN = jose.decodeJwt(apiKey)
.sub as ApplicationURN
clientIdFromApiKey =
ApplicationURNSpace.nss(apiKeyApplicationURN).split('/')[1]
} catch (e) {
console.error('Error parsing clientId', e)
throw new GraphQLError('Could not retrieve clientId from API key.')
}

const edgeResults =
await coreClient.starbase.getAuthorizedIdentities.query({
client: clientIdFromApiKey,
opt: {
limit,
offset,
},
})
return edgeResults.identities.map(({ identityURN, imageURL, name }) => {
return { identityURN, imageURL, name }
})
},
},
Mutation: {
setExternalAppData: async (
Expand Down Expand Up @@ -60,6 +114,12 @@ const AuthorizationResolverComposition = {
isAuthorized(),
logAnalytics(),
],
'Query.getAuthorizedIdentities': [
requestLogging(),
setupContext(),
validateApiKey(),
logAnalytics(),
],
'Mutation.setExternalAppData': [
requestLogging(),
setupContext(),
Expand Down
7 changes: 7 additions & 0 deletions platform/galaxy/src/schema/types/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export default /* GraphQL */ `
type AuthorizationIdentity {
identityURN: String
name: String
imageURL: String
}
type Query {
getExternalAppData: JSON
getAuthorizedIdentities(opts: Pagination!): [AuthorizationIdentity]
}
type Mutation {
Expand Down
5 changes: 5 additions & 0 deletions platform/galaxy/src/schema/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export default /* GraphQL */ `
scalar JSON
input Pagination {
offset: Int!
limit: Int!
}
`
1 change: 1 addition & 0 deletions platform/starbase/src/jsonrpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export const appRouter = t.router({
getAuthorizedIdentities: t.procedure
.use(AuthorizationTokenFromHeader)
.use(ValidateJWT)
.use(ApiKeyExtractMiddleware)
.use(LogUsage)
.use(Analytics)
.use(OwnAppsMiddleware)
Expand Down

0 comments on commit 5d80b01

Please sign in to comment.