This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dara Hayes
committed
Jul 11, 2019
1 parent
16707ac
commit fbb0cef
Showing
12 changed files
with
405 additions
and
107 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
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,37 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const session = require('express-session') | ||
const Keycloak = require('keycloak-connect') | ||
|
||
function configureKeycloak(app, graphqlPath) { | ||
|
||
const keycloakConfig = JSON.parse(fs.readFileSync(path.resolve(__dirname, './config/keycloak.json'))) | ||
|
||
const memoryStore = new session.MemoryStore() | ||
|
||
app.use(session({ | ||
secret: process.env.SESSION_SECRET_STRING || 'this should be a long secret', | ||
resave: false, | ||
saveUninitialized: true, | ||
store: memoryStore | ||
})) | ||
|
||
const keycloak = new Keycloak({ | ||
store: memoryStore | ||
}, keycloakConfig) | ||
|
||
// Install general keycloak middleware | ||
app.use(keycloak.middleware({ | ||
admin: graphqlPath | ||
})) | ||
|
||
// Protect the main route for all graphql services | ||
// Disable unauthenticated access | ||
app.use(graphqlPath, keycloak.middleware()) | ||
|
||
return { keycloak } | ||
} | ||
|
||
module.exports = { | ||
configureKeycloak | ||
} |
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,93 @@ | ||
const express = require('express') | ||
const { PubSub } = require('graphql-subscriptions') | ||
const { execute, subscribe } = require('graphql') | ||
const { SubscriptionServer } = require('subscriptions-transport-ws') | ||
|
||
const { ApolloServer, gql } = require('apollo-server-express') | ||
|
||
const { configureKeycloak } = require('./common') | ||
|
||
const { | ||
KeycloakContext, | ||
KeycloakSubscriptionContext, | ||
KeycloakTypeDefs, | ||
KeycloakSchemaDirectives, | ||
KeycloakSubscriptionHandler | ||
} = require('../') | ||
|
||
|
||
const app = express() | ||
|
||
const graphqlPath = '/graphql' | ||
|
||
// perform the standard keycloak-connect middleware setup on our app | ||
// return the initialized keycloak object | ||
const { keycloak } = configureKeycloak(app, graphqlPath) | ||
|
||
const pubsub = new PubSub() | ||
|
||
// set up the pubsub to publish a message every 2 seconds | ||
const TOPIC = 'HELLO' | ||
setInterval(() => { | ||
pubsub.publish(TOPIC, { testSubscription: `tesing... ${Date.now()}`}) | ||
}, 2000) | ||
|
||
const typeDefs = gql` | ||
type Query { | ||
hello: String! | ||
} | ||
type Subscription { | ||
testSubscription: String! | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: (obj, args, context, info) => { | ||
return `Hello world` | ||
} | ||
}, | ||
Subscription: { | ||
testSubscription: { | ||
subscribe: () => pubsub.asyncIterator(TOPIC) | ||
} | ||
} | ||
} | ||
|
||
const server = new ApolloServer({ | ||
typeDefs: [KeycloakTypeDefs, typeDefs], | ||
schemaDirectives: KeycloakSchemaDirectives, | ||
resolvers, | ||
context: ({ req }) => { | ||
return { | ||
kauth: new KeycloakContext({ req }) | ||
} | ||
} | ||
}) | ||
|
||
server.applyMiddleware({ app }) | ||
|
||
const port = 4000 | ||
|
||
const httpServer = app.listen({ port }, () => { | ||
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`) | ||
|
||
// Initialize the keycloak subscription handler passing in our keycloak instance | ||
const keycloakSubscriptionHandler = new KeycloakSubscriptionHandler({ keycloak }) | ||
new SubscriptionServer({ | ||
execute, | ||
subscribe, | ||
schema: server.schema, | ||
onConnect: async (connectionParams, websocket, connectionContext) => { | ||
const token = await keycloakSubscriptionHandler.onSubscriptionConnect(connectionParams) | ||
return { | ||
kauth: new KeycloakSubscriptionContext(token) | ||
} | ||
} | ||
}, { | ||
server: httpServer, | ||
path: graphqlPath | ||
}) | ||
}) | ||
|
Oops, something went wrong.