-
Notifications
You must be signed in to change notification settings - Fork 334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: saml upload #9750
Merged
Merged
feat: saml upload #9750
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import graphql from 'babel-plugin-relay/macro' | ||
import {useMutation} from 'react-relay' | ||
import {useUploadIdPMetadataMutation as TuseUploadIdPMetadataMutation} from '../__generated__/useUploadIdPMetadataMutation.graphql' | ||
|
||
const mutation = graphql` | ||
mutation useUploadIdPMetadataMutation($file: File!, $orgId: ID!) { | ||
uploadIdPMetadata(file: $file, orgId: $orgId) { | ||
... on ErrorPayload { | ||
error { | ||
message | ||
} | ||
} | ||
... on UploadIdPMetadataSuccess { | ||
url | ||
} | ||
} | ||
} | ||
` | ||
interface TTuseUploadIdPMetadataMutation extends Omit<TuseUploadIdPMetadataMutation, 'variables'> { | ||
variables: Omit<TuseUploadIdPMetadataMutation['variables'], 'file'> | ||
uploadables: {file: File} | ||
} | ||
|
||
export const useUploadIdPMetadata = () => { | ||
const [commit, submitting] = useMutation<TTuseUploadIdPMetadataMutation>(mutation) | ||
type Execute = ( | ||
config: Parameters<typeof commit>[0] & {uploadables: {file: File}} | ||
) => ReturnType<typeof commit> | ||
|
||
const execute: Execute = (config) => { | ||
const {variables} = config | ||
const {orgId} = variables | ||
return commit({ | ||
updater: (store) => { | ||
const org = store.get(orgId) | ||
org?.setValue(orgId, 'id') | ||
}, | ||
// allow components to override default handlers | ||
...config | ||
}) | ||
} | ||
return [execute, submitting] as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/server/graphql/public/mutations/uploadIdPMetadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import getFileStoreManager from '../../../fileStorage/getFileStoreManager' | ||
import {MutationResolvers} from '../resolverTypes' | ||
|
||
const uploadIdPMetadata: MutationResolvers['uploadIdPMetadata'] = async (_, {file, orgId}) => { | ||
// VALIDATION | ||
const {contentType, buffer: jsonBuffer} = file | ||
const buffer = Buffer.from(jsonBuffer.data) | ||
if (!contentType || !contentType.includes('xml')) { | ||
return {error: {message: 'file must be XML'}} | ||
} | ||
if (buffer.byteLength > 1000000) { | ||
return {error: {message: 'file must be less than 1MB'}} | ||
} | ||
if (buffer.byteLength <= 1) { | ||
return {error: {message: 'file must be larger than 1 byte'}} | ||
} | ||
|
||
// RESOLUTION | ||
const manager = getFileStoreManager() | ||
const url = await manager.putOrgIdPMetadata(buffer, orgId) | ||
return {url} | ||
} | ||
|
||
export default uploadIdPMetadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const getResolverDotPath = ( | ||
dotPath: ResolverDotPath, | ||
source: Record<string, any>, | ||
args: Record<string, any> | ||
) => { | ||
return dotPath.split('.').reduce((val: any, key) => val?.[key], {source, args}) | ||
} | ||
|
||
export type ResolverDotPath = `source.${string}` | `args.${string}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,16 @@ | ||
import {rule} from 'graphql-shield' | ||
import {GQLContext} from '../../graphql' | ||
import {TierEnum} from '../resolverTypes' | ||
import {ResolverDotPath, getResolverDotPath} from './getResolverDotPath' | ||
|
||
const resolve = async (requiredTier: TierEnum, orgId: string, {dataLoader}: GQLContext) => { | ||
const organization = await dataLoader.get('organizations').load(orgId) | ||
if (!organization) return new Error('Organization not found') | ||
const {tier} = organization | ||
if (tier !== requiredTier) return new Error(`Organization is not ${requiredTier}`) | ||
return true | ||
} | ||
|
||
export const isOrgTierSource = (requiredTier: TierEnum) => | ||
rule(`isOrgTierSource-${requiredTier}`, {cache: 'strict'})( | ||
async ({id: orgId}, _args, context: GQLContext) => { | ||
return resolve(requiredTier, orgId, context) | ||
} | ||
) | ||
|
||
export const isOrgTier = (requiredTier: TierEnum) => | ||
rule(`isOrgTier-${requiredTier}`, {cache: 'strict'})( | ||
async (_source, {orgId}, context: GQLContext) => { | ||
return resolve(requiredTier, orgId, context) | ||
export const isOrgTier = (orgIdDotPath: ResolverDotPath, requiredTier: TierEnum) => | ||
rule(`isViewerOnOrg-${orgIdDotPath}-${requiredTier}`, {cache: 'strict'})( | ||
async (source, args, {dataLoader}: GQLContext) => { | ||
const orgId = getResolverDotPath(orgIdDotPath, source, args) | ||
const organization = await dataLoader.get('organizations').load(orgId) | ||
if (!organization) return new Error('Organization not found') | ||
const {tier} = organization | ||
if (tier !== requiredTier) return new Error(`Organization is not ${requiredTier}`) | ||
return true | ||
} | ||
) |
43 changes: 16 additions & 27 deletions
43
packages/server/graphql/public/rules/isViewerBillingLeader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,20 @@ | ||
import {rule} from 'graphql-shield' | ||
import {getUserId} from '../../../utils/authorization' | ||
import {GQLContext} from '../../graphql' | ||
import {ResolverDotPath, getResolverDotPath} from './getResolverDotPath' | ||
|
||
const resolve = async (orgId: string, {authToken, dataLoader}: GQLContext) => { | ||
const viewerId = getUserId(authToken) | ||
const organizationUser = await dataLoader | ||
.get('organizationUsersByUserIdOrgId') | ||
.load({orgId, userId: viewerId}) | ||
if (!organizationUser) return new Error('Organization User not found') | ||
const {role} = organizationUser | ||
if (role !== 'BILLING_LEADER' && role !== 'ORG_ADMIN') | ||
return new Error('User is not billing leader') | ||
return true | ||
} | ||
|
||
export const isViewerBillingLeader = rule({cache: 'strict'})(async ( | ||
_source, | ||
{orgId}, | ||
context: GQLContext | ||
) => { | ||
return resolve(orgId, context) | ||
}) | ||
|
||
export const isViewerBillingLeaderSource = rule({cache: 'strict'})(async ( | ||
{id: orgId}, | ||
_args, | ||
context: GQLContext | ||
) => { | ||
return resolve(orgId, context) | ||
}) | ||
export const isViewerBillingLeader = (orgIdDotPath: ResolverDotPath) => | ||
rule(`isViewerBillingLeader-${orgIdDotPath}`, {cache: 'strict'})( | ||
async (source, args, {authToken, dataLoader}: GQLContext) => { | ||
const orgId = getResolverDotPath(orgIdDotPath, source, args) | ||
const viewerId = getUserId(authToken) | ||
const organizationUser = await dataLoader | ||
.get('organizationUsersByUserIdOrgId') | ||
.load({orgId, userId: viewerId}) | ||
if (!organizationUser) return new Error('Organization User not found') | ||
const {role} = organizationUser | ||
if (role !== 'BILLING_LEADER' && role !== 'ORG_ADMIN') | ||
return new Error('User is not billing leader') | ||
return true | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {rule} from 'graphql-shield' | ||
import {getUserId} from '../../../utils/authorization' | ||
import {GQLContext} from '../../graphql' | ||
import {ResolverDotPath, getResolverDotPath} from './getResolverDotPath' | ||
|
||
export const isViewerOnOrg = (orgIdDotPath: ResolverDotPath) => | ||
rule(`isViewerOnOrg-${orgIdDotPath}`, {cache: 'strict'})( | ||
async (source, args, {authToken, dataLoader}: GQLContext) => { | ||
const orgId = getResolverDotPath(orgIdDotPath, source, args) | ||
const viewerId = getUserId(authToken) | ||
const organizationUser = await dataLoader | ||
.get('organizationUsersByUserIdOrgId') | ||
.load({orgId, userId: viewerId}) | ||
if (!organizationUser) return new Error('Viewer is not on Organization') | ||
return true | ||
} | ||
) |
22 changes: 22 additions & 0 deletions
22
packages/server/graphql/public/typeDefs/uploadIdPMetadata.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
extend type Mutation { | ||
""" | ||
Upload the IdP Metadata file for an org for those who cannot self-host the file | ||
""" | ||
uploadIdPMetadata( | ||
""" | ||
the XML Metadata file for the IdP | ||
""" | ||
file: File! | ||
|
||
""" | ||
The orgId to upload the IdP Metadata for | ||
""" | ||
orgId: ID! | ||
): UploadIdPMetadataPayload! | ||
} | ||
|
||
union UploadIdPMetadataPayload = ErrorPayload | UploadIdPMetadataSuccess | ||
|
||
type UploadIdPMetadataSuccess { | ||
url: String! | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious on your thoughts about this new pattern.
I want to reuse rules as much as I can. For example, this rule requires an orgId to determine if the viewer is on the org. That
orgId
may exist in an arg, or in the source, and it could have any name. I think a dot notation path is the cleanest way to do this, but wonder what you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! Not having type safety is my main concern, but that's still probably safer that accidentally mixing
isOrgTierSource
forisOrgTier