Skip to content

Commit

Permalink
put content file under scope of logged in user
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkorus committed Jan 11, 2022
1 parent be25e56 commit 9e982b2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type Authentication = { __TYPE__: "Authentication" }
export type Authentication = {
principal: string
}

export type Config = { __TYPE__: "Config" }

Expand Down
12 changes: 7 additions & 5 deletions src/aws/auth/ClientCredentialsAuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export function ClientCredentialsAuthProvider({children}:ClientCredentialsAuthPr
throw new Error("Invalid configuration. Required properties not found")
}

const authenticationProvider = authenticateClientIdClientSecret(config.clientIdSecret)
const authentication = await authenticationProvider().then(validateCredentials)
setCredentials(authentication)
const credentials = await authenticateClientIdClientSecret(config.clientIdSecret)()
setCredentials(toAuthentication(config.clientIdSecret.clientId, credentials))
setLoading(false)
}

Expand All @@ -47,8 +46,11 @@ type ClientIdSecretProperties = {
clientSecret: string
}

function validateCredentials(credentials:Credentials):AWSAuthentication {
return credentials as AWSAuthentication
function toAuthentication(clientId:string, credentials:Credentials):AWSAuthentication {
return {
principal: clientId,
...credentials
}
}

function isClientIdSecretConfig(config:Config):config is ClientIdSecretConfig {
Expand Down
12 changes: 8 additions & 4 deletions src/aws/auth/IdentityPoolAuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"
import { createOIDCClient, ErrorResult, IdTokenResult, IssuerConfig } from "../../oauth2";
import { ConfigContext, AuthContext, Config } from "../../api"
import { AWSAuthentication } from "..";
import { IdToken } from '../../oauth2/common';

type IdentityPoolAuthProviderProps = {
children: React.ReactNode
Expand Down Expand Up @@ -84,8 +85,8 @@ export function IdentityPoolAuthProvider({children}:IdentityPoolAuthProviderProp
region: config.cognito.region
}
})
const authentication = await awsCredentialsProvider().then(validateCredentials)
setCredentials(authentication)
const congnitoIdentityCredentials = await awsCredentialsProvider()
setCredentials(toAuthentication(result.idToken, congnitoIdentityCredentials))
setLoading(false)
}
}
Expand All @@ -99,6 +100,9 @@ export function IdentityPoolAuthProvider({children}:IdentityPoolAuthProviderProp
</AuthContext.Provider>
}

function validateCredentials(credentials:Credentials):AWSAuthentication {
return credentials as AWSAuthentication
function toAuthentication(token:IdToken, credentials:Credentials):AWSAuthentication {
return {
principal: token.sub || "",
...credentials
}
}
5 changes: 3 additions & 2 deletions src/aws/realms/AWSBackend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { S3Client, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"
import { hostHeaderMiddlewareOptions } from "@aws-sdk/middleware-host-header"
import { HttpRequest } from "@aws-sdk/protocol-http";
import { Credentials, Provider } from "@aws-sdk/types"
import { Credentials } from "@aws-sdk/types"
import { S3RealmsProperties } from './realms.api'

export default class AWSBackend {
Expand Down Expand Up @@ -44,7 +44,8 @@ export default class AWSBackend {
return toString(Body as ReadableStream | Blob)
} catch(error) {
const { httpStatusCode } = error.$metadata
if(404 == httpStatusCode) {
// accomodate situations when user don't have ListBucket permissions
if(404 == httpStatusCode || 403 == httpStatusCode) {
return ''
} else {
throw error
Expand Down
27 changes: 22 additions & 5 deletions src/aws/realms/realms.api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Papa from "papaparse"
import AWSBackend from "./AWSBackend"
import { Credentials, Provider } from "@aws-sdk/types";
import { RealmDefinition } from "../../api"
import { AWSAuthentication } from "..";

let resolveBackend:(backend:AWSBackend) => void
let resolveSource:(source:S3RealmsProperties) => void
let resolvePrincipal:(prinsipal:string) => void

const awsBackendPromise = new Promise<AWSBackend>(resolve => {
resolveBackend = resolve
Expand All @@ -14,22 +15,30 @@ const awsSourcePromise = new Promise<S3RealmsProperties>(resolve => {
resolveSource = resolve
})

const principalPromise = new Promise<string>(resolve => {
resolvePrincipal = resolve
})


export type S3RealmsProperties = {
bucket: string
object: string
// @deprecated
object?: string
objectPrefix?: string
objectName?: string
endpoint?: string
region?:string
}

export const setupRealms = (source:S3RealmsProperties, credentials:Credentials) => {
export const setupRealms = (source:S3RealmsProperties, credentials:AWSAuthentication) => {
resolveBackend(new AWSBackend(source, credentials))
resolveSource(source)
resolvePrincipal(credentials.principal)
}

export const fetchRealms = async () => {
const awsBackend = await awsBackendPromise
const source = await awsSourcePromise
const source = await calculateObjectDetails()
const value = await awsBackend.fetchResource(source.bucket, source.object)
const realms = Papa.parse<RealmDefinition>(value, { header: true, transform: transform }).data
realms?.forEach(realm => realm.persisted = true)
Expand All @@ -38,7 +47,7 @@ export const fetchRealms = async () => {

export const pushRealms = async (realms:RealmDefinition[]) => {
const awsBackend = await awsBackendPromise
const awsSource = await awsSourcePromise
const awsSource = await calculateObjectDetails()
await awsBackend.storeResource(awsSource.bucket, awsSource.object, Papa.unparse(realms))
realms?.forEach(realm => realm.persisted = true)
return realms
Expand All @@ -50,3 +59,11 @@ function transform(value:string, headerName:string) {
}
return value;
}

async function calculateObjectDetails():Promise<{bucket:string, object: string}> {
const awsSource = await awsSourcePromise
const principal = await principalPromise
const bucket = awsSource.bucket
const path = principal + "/" + awsSource.object
return {bucket: bucket, object: path}
}

0 comments on commit 9e982b2

Please sign in to comment.