-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Manuel Ruck <[email protected]>
- Loading branch information
Manuel Ruck
committed
Oct 23, 2023
1 parent
fe30ff4
commit db94bf9
Showing
26 changed files
with
2,874 additions
and
9,831 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ diffs/* | |
.env* | ||
!.env.example | ||
built | ||
generated |
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,8 +1,10 @@ | ||
schema: http://localhost:4000/ | ||
overwrite: true | ||
schema: './src/graphql/schemas/*.graphql' | ||
# documents: './src/**/*.graphql' | ||
generates: | ||
./src/graphql/resolvers/types.ts: | ||
config: | ||
contextType: ../../types/graphqlContext#GraphQlContext | ||
./src/generated/graphql.ts: | ||
plugins: | ||
- typescript | ||
- typescript-resolvers | ||
- 'typescript' | ||
- 'codegen/typedefs.js' | ||
- 'typescript-operations' | ||
- 'typescript-resolvers' |
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,16 @@ | ||
var graphqlUtils = require('@graphql-tools/utils'); | ||
var graphql = require('graphql'); | ||
|
||
// https://github.com/dotansimha/graphql-code-generator/issues/3899 | ||
var print = function (schema) { | ||
var escapedSchema = schema.replace(/\\`/g, '\\\\`').replace(/`/g, '\\`'); | ||
|
||
// import { gql } from "@apollo/client/core" | ||
return '\n' + 'export const typeDefs = `' + escapedSchema + '`;'; | ||
}; | ||
|
||
module.exports = { | ||
plugin: function (schema) { | ||
return print(graphql.stripIgnoredCharacters(graphqlUtils.printSchemaWithDirectives(schema))); | ||
}, | ||
}; |
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
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,61 +1,43 @@ | ||
import { defaultFieldResolver } from 'graphql'; | ||
import { Log } from '../../services/logger'; | ||
import { SchemaDirectiveVisitor } from 'apollo-server-express'; | ||
import { defaultFieldResolver, GraphQLSchema } from 'graphql'; | ||
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils'; | ||
|
||
class AuthDirective extends SchemaDirectiveVisitor { | ||
visitObject(type): void { | ||
this.ensureFieldsWrapped(type); | ||
type._requiredAuthRole = this.args.requires; | ||
} | ||
const directiveTypeDefs = ` | ||
directive @auth( | ||
requires: Role = USER | ||
) on FIELD_DEFINITION | ||
// Visitor methods for nested types like fields and arguments | ||
// also receive a details object that provides information about | ||
// the parent and grandparent types. | ||
visitFieldDefinition(field, details) { | ||
this.ensureFieldsWrapped(details.objectType); | ||
field._requiredAuthRole = this.args.requires; | ||
} | ||
|
||
ensureFieldsWrapped(objectType) { | ||
// Mark the GraphQLObjectType object to avoid re-wrapping: | ||
if (objectType._authFieldsWrapped) return; | ||
objectType._authFieldsWrapped = true; | ||
|
||
const fields = objectType.getFields(); | ||
Object.keys(fields).forEach((fieldName) => { | ||
const field = fields[fieldName]; | ||
const { resolve = defaultFieldResolver } = field; | ||
|
||
field.resolve = async (...args) => { | ||
// Get the required Role from the field first, falling back | ||
// to the objectType if no Role is required by the field: | ||
const requiredRole = field._requiredAuthRole || objectType._requiredAuthRole; | ||
|
||
if (!requiredRole) { | ||
return resolve.apply(this, args); | ||
} | ||
|
||
const [, , context] = args; | ||
let allow = true; | ||
if (requiredRole === 'BACKEND') { | ||
if ( | ||
!context.req.headers['bio-auth-token'] || | ||
context.req.headers['bio-auth-token'] !== process.env.BIO_EDIT_TOKEN | ||
) { | ||
Log.warn( | ||
`Connection to Bio blocked from ${context.req.connection.remoteAddress} for role 'BACKEND'`, | ||
); | ||
allow = false; | ||
} | ||
} | ||
if (!allow) { | ||
throw new Error(`not authorized ${context.req.connection.remoteAddress}`); | ||
} | ||
|
||
return resolve.apply(this, args); | ||
}; | ||
}); | ||
} | ||
enum Role { | ||
BACKEND | ||
USER | ||
} | ||
|
||
export default AuthDirective; | ||
`; | ||
|
||
const hasPermissions = (context, role) => | ||
context.req.headers['bio-auth-token'] && | ||
context.req.headers['bio-auth-token'] === process.env.BIO_EDIT_TOKEN; | ||
|
||
export const authDirective = (directiveName: string) => { | ||
return { | ||
authDirectiveTypeDefs: directiveTypeDefs, | ||
authDirectiveTransformer: (schema: GraphQLSchema) => | ||
mapSchema(schema, { | ||
[MapperKind.OBJECT_FIELD](fieldConfig) { | ||
const authDirective = getDirective(schema, fieldConfig, directiveName)?.[0]; | ||
if (authDirective) { | ||
const { resolve = defaultFieldResolver } = fieldConfig; | ||
console.log(authDirective); | ||
const { requires } = authDirective; | ||
fieldConfig.resolve = async (parent, args, context, info) => { | ||
console.log(hasPermissions(context, requires)); | ||
if (!hasPermissions(context, requires)) { | ||
throw new Error('You have not enough permissions!'); | ||
} | ||
const result = await resolve(parent, args, context, info); | ||
return result; | ||
}; | ||
return fieldConfig; | ||
} | ||
}, | ||
}), | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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
2 changes: 0 additions & 2 deletions
2
...ag.io/api/src/graphql/schemas/Document.ts → .../api/src/graphql/schemas/Document.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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
export default ` | ||
type Document { | ||
editor: String | ||
number: String | ||
type: String | ||
url: 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
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
Oops, something went wrong.